From 1654381a3ae38974daa497bcbdd245c1e70a7563 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 5 Jan 2014 14:52:38 -0500 Subject: [PATCH] support table aliases for ActiveQuery::joinWith(). --- framework/yii/db/ActiveQuery.php | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/framework/yii/db/ActiveQuery.php b/framework/yii/db/ActiveQuery.php index a9a1b19..dc00661 100644 --- a/framework/yii/db/ActiveQuery.php +++ b/framework/yii/db/ActiveQuery.php @@ -326,19 +326,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface } /** - * Returns the table name used by the specified active query. + * Returns the table alias (or table name) that can be used to prefix column names. * @param ActiveQuery $query - * @return string the table name + * @return string the table alias (or table name) enclosed within double curly brackets. */ - private function getQueryTableName($query) + private function getQueryTableAlias($query) { if (empty($query->from)) { /** @var ActiveRecord $modelClass */ $modelClass = $query->modelClass; - return $modelClass::tableName(); + $tableName = $modelClass::tableName(); } else { - return reset($query->from); + $tableName = reset($query->from); } + + if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) { + $tableName = $matches[2]; + } + if (strpos($tableName, '{{') === false) { + $tableName = '{{' . $tableName . '}}'; + } + return $tableName; } /** @@ -364,15 +372,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface return; } - $parentTable = $this->getQueryTableName($parent); - $childTable = $this->getQueryTableName($child); - if (strpos($parentTable, '{{') === false) { - $parentTable = '{{' . $parentTable . '}}'; - } - if (strpos($childTable, '{{') === false) { - $childTable = '{{' . $childTable . '}}'; - } - + $parentTable = $this->getQueryTableAlias($parent); + $childTable = $this->getQueryTableAlias($child); if (!empty($child->link)) { $on = [];