diff --git a/framework/validators/ExistValidator.php b/framework/validators/ExistValidator.php index fb0761e1f8c..679bf57d26b 100644 --- a/framework/validators/ExistValidator.php +++ b/framework/validators/ExistValidator.php @@ -131,15 +131,13 @@ private function checkTargetRelationExistence($model, $attribute) $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 @@ protected function validateValue($value) } /** - * 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 @@ private function valueExists($targetClass, $query, $value) $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 @@ private function valueExists($targetClass, $query, $value) /** - * 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 @@ private function valueExists($targetClass, $query, $value) 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(); }