mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 13:02:24 +08:00
Use low-level query in UniqueValidator (#13461)
Fixes #13453, relates to #13098
This commit is contained in:
committed by
Alexander Makarov
parent
015f14e374
commit
240bca515a
@ -160,8 +160,7 @@ class UniqueValidator extends Validator
|
||||
$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);
|
||||
@ -173,7 +172,7 @@ class UniqueValidator extends Validator
|
||||
$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;
|
||||
|
||||
Reference in New Issue
Block a user