diff --git a/framework/yii/db/ActiveQueryTrait.php b/framework/yii/db/ActiveQueryTrait.php index 7aae6e6..c8ba773 100644 --- a/framework/yii/db/ActiveQueryTrait.php +++ b/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. * @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) { diff --git a/framework/yii/db/ActiveRelationTrait.php b/framework/yii/db/ActiveRelationTrait.php index 832bb62..f7a3e14 100644 --- a/framework/yii/db/ActiveRelationTrait.php +++ b/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 */ 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()); } } diff --git a/tests/unit/framework/ar/ActiveRecordTestTrait.php b/tests/unit/framework/ar/ActiveRecordTestTrait.php index 75e2120..bf1e09a 100644 --- a/tests/unit/framework/ar/ActiveRecordTestTrait.php +++ b/tests/unit/framework/ar/ActiveRecordTestTrait.php @@ -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() @@ -746,4 +750,4 @@ trait ActiveRecordTestTrait $customers = $this->callCustomerFind()->where(['status' => false])->all(); $this->assertEquals(1, count($customers)); } -} \ No newline at end of file +}