Browse Source

Merge pull request #10939 from yiisoft/ar-dependency

Avoid some calls to `new $this->modelClass` in ActiveQuery
batch-query-test
Carsten Brandt 9 years ago
parent
commit
722c9625d7
  1. 16
      framework/db/ActiveRelationTrait.php

16
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());
}
}

Loading…
Cancel
Save