Skip to content

Commit

Permalink
Fixed ambiguous column name in SELECT in UniqueValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe authored and samdark committed May 10, 2017
1 parent 23cc4bf commit 5c72047
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Yii Framework 2 Change Log
- Bug #13594: Fixes insufficient quoting in `yii\db\QueryBuilder::prepareInsertSelectSubQuery()` (sergeymakinen)
- Bug #8120: Fixes LIKE special characters escaping for Cubrid/MSSQL/Oracle/SQLite in `yii\db\QueryBuilder` (sergeymakinen)
- Bug #12715: Exception `SAVEPOINT LEVEL1 does not exist` instead of deadlock exception (Vovan-VE)
- Bug #14042: Fixed ambiguous column name in SELECT in UniqueValidator (cebe)
- Enh #8641: Enhanced `yii\console\Request::resolve()` to prevent passing parameters, that begin from digits (silverfire)
- Enh #13278: `yii\caching\DbQueryDependency` created allowing specification of the cache dependency via `yii\db\QueryInterface` (klimov-paul)
- Enh #13467: `yii\data\ActiveDataProvider` no longer queries models if models count is zero (kLkA, Kolyunya)
Expand Down
7 changes: 6 additions & 1 deletion framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ private function modelExists($targetClass, $conditions, $model)
// if current $model is in the database already we can't use exists()
if ($query instanceof \yii\db\ActiveQuery) {
// only select primary key to optimize query
$query->select($targetClass::primaryKey());
$primaryAlias = array_keys($query->getTablesUsedInFrom())[0];
$columns = $targetClass::primaryKey();
foreach($columns as $c => $column) {
$columns[$c] = "{{{$primaryAlias}}}.$column";
}
$query->select($columns);
}
$models = $query->limit(2)->asArray()->all();
$n = count($models);
Expand Down
20 changes: 20 additions & 0 deletions tests/framework/validators/UniqueValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,24 @@ public function testPrepareQuery()
$expected = "SELECT * FROM {$schema->quoteTableName('validator_main')} WHERE ({$schema->quoteColumnName('val_attr_b')}=:qp0) OR (val_attr_a > 0)";
$this->assertEquals($expected, $query->createCommand()->getSql());
}

/**
* Test ambiguous column name in select clause
* @see https://github.com/yiisoft/yii2/issues/14042
*/
public function testAmbiguousColumnName()
{
$validator = new UniqueValidator([
'filter' => function($query) {
$query->joinWith('items', false);
},
]);
$model = new Order();
$model->id = 42;
$model->customer_id = 1;
$model->total = 800;
$model->save(false);
$validator->validateAttribute($model, 'id');
$this->assertFalse($model->hasErrors());
}
}

0 comments on commit 5c72047

Please sign in to comment.