Commit 4889ff9c by Qiang Xue

Merge pull request #2981 from yiisoft/fix-duplicated-joins

removed duplicated joins when using joinWith and via relations
parents 7437e21d 1a3accb6
...@@ -414,6 +414,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface ...@@ -414,6 +414,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface
$this->with($with); $this->with($with);
} }
// remove duplicated joins added by joinWithRelations that may be added
// e.g. when joining a relation and a via relation at the same time
$uniqueJoins = [];
foreach($this->join as $j) {
$uniqueJoins[serialize($j)] = $j;
}
$this->join = array_values($uniqueJoins);
if (!empty($join)) { if (!empty($join)) {
// append explicit join to joinWith() // append explicit join to joinWith()
// https://github.com/yiisoft/yii2/issues/2880 // https://github.com/yiisoft/yii2/issues/2880
......
...@@ -426,7 +426,16 @@ class ActiveRecordTest extends DatabaseTestCase ...@@ -426,7 +426,16 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(1, $customers[1]->id); $this->assertEquals(1, $customers[1]->id);
$this->assertTrue($customers[0]->isRelationPopulated('orders')); $this->assertTrue($customers[0]->isRelationPopulated('orders'));
$this->assertTrue($customers[1]->isRelationPopulated('orders')); $this->assertTrue($customers[1]->isRelationPopulated('orders'));
}
/**
* This query will do the same join twice, ensure duplicated JOIN gets removed
* https://github.com/yiisoft/yii2/pull/2650
*/
public function testJoinWithVia()
{
Order::getDb()->getQueryBuilder()->separator = "\n";
Order::find()->joinWith('itemsInOrder1')->joinWith('items')->all();
} }
public function testInverseOf() public function testInverseOf()
......
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