Skip to content

Commit

Permalink
Use low-level query in UniqueValidator (yiisoft#13461)
Browse files Browse the repository at this point in the history
Fixes yiisoft#13453, relates to yiisoft#13098
  • Loading branch information
developeruz authored and samdark committed Jan 28, 2017
1 parent 015f14e commit 240bca5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Yii Framework 2 Change Log
- Enh #11464: Populate foreign key names from schema (joaoppereira)
- Bug #13401: Fixed lack of escaping of request dump at exception screens (samdark)
- Enh #13417: Allow customizing `yii\data\ActiveDataProvider` in `yii\rest\IndexAction` (leandrogehlen)
- Enh #13453: Select only primary key when counting records in UniqueValidator (developeruz)
- Bug #12599: Fixed MSSQL fail to work with `nvarbinary`. Enhanced SQL scripts compatibility with older versions (samdark)
- Enh #7435: Added `EVENT_BEFORE_RUN`, `EVENT_AFTER_RUN` and corresponding methods to `yii\base\Widget` (petrabarus)

Expand Down
5 changes: 2 additions & 3 deletions framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ private function modelExists($targetClass, $conditions, $model)
$exists = $query->exists();
} else {
// if current $model is in the database already we can't use exists()
/** @var $models ActiveRecordInterface[] */
$models = $query->limit(2)->all();
$models = $query->select($targetClass::primaryKey())->limit(2)->createCommand()->queryAll();
$n = count($models);
if ($n === 1) {
$keys = array_keys($conditions);
Expand All @@ -173,7 +172,7 @@ private function modelExists($targetClass, $conditions, $model)
$exists = $model->getOldPrimaryKey() != $model->getPrimaryKey();
} else {
// non-primary key, need to exclude the current record based on PK
$exists = reset($models)->getPrimaryKey() != $model->getOldPrimaryKey();
$exists = $models[0] != $model->getOldPrimaryKey(true);
}
} else {
$exists = $n > 1;
Expand Down

0 comments on commit 240bca5

Please sign in to comment.