Skip to content

Commit 6f047cc

Browse files
Bizleysamdark
Bizley
andauthored
Unique and Exist validators docs updated (#19678)
* Unique and Exist validators docs updated * Correct the line in unique validator as well Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
1 parent 3dc3175 commit 6f047cc

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

validators/ExistValidator.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private function checkTargetAttributeExistence($model, $attribute)
162162
}
163163

164164
$params = $this->prepareConditions($targetAttribute, $model, $attribute);
165-
$conditions = [$this->targetAttributeJunction == 'or' ? 'or' : 'and'];
165+
$conditions = [$this->targetAttributeJunction === 'or' ? 'or' : 'and'];
166166

167167
if (!$this->allowArray) {
168168
foreach ($params as $key => $value) {
@@ -264,17 +264,14 @@ protected function validateValue($value)
264264
private function valueExists($targetClass, $query, $value)
265265
{
266266
$db = $targetClass::getDb();
267-
$exists = false;
268267

269268
if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
270-
$exists = $db->useMaster(function () use ($query, $value) {
269+
return $db->useMaster(function () use ($query, $value) {
271270
return $this->queryValueExists($query, $value);
272271
});
273-
} else {
274-
$exists = $this->queryValueExists($query, $value);
275272
}
276273

277-
return $exists;
274+
return $this->queryValueExists($query, $value);
278275
}
279276

280277

@@ -290,6 +287,7 @@ private function queryValueExists($query, $value)
290287
if (is_array($value)) {
291288
return $query->count("DISTINCT [[$this->targetAttribute]]") == count(array_unique($value));
292289
}
290+
293291
return $query->exists();
294292
}
295293

@@ -328,7 +326,7 @@ private function applyTableAlias($query, $conditions, $alias = null)
328326
foreach ($conditions as $columnName => $columnValue) {
329327
if (strpos($columnName, '(') === false) {
330328
$prefixedColumn = "{$alias}.[[" . preg_replace(
331-
'/^' . preg_quote($alias) . '\.(.*)$/',
329+
'/^' . preg_quote($alias, '/') . '\.(.*)$/',
332330
'$1',
333331
$columnName) . ']]';
334332
} else {

validators/UniqueValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ private function modelExists($targetClass, $conditions, $model)
189189
/** @var ActiveRecordInterface|\yii\base\BaseObject $targetClass $query */
190190
$query = $this->prepareQuery($targetClass, $conditions);
191191

192-
if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) {
193-
// if current $model isn't in the database yet then it's OK just to call exists()
192+
if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model::className() !== $targetClass::className()) {
193+
// if current $model isn't in the database yet, then it's OK just to call exists()
194194
// also there's no need to run check based on primary keys, when $targetClass is not the same as $model's class
195195
$exists = $query->exists();
196196
} else {
@@ -328,7 +328,7 @@ private function applyTableAlias($query, $conditions, $alias = null)
328328
$prefixedConditions = [];
329329
foreach ($conditions as $columnName => $columnValue) {
330330
if (strpos($columnName, '(') === false) {
331-
$columnName = preg_replace('/^' . preg_quote($alias) . '\.(.*)$/', '$1', $columnName);
331+
$columnName = preg_replace('/^' . preg_quote($alias, '/') . '\.(.*)$/', '$1', $columnName);
332332
if (strncmp($columnName, '[[', 2) === 0) {
333333
$prefixedColumn = "{$alias}.{$columnName}";
334334
} else {

0 commit comments

Comments
 (0)