Browse Source

Optimize `yii\validators\ExistValidator`

Optimize methods `valueExists()` and `checkTargetRelationExistence()`
tags/2.0.43
Anton 3 years ago committed by GitHub
parent
commit
1474e2d61a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      framework/validators/ExistValidator.php

20
framework/validators/ExistValidator.php

@ -131,15 +131,13 @@ class ExistValidator extends Validator
$relationQuery->andWhere($this->filter);
}
if ($this->forceMasterDb && method_exists($model::getDb(), 'useMaster')) {
$model::getDb()->useMaster(function() use ($relationQuery, &$exists) {
$exists = $relationQuery->exists();
});
$connection = $model::getDb();
if ($this->forceMasterDb && method_exists($connection, 'useMaster')) {
$exists = $connection->useMaster([$relationQuery, 'exists']);
} else {
$exists = $relationQuery->exists();
}
if (!$exists) {
$this->addError($model, $attribute, $this->message);
}
@ -246,9 +244,9 @@ class ExistValidator extends Validator
}
/**
* Check whether value exists in target table
* Check whether value exists in target table.
*
* @param string $targetClass
* @param string $targetClass the model
* @param QueryInterface $query
* @param mixed $value the value want to be checked
* @return bool
@ -259,8 +257,8 @@ class ExistValidator extends Validator
$exists = false;
if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
$db->useMaster(function ($db) use ($query, $value, &$exists) {
$exists = $this->queryValueExists($query, $value);
$exists = $db->useMaster(function () use ($query, $value) {
return $this->queryValueExists($query, $value);
});
} else {
$exists = $this->queryValueExists($query, $value);
@ -271,7 +269,7 @@ class ExistValidator extends Validator
/**
* Run query to check if value exists
* Run query to check if value exists.
*
* @param QueryInterface $query
* @param mixed $value the value to be checked
@ -280,7 +278,7 @@ class ExistValidator extends Validator
private function queryValueExists($query, $value)
{
if (is_array($value)) {
return $query->count("DISTINCT [[$this->targetAttribute]]") == count(array_unique($value)) ;
return $query->count("DISTINCT [[$this->targetAttribute]]") == count(array_unique($value));
}
return $query->exists();
}

Loading…
Cancel
Save