Skip to content

Commit

Permalink
#14150: Added {{ and }} to getTablesUsedInFrom() output
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed May 12, 2017
1 parent 5ea1acc commit 8a087c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
11 changes: 6 additions & 5 deletions framework/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ public function alias($alias)

/**
* Returns table names used in [[from]] indexed by aliases.
* Both aliases and names are enclosed into {{ and }}.
* @return string[] table names indexed by aliases
* @throws \yii\base\InvalidConfigException
* @since 2.0.12
Expand All @@ -811,18 +812,18 @@ public function getTablesUsedInFrom()
// Clean up table names and aliases
$cleanedUpTableNames = [];
foreach ($tableNames as $alias => $tableName) {
if (preg_match('~{{(.*?)}}~', $tableName, $matches)) {
$alias = $tableName = $matches[1];
}

if (!is_string($alias)) {
if (preg_match('~^\s*([\'"`\[].*?[\'"`\]]|\w+)(?:(?:\s+(?:as)?\s*)([\'"`\[].*?[\'"`\]]|\w+))?\s*$~iu', $tableName, $matches)) {
if (preg_match('~\s*({{.*?}})\s*~', $tableName, $matches)) {

This comment has been minimized.

Copy link
@cebe

cebe May 12, 2017

Member

this matches {{something}} AS myalias which would ignore the alias.

This comment has been minimized.

Copy link
@samdark

samdark May 12, 2017

Author Member
$alias = $tableName = $matches[1];
} elseif (preg_match('~^\s*([\'"`\[].*?[\'"`\]]|\w+)(?:(?:\s+(?:as)?\s*)([\'"`\[].*?[\'"`\]]|\w+))?\s*$~iu', $tableName, $matches)) {
if (isset($matches[1])) {
if (isset($matches[2])) {
list(, $tableName, $alias) = $matches;
} else {
$tableName = $alias = $matches[1];
}
$alias = '{{' . $alias . '}}';
$tableName = '{{' . $tableName . '}}';
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion framework/validators/ExistValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private function prefixConditions($model, $conditions)
$primaryTableAlias = $tableAliases[0];
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{{{$primaryTableAlias}}}.[[{$columnName}]]";
$prefixedColumn = "{$primaryTableAlias}.[[{$columnName}]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}

Expand Down
4 changes: 2 additions & 2 deletions framework/validators/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private function modelExists($targetClass, $conditions, $model)
$primaryAlias = array_keys($query->getTablesUsedInFrom())[0];
$columns = $targetClass::primaryKey();
foreach($columns as $c => $column) {
$columns[$c] = "{{{$primaryAlias}}}.[[$column]]";
$columns[$c] = "{$primaryAlias}.[[$column]]";
}
$query->select($columns);
}
Expand Down Expand Up @@ -296,7 +296,7 @@ private function prefixConditions($model, $conditions)
$primaryTableAlias = $tableAliases[0];
$prefixedConditions = [];
foreach ($conditions as $columnName => $columnValue) {
$prefixedColumn = "{{{$primaryTableAlias}}}.[[{$columnName}]]";
$prefixedColumn = "{$primaryTableAlias}.[[{$columnName}]]";
$prefixedConditions[$prefixedColumn] = $columnValue;
}

Expand Down
50 changes: 25 additions & 25 deletions tests/framework/db/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,25 @@ public function testGetTableNames_notFilledFrom()
$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
Profile::tableName() => Profile::tableName(),
'{{' . Profile::tableName() . '}}' => '{{' . Profile::tableName() . '}}',
], $tables);
}

public function testGetTableNames_isFromArray()
{
$query = new ActiveQuery(null);
$query->from = [
'prf' => 'profile',
'usr' => 'user',
'a b' => 'c d',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{a b}}' => '{{c d}}',
];

$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
'prf' => 'profile',
'usr' => 'user',
'a b' => 'c d',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{a b}}' => '{{c d}}',
], $tables);
}

Expand All @@ -269,11 +269,11 @@ public function testGetTableNames_isFromString()
$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
'prf' => 'profile',
'usr' => 'user',
'order' => 'order',
'customer' => 'customer',
'c d' => 'a b',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{order}}' => '{{order}}',
'{{customer}}' => '{{customer}}',
'{{c d}}' => '{{a b}}',
], $tables);
}

Expand All @@ -294,25 +294,25 @@ public function testGetTablesAlias_notFilledFrom()
$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
Profile::tableName() => Profile::tableName(),
'{{' . Profile::tableName() . '}}' => '{{' . Profile::tableName() . '}}',
], $tables);
}

public function testGetTablesAlias_isFromArray()
{
$query = new ActiveQuery(null);
$query->from = [
'prf' => 'profile',
'usr' => 'user',
'a b' => 'c d',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{a b}}' => '{{c d}}',
];

$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
'prf' => 'profile',
'usr' => 'user',
'a b' => 'c d',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{a b}}' => '{{c d}}',
], $tables);
}

Expand All @@ -324,11 +324,11 @@ public function testGetTablesAlias_isFromString()
$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
'prf' => 'profile',
'usr' => 'user',
'srv' => 'service',
'order' => 'order',
'c d' => 'a b',
'{{prf}}' => '{{profile}}',
'{{usr}}' => '{{user}}',
'{{srv}}' => '{{service}}',
'{{order}}' => '{{order}}',
'{{c d}}' => '{{a b}}',
], $tables);
}

Expand All @@ -353,7 +353,7 @@ public function testGetTableAliasFromPrefixedTableName()
$tables = $query->getTablesUsedInFrom();

$this->assertEquals([
'%order_item' => '%order_item',
'{{%order_item}}' => '{{%order_item}}',
], $tables);
}
}

0 comments on commit 8a087c8

Please sign in to comment.