Commit b0568612 by Qiang Xue

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

parent ef51c7b2
...@@ -146,7 +146,7 @@ trait ActiveQueryTrait ...@@ -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)
{ {
......
...@@ -236,7 +236,7 @@ trait ActiveRelationTrait ...@@ -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 ...@@ -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());
} }
} }
...@@ -411,6 +411,10 @@ trait ActiveRecordTestTrait ...@@ -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()
......
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