Commit 1654381a by Qiang Xue

support table aliases for ActiveQuery::joinWith().

parent e050aeac
...@@ -326,19 +326,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -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 * @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)) { if (empty($query->from)) {
/** @var ActiveRecord $modelClass */ /** @var ActiveRecord $modelClass */
$modelClass = $query->modelClass; $modelClass = $query->modelClass;
return $modelClass::tableName(); $tableName = $modelClass::tableName();
} else { } 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 ...@@ -364,15 +372,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
return; return;
} }
$parentTable = $this->getQueryTableName($parent); $parentTable = $this->getQueryTableAlias($parent);
$childTable = $this->getQueryTableName($child); $childTable = $this->getQueryTableAlias($child);
if (strpos($parentTable, '{{') === false) {
$parentTable = '{{' . $parentTable . '}}';
}
if (strpos($childTable, '{{') === false) {
$childTable = '{{' . $childTable . '}}';
}
if (!empty($child->link)) { if (!empty($child->link)) {
$on = []; $on = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment