Browse Source

removed duplicated joins when using joinWith and via relations

fixes #2650
tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
1a3accb6cb
  1. 8
      framework/db/ActiveQuery.php
  2. 9
      tests/unit/framework/db/ActiveRecordTest.php

8
framework/db/ActiveQuery.php

@ -414,6 +414,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface
$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)) {
// append explicit join to joinWith()
// https://github.com/yiisoft/yii2/issues/2880

9
tests/unit/framework/db/ActiveRecordTest.php

@ -426,7 +426,16 @@ class ActiveRecordTest extends DatabaseTestCase
$this->assertEquals(1, $customers[1]->id);
$this->assertTrue($customers[0]->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()

Loading…
Cancel
Save