Browse Source

Applied disambiguation to ExistValidator as well

tags/2.0.12
Alexander Makarov 8 years ago
parent
commit
99a0289221
No known key found for this signature in database
GPG Key ID: 3617B79C6A325E4A
  1. 2
      framework/CHANGELOG.md
  2. 37
      framework/validators/ExistValidator.php

2
framework/CHANGELOG.md

@ -5,7 +5,7 @@ Yii Framework 2 Change Log
--------------------------
- Bug #13694: `yii\widgets\Pjax` now sends `X-Pjax-Url` header with response to fix redirect (wleona3, Faryshta)
- Bug #13842: Fixed ambiguous table SQL error while using unique validator (vladis84, samdark)
- Bug #13842: Fixed ambiguous table SQL error while using `yii\validators\UniqueValidator` and `yii\validators\UniqueValidator` (vladis84, samdark)
- Bug #14012: `yii\db\pgsql\Schema::findViewNames()` was skipping materialized views (insolita)
- Bug #13362: Fixed return value of `yii\caching\MemCache::setValues()` (masterklavi)
- Enh #13963: Added tests for yii\behaviors\TimestampBehavior (vladis84)

37
framework/validators/ExistValidator.php

@ -9,6 +9,8 @@ namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\db\ActiveRecord;
/**
* ExistValidator validates that the attribute value exists in a table.
@ -137,14 +139,41 @@ class ExistValidator extends Validator
if ($this->allowArray) {
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.');
}
$params = [];
$conditions = [];
foreach ($targetAttribute as $k => $v) {
$params[$v] = is_int($k) ? $model->$v : $model->$k;
$conditions[$v] = is_int($k) ? $model->$v : $model->$k;
}
} else {
$params = [$targetAttribute => $model->$attribute];
$conditions = [$targetAttribute => $model->$attribute];
}
return $params;
if (!$model instanceof ActiveRecord) {
return $conditions;
}
// Add table prefix for column
$targetClass = $this->getTargetClass($model);
/** @var ActiveRecord $targetClass */
$query = $targetClass::find();
$tableAliases = $query->getFromAliases();
$primaryTableAlias = $tableAliases[0];
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{$primaryTableAlias}.{$columnName}";
$prefixedConditions[$prefixedColumn] = $columnValue;
}
return $prefixedConditions;
}
/**
* @param Model $model the data model to be validated
* @return string Target class name
*/
private function getTargetClass($model)
{
return $this->targetClass === null ? get_class($model) : $this->targetClass;
}
/**

Loading…
Cancel
Save