Skip to content

Commit

Permalink
Fixes yiisoft#1791: joinWithRelation using table alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jan 5, 2014
1 parent a4f8e82 commit 795a09c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
30 changes: 19 additions & 11 deletions framework/yii/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,11 @@ private function getJoinType($joinType, $name)
}

/**
* Returns the table alias (or table name) that can be used to prefix column names.
* Returns the table name and the table alias for [[modelClass]].
* @param ActiveQuery $query
* @return string the table alias (or table name) enclosed within double curly brackets.
* @return array the table name and the table alias.
*/
private function getQueryTableAlias($query)
private function getQueryTableName($query)
{
if (empty($query->from)) {
/** @var ActiveRecord $modelClass */
Expand All @@ -341,12 +341,12 @@ private function getQueryTableAlias($query)
}

if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) {
$tableName = $matches[2];
}
if (strpos($tableName, '{{') === false) {
$tableName = '{{' . $tableName . '}}';
$alias = $matches[2];
} else {
$alias = $tableName;
}
return $tableName;

return [$tableName, $alias];
}

/**
Expand All @@ -372,13 +372,21 @@ private function joinWithRelation($parent, $child, $joinType)
return;
}

$parentTable = $this->getQueryTableAlias($parent);
$childTable = $this->getQueryTableAlias($child);
list ($parentTable, $parentAlias) = $this->getQueryTableName($parent);
list ($childTable, $childAlias) = $this->getQueryTableName($child);

if (!empty($child->link)) {

if (strpos($parentAlias, '{{') === false) {
$parentAlias = '{{' . $parentAlias . '}}';
}
if (strpos($childAlias, '{{') === false) {
$childAlias = '{{' . $childAlias . '}}';
}

$on = [];
foreach ($child->link as $childColumn => $parentColumn) {
$on[] = "$parentTable.[[$parentColumn]] = $childTable.[[$childColumn]]";
$on[] = "$parentAlias.[[$parentColumn]] = $childAlias.[[$childColumn]]";
}
$on = implode(' AND ', $on);
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/framework/db/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,19 @@ public function testJoinWith()
$this->assertEquals(3, count($orders[0]->items));
$this->assertTrue($orders[0]->items[0]->isRelationPopulated('category'));
$this->assertEquals(2, $orders[0]->items[0]->category->id);

// join with table alias
$orders = Order::find()->joinWith([
'customer' => function ($q) {
$q->from('tbl_customer c');
}
])->orderBy('c.id DESC, tbl_order.id')->all();
$this->assertEquals(3, count($orders));
$this->assertEquals(2, $orders[0]->id);
$this->assertEquals(3, $orders[1]->id);
$this->assertEquals(1, $orders[2]->id);
$this->assertTrue($orders[0]->isRelationPopulated('customer'));
$this->assertTrue($orders[1]->isRelationPopulated('customer'));
$this->assertTrue($orders[2]->isRelationPopulated('customer'));
}
}

0 comments on commit 795a09c

Please sign in to comment.