Browse Source

Fixes #1402: AR eager loading has problem when asArray() is used together with viaTable().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
b0568612bf
  1. 2
      framework/yii/db/ActiveQueryTrait.php
  2. 6
      framework/yii/db/ActiveRelationTrait.php
  3. 4
      tests/unit/framework/ar/ActiveRecordTestTrait.php

2
framework/yii/db/ActiveQueryTrait.php

@ -146,7 +146,7 @@ trait ActiveQueryTrait
* Finds records corresponding to one or multiple relations and populates them into the primary models. * Finds records corresponding to one or multiple relations and populates them into the primary models.
* @param array $with a list of relations that this query should be performed with. Please * @param array $with a list of relations that this query should be performed with. Please
* refer to [[with()]] for details about specifying this parameter. * refer to [[with()]] for details about specifying this parameter.
* @param ActiveRecord[] $models the primary models * @param array $models the primary models (can be either AR instances or arrays)
*/ */
public function findWith($with, &$models) public function findWith($with, &$models)
{ {

6
framework/yii/db/ActiveRelationTrait.php

@ -236,7 +236,7 @@ trait ActiveRelationTrait
} }
/** /**
* @param ActiveRecord[] $primaryModels * @param array $primaryModels either array of AR instances or arrays
* @return array * @return array
*/ */
private function findPivotRows($primaryModels) private function findPivotRows($primaryModels)
@ -247,6 +247,10 @@ trait ActiveRelationTrait
$this->filterByModels($primaryModels); $this->filterByModels($primaryModels);
/** @var ActiveRecord $primaryModel */ /** @var ActiveRecord $primaryModel */
$primaryModel = reset($primaryModels); $primaryModel = reset($primaryModels);
if (!$primaryModel instanceof ActiveRecordInterface) {
// when primaryModels are array of arrays (asArray case)
$primaryModel = new $this->modelClass;
}
return $this->asArray()->all($primaryModel->getDb()); return $this->asArray()->all($primaryModel->getDb());
} }
} }

4
tests/unit/framework/ar/ActiveRecordTestTrait.php

@ -411,6 +411,10 @@ trait ActiveRecordTestTrait
$this->assertTrue($customer->isRelationPopulated('orders')); $this->assertTrue($customer->isRelationPopulated('orders'));
$this->assertEquals(1, count($customer->orders)); $this->assertEquals(1, count($customer->orders));
$this->assertEquals(1, count($customer->populatedRelations)); $this->assertEquals(1, count($customer->populatedRelations));
// https://github.com/yiisoft/yii2/issues/1402
$orders = $this->callOrderFind()->with('books')->asArray()->all();
$this->assertEquals(3, count($orders));
} }
public function testFindLazyVia() public function testFindLazyVia()

Loading…
Cancel
Save