mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-12 20:21:19 +08:00
Fixed Query not to dig inside Expression
This commit is contained in:
@@ -8,7 +8,7 @@ Yii Framework 2 Change Log
|
||||
- Bug #14157: Add support for loading default value `CURRENT_TIMESTAMP` of MySQL `datetime` field (rossoneri)
|
||||
- Bug #15046: Throw an `yii\web\HeadersAlreadySentException` if headers were sent before web response (dmirogin)
|
||||
- Enh #3087: Added `yii\helpers\BaseHtml::error()` "errorMethod" option to be able to customize errors display (yanggs07, developeruz)
|
||||
- Bug #15355: Fixed `yii\db\Query::from()` does not work with `yii\db\Expression` (vladis84)
|
||||
- Bug #15355: Fixed `yii\db\Query::from()` does not work with `yii\db\Expression` (vladis84, silverfire, samdark)
|
||||
- Bug #8983: Only truncate the original log file for rotation (matthewyang, developeruz)
|
||||
- Bug #14276: Fixed I18N format with dotted parameters (developeruz)
|
||||
- Bug #14484: Fixed `yii\validators\UniqueValidator` for target classes with a default scope (laszlovl, developeruz)
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace yii\db;
|
||||
use Yii;
|
||||
use yii\base\Component;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\base\InvalidParamException;
|
||||
|
||||
/**
|
||||
* Query represents a SELECT SQL statement in a way that is independent of DBMS.
|
||||
@@ -481,60 +482,57 @@ class Query extends Component implements QueryInterface
|
||||
// Clean up table names and aliases
|
||||
$cleanedUpTableNames = [];
|
||||
foreach ($tableNames as $alias => $tableName) {
|
||||
$tableNameString = null;
|
||||
if (is_string($tableName)) {
|
||||
$tableNameString = $tableName;
|
||||
} elseif ($tableName instanceof Expression) {
|
||||
$tableNameString = $tableName->expression;
|
||||
}
|
||||
|
||||
if (!is_string($alias) && $tableNameString !== null) {
|
||||
if (is_string($tableName) && !is_string($alias)) {
|
||||
$pattern = <<<PATTERN
|
||||
~
|
||||
^
|
||||
\s*
|
||||
(
|
||||
(?:['"`\[]|{{)
|
||||
(?:['"`\[]|{{)
|
||||
.*?
|
||||
(?:['"`\]]|}})
|
||||
|
|
||||
\(.*?\)
|
||||
|
|
||||
.*?
|
||||
)
|
||||
(?:
|
||||
(?:
|
||||
\s+
|
||||
(?:as)?
|
||||
\s*
|
||||
)
|
||||
(
|
||||
(?:['"`\[]|{{)
|
||||
.*?
|
||||
(?:['"`\]]|}})
|
||||
|
|
||||
\(.*?\)
|
||||
|
|
||||
.*?
|
||||
)
|
||||
(?:
|
||||
(?:
|
||||
\s+
|
||||
(?:as)?
|
||||
\s*
|
||||
)
|
||||
(
|
||||
(?:['"`\[]|{{)
|
||||
.*?
|
||||
(?:['"`\]]|}})
|
||||
|
|
||||
.*?
|
||||
)
|
||||
)?
|
||||
\s*
|
||||
$
|
||||
~iux
|
||||
PATTERN;
|
||||
if (preg_match($pattern, $tableNameString, $matches)) {
|
||||
if (preg_match($pattern, $tableName, $matches)) {
|
||||
if (isset($matches[2])) {
|
||||
list(, $tableNameString, $alias) = $matches;
|
||||
list(, $tableName, $alias) = $matches;
|
||||
} else {
|
||||
$tableNameString = $alias = $matches[1];
|
||||
$tableName = $alias = $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($tableName instanceof Expression) {
|
||||
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableNameString;
|
||||
if (!is_string($alias)) {
|
||||
throw new InvalidParamException('To use Expression in from() method, pass it in array format with alias.');
|
||||
}
|
||||
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableName;
|
||||
} elseif ($tableName instanceof self) {
|
||||
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $tableName;
|
||||
} else {
|
||||
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $this->ensureNameQuoted($tableNameString);
|
||||
$cleanedUpTableNames[$this->ensureNameQuoted($alias)] = $this->ensureNameQuoted($tableName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,13 +129,14 @@ trait GetTablesAliasTestTrait
|
||||
public function testGetTableNames_isFromAliasedExpression()
|
||||
{
|
||||
$query = $this->createQuery();
|
||||
$query->from = new \yii\db\Expression('(SELECT id FROM user) x');
|
||||
$expression = new \yii\db\Expression('(SELECT id FROM user)');
|
||||
$query->from = $expression;
|
||||
|
||||
$this->expectException('yii\base\InvalidParamException');
|
||||
$this->expectExceptionMessage('To use Expression in from() method, pass it in array format with alias.');
|
||||
$tables = $query->getTablesUsedInFrom();
|
||||
|
||||
$this->assertEquals([
|
||||
'{{x}}' => '(SELECT id FROM user)',
|
||||
], $tables);
|
||||
$this->assertEquals(['{{x}}' => $expression], $tables);
|
||||
}
|
||||
|
||||
public function testGetTableNames_isFromAliasedArrayWithExpression()
|
||||
|
||||
Reference in New Issue
Block a user