Skip to content

Commit

Permalink
Refactor addInverseRelations() (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored May 27, 2024
1 parent 2c1010d commit fe61e2d
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,21 @@ private function addInverseRelations(array &$result): void
return;
}

foreach ($result as $i => $relatedModel) {
if ($relatedModel instanceof ActiveRecordInterface) {
if (!isset($inverseRelation)) {
/** @var ActiveQuery $inverseRelation */
$inverseRelation = $relatedModel->relationQuery($this->inverseOf);
}
$relatedModel->populateRelation(
$this->inverseOf,
$inverseRelation->multiple ? [$this->primaryModel] : $this->primaryModel
);
} else {
if (!isset($inverseRelation)) {
/** @var ActiveQuery $inverseRelation */
$inverseRelation = $this->getARInstance()->relationQuery($this->inverseOf);
}
$relatedModel = reset($result);

if ($relatedModel instanceof ActiveRecordInterface) {
$inverseRelation = $relatedModel->relationQuery($this->inverseOf);
$primaryModel = $inverseRelation->getMultiple() ? [$this->primaryModel] : $this->primaryModel;

foreach ($result as $relatedModel) {
$relatedModel->populateRelation($this->inverseOf, $primaryModel);
}
} else {
$inverseRelation = $this->getARInstance()->relationQuery($this->inverseOf);
$primaryModel = $inverseRelation->getMultiple() ? [$this->primaryModel] : $this->primaryModel;

$result[$i][$this->inverseOf] = $inverseRelation->multiple
? [$this->primaryModel] : $this->primaryModel;
foreach ($result as &$relatedModel) {
$relatedModel[$this->inverseOf] = $primaryModel;
}
}
}
Expand Down

0 comments on commit fe61e2d

Please sign in to comment.