diff --git a/validators/ExistValidator.php b/validators/ExistValidator.php index be02f1a41..faea7b55d 100644 --- a/validators/ExistValidator.php +++ b/validators/ExistValidator.php @@ -162,7 +162,7 @@ private function checkTargetAttributeExistence($model, $attribute) } $params = $this->prepareConditions($targetAttribute, $model, $attribute); - $conditions = [$this->targetAttributeJunction == 'or' ? 'or' : 'and']; + $conditions = [$this->targetAttributeJunction === 'or' ? 'or' : 'and']; if (!$this->allowArray) { foreach ($params as $key => $value) { @@ -264,17 +264,14 @@ protected function validateValue($value) private function valueExists($targetClass, $query, $value) { $db = $targetClass::getDb(); - $exists = false; if ($this->forceMasterDb && method_exists($db, 'useMaster')) { - $exists = $db->useMaster(function () use ($query, $value) { + return $db->useMaster(function () use ($query, $value) { return $this->queryValueExists($query, $value); }); - } else { - $exists = $this->queryValueExists($query, $value); } - return $exists; + return $this->queryValueExists($query, $value); } @@ -290,6 +287,7 @@ private function queryValueExists($query, $value) if (is_array($value)) { return $query->count("DISTINCT [[$this->targetAttribute]]") == count(array_unique($value)); } + return $query->exists(); } @@ -328,7 +326,7 @@ private function applyTableAlias($query, $conditions, $alias = null) foreach ($conditions as $columnName => $columnValue) { if (strpos($columnName, '(') === false) { $prefixedColumn = "{$alias}.[[" . preg_replace( - '/^' . preg_quote($alias) . '\.(.*)$/', + '/^' . preg_quote($alias, '/') . '\.(.*)$/', '$1', $columnName) . ']]'; } else { diff --git a/validators/UniqueValidator.php b/validators/UniqueValidator.php index 56bb0fe48..74edcfd66 100644 --- a/validators/UniqueValidator.php +++ b/validators/UniqueValidator.php @@ -189,8 +189,8 @@ private function modelExists($targetClass, $conditions, $model) /** @var ActiveRecordInterface|\yii\base\BaseObject $targetClass $query */ $query = $this->prepareQuery($targetClass, $conditions); - if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) { - // if current $model isn't in the database yet then it's OK just to call exists() + if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model::className() !== $targetClass::className()) { + // if current $model isn't in the database yet, then it's OK just to call exists() // also there's no need to run check based on primary keys, when $targetClass is not the same as $model's class $exists = $query->exists(); } else { @@ -328,7 +328,7 @@ private function applyTableAlias($query, $conditions, $alias = null) $prefixedConditions = []; foreach ($conditions as $columnName => $columnValue) { if (strpos($columnName, '(') === false) { - $columnName = preg_replace('/^' . preg_quote($alias) . '\.(.*)$/', '$1', $columnName); + $columnName = preg_replace('/^' . preg_quote($alias, '/') . '\.(.*)$/', '$1', $columnName); if (strncmp($columnName, '[[', 2) === 0) { $prefixedColumn = "{$alias}.{$columnName}"; } else {