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
* 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
* 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)
{
......
......@@ -236,7 +236,7 @@ trait ActiveRelationTrait
}
/**
* @param ActiveRecord[] $primaryModels
* @param array $primaryModels either array of AR instances or arrays
* @return array
*/
private function findPivotRows($primaryModels)
......@@ -247,6 +247,10 @@ trait ActiveRelationTrait
$this->filterByModels($primaryModels);
/** @var ActiveRecord $primaryModel */
$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());
}
}
......@@ -411,6 +411,10 @@ trait ActiveRecordTestTrait
$this->assertTrue($customer->isRelationPopulated('orders'));
$this->assertEquals(1, count($customer->orders));
$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()
......
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