Skip to content

Commit

Permalink
Fixes yiisoft#14161, yiisoft#14150: fixed primary table aliasing (inc…
Browse files Browse the repository at this point in the history
…lude case manual set alias) in validators
  • Loading branch information
bscheshirwork authored and samdark committed May 16, 2017
1 parent 2d12e1a commit 921147d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
35 changes: 25 additions & 10 deletions framework/validators/ExistValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;

/**
Expand Down Expand Up @@ -206,6 +207,29 @@ protected function createQuery($targetClass, $condition)
return $query;
}

/**
* Returns conditions with alias
* @param ActiveQuery $query
* @param array $conditions array of condition, keys to be modified
* @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias
* @return array
*/
private function applyTableAlias($query, $conditions, $alias = null)
{
if ($alias === null) {
$alias = array_keys($query->getTablesUsedInFrom())[0];
}
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{$alias}.[[" . preg_replace(
'/^' . preg_quote($alias) . '\.(.*)$/',
"$1",
$columnName) . "]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}
return $prefixedConditions;
}

/**
* Prefix conditions with aliases
*
Expand All @@ -218,15 +242,6 @@ private function prefixConditions($model, $conditions)
$targetModelClass = $this->getTargetClass($model);

/** @var ActiveRecord $targetModelClass */
$query = $targetModelClass::find();
$tableAliases = array_keys($query->getTablesUsedInFrom());
$primaryTableAlias = $tableAliases[0];
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{$primaryTableAlias}.[[{$columnName}]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}

return $prefixedConditions;
return $this->applyTableAlias($targetModelClass::find(), $conditions);
}
}
42 changes: 26 additions & 16 deletions framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,8 @@ private function modelExists($targetClass, $conditions, $model)
// if current $model is in the database already we can't use exists()
if ($query instanceof \yii\db\ActiveQuery) {
// only select primary key to optimize query
$primaryAlias = array_keys($query->getTablesUsedInFrom())[0];
$columns = $targetClass::primaryKey();
foreach($columns as $c => $column) {
$columns[$c] = "{$primaryAlias}.[[$column]]";
}
$query->select($columns);
$columnsCondition = array_flip($targetClass::primaryKey());
$query->select(array_flip($this->applyTableAlias($query, $columnsCondition)));
}
$models = $query->limit(2)->asArray()->all();
$n = count($models);
Expand Down Expand Up @@ -279,6 +275,29 @@ private function addComboNotUniqueError($model, $attribute)
]);
}

/**
* Returns conditions with alias
* @param ActiveQuery $query
* @param array $conditions array of condition, keys to be modified
* @param null|string $alias set empty string for no apply alias. Set null for apply primary table alias
* @return array
*/
private function applyTableAlias($query, $conditions, $alias = null)
{
if ($alias === null) {
$alias = array_keys($query->getTablesUsedInFrom())[0];
}
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{$alias}.[[" . preg_replace(
'/^' . preg_quote($alias) . '\.(.*)$/',
"$1",
$columnName) . "]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}
return $prefixedConditions;
}

/**
* Prefix conditions with aliases
*
Expand All @@ -291,15 +310,6 @@ private function prefixConditions($model, $conditions)
$targetModelClass = $this->getTargetClass($model);

/** @var ActiveRecord $targetModelClass */
$query = $targetModelClass::find();
$tableAliases = array_keys($query->getTablesUsedInFrom());
$primaryTableAlias = $tableAliases[0];
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{$primaryTableAlias}.[[{$columnName}]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}

return $prefixedConditions;
return $this->applyTableAlias($targetModelClass::find(), $conditions);
}
}

0 comments on commit 921147d

Please sign in to comment.