From 762b9207eafae9b06c41d8aef5026e48b7f7f5a8 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 23 Feb 2016 13:21:01 +0100 Subject: [PATCH] Avoid some calls to `new $this->modelClass` in ActiveQuery this fixes some cases of the problem described in #8639 and is fully BC. Fixing other places may need BC breaks. --- framework/db/ActiveRelationTrait.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/framework/db/ActiveRelationTrait.php b/framework/db/ActiveRelationTrait.php index ac8c883..22f971f 100644 --- a/framework/db/ActiveRelationTrait.php +++ b/framework/db/ActiveRelationTrait.php @@ -160,20 +160,26 @@ trait ActiveRelationTrait return $related; } - $inverseRelation = (new $this->modelClass)->getRelation($this->inverseOf); - if ($this->multiple) { foreach ($related as $i => $relatedModel) { if ($relatedModel instanceof ActiveRecordInterface) { + if (!isset($inverseRelation)) { + $inverseRelation = $relatedModel->getRelation($this->inverseOf); + } $relatedModel->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$model] : $model); } else { + if (!isset($inverseRelation)) { + $inverseRelation = (new $this->modelClass)->getRelation($this->inverseOf); + } $related[$i][$this->inverseOf] = $inverseRelation->multiple ? [$model] : $model; } } } else { if ($related instanceof ActiveRecordInterface) { + $inverseRelation = $related->getRelation($this->inverseOf); $related->populateRelation($this->inverseOf, $inverseRelation->multiple ? [$model] : $model); } else { + $inverseRelation = (new $this->modelClass)->getRelation($this->inverseOf); $related[$this->inverseOf] = $inverseRelation->multiple ? [$model] : $model; } } @@ -475,7 +481,7 @@ trait ActiveRelationTrait } /** - * @param ActiveRecord|array $model + * @param ActiveRecordInterface|array $model * @param array $attributes * @return string */ @@ -519,9 +525,9 @@ trait ActiveRelationTrait $primaryModel = reset($primaryModels); if (!$primaryModel instanceof ActiveRecordInterface) { // when primaryModels are array of arrays (asArray case) - $primaryModel = new $this->modelClass; + $primaryModel = $this->modelClass; } - return $this->asArray()->all($primaryModel->getDb()); + return $this->asArray()->all($primaryModel::getDb()); } }