Skip to content

Commit

Permalink
Fixes yiisoft#14484: Fixed yii\validators\UniqueValidator for targe…
Browse files Browse the repository at this point in the history
…t classes with a default scope
  • Loading branch information
laszlovl authored and samdark committed Dec 18, 2017
1 parent 3b1ff66 commit 0bec2b4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Yii Framework 2 Change Log

- Bug #8983: Only truncate the original log file for rotation (matthewyang, developeruz)
- Bug #14276: Fixed I18N format with dotted parameters (developeruz)
- Bug #14484: Fixed `yii\validators\UniqueValidator` for target classes with a default scope (laszlovl, developeruz)
- Bug #14604: Fixed `yii\validators\CompareValidator` `compareAttribute` does not work if `compareAttribute` form ID has been changed (mikk150)
- Bug #15142: Fixed array params replacing in `yii\helpers\BaseUrl::current()` (IceJOKER)
- Bug #15194: Fixed `yii\db\QueryBuilder::insert()` to preserve passed params when building a `INSERT INTO ... SELECT` query for MSSQL, PostgreSQL and SQLite (sergeymakinen)
Expand Down Expand Up @@ -37,6 +38,7 @@ Yii Framework 2 Change Log
- Bug #15317: Regenerate CSRF token if an empty value is given (sammousa)



2.0.13.1 November 14, 2017
--------------------------

Expand Down Expand Up @@ -151,7 +153,6 @@ Yii Framework 2 Change Log
- Chg #14321: `yii\widgets\MaskedInput` is now registering its JavaScript `clientOptions` initialization code in head section (DaveFerger)
- Chg #14487: Changed i18n message error to warning (dmirogin)


2.0.12 June 05, 2017
--------------------

Expand Down
3 changes: 3 additions & 0 deletions framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ private function modelExists($targetClass, $conditions, $model)
// only select primary key to optimize query
$columnsCondition = array_flip($targetClass::primaryKey());
$query->select(array_flip($this->applyTableAlias($query, $columnsCondition)));

// any with relation can't be loaded because related fields are not selected
$query->with = null;
}
$models = $query->limit(2)->asArray()->all();
$n = count($models);
Expand Down
29 changes: 29 additions & 0 deletions tests/framework/validators/UniqueValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,33 @@ public function testExpresionInAttributeColumnName()
$validator->validateAttribute($model, 'title');
$this->assertFalse($model->hasErrors(), 'There were errors: ' . json_encode($model->getErrors()));
}

/**
* Test validating a class with default scope
* @see https://github.com/yiisoft/yii2/issues/14484
*/
public function testFindModelWith()
{
$validator = new UniqueValidator([
'targetAttribute' => ['status', 'profile_id']
]);
$model = WithCustomer::find()->one();
try {
$validator->validateAttribute($model, 'email');
} catch (\Exception $exception) {
$this->fail('Query is crashed because "with" relation cannot be loaded');
}


}
}

class WithCustomer extends Customer {
public static function find() {
$res = parent::find();

$res->with('profile');

return $res;
}
}

0 comments on commit 0bec2b4

Please sign in to comment.