Use low-level query in UniqueValidator (#13461)

Fixes #13453, relates to #13098
This commit is contained in:
Elvira Sheina
2017-01-28 17:38:06 +02:00
committed by Alexander Makarov
parent 015f14e374
commit 240bca515a
2 changed files with 3 additions and 3 deletions

View File

@ -114,6 +114,7 @@ Yii Framework 2 Change Log
- Enh #11464: Populate foreign key names from schema (joaoppereira) - Enh #11464: Populate foreign key names from schema (joaoppereira)
- Bug #13401: Fixed lack of escaping of request dump at exception screens (samdark) - 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 #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) - 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) - Enh #7435: Added `EVENT_BEFORE_RUN`, `EVENT_AFTER_RUN` and corresponding methods to `yii\base\Widget` (petrabarus)

View File

@ -160,8 +160,7 @@ class UniqueValidator extends Validator
$exists = $query->exists(); $exists = $query->exists();
} else { } else {
// if current $model is in the database already we can't use exists() // if current $model is in the database already we can't use exists()
/** @var $models ActiveRecordInterface[] */ $models = $query->select($targetClass::primaryKey())->limit(2)->createCommand()->queryAll();
$models = $query->limit(2)->all();
$n = count($models); $n = count($models);
if ($n === 1) { if ($n === 1) {
$keys = array_keys($conditions); $keys = array_keys($conditions);
@ -173,7 +172,7 @@ class UniqueValidator extends Validator
$exists = $model->getOldPrimaryKey() != $model->getPrimaryKey(); $exists = $model->getOldPrimaryKey() != $model->getPrimaryKey();
} else { } else {
// non-primary key, need to exclude the current record based on PK // 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 { } else {
$exists = $n > 1; $exists = $n > 1;