Skip to content

Commit

Permalink
Unique and Exist validators docs updated (#19678)
Browse files Browse the repository at this point in the history
* Unique and Exist validators docs updated

* Correct the line in unique validator as well

Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
Bizley and samdark committed Nov 16, 2022
1 parent 3dc3175 commit 6f047cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 5 additions & 7 deletions validators/ExistValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}


Expand All @@ -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();
}

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6f047cc

Please sign in to comment.