Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 27, 2024
1 parent fd76c5b commit 924316d
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,14 @@ private function populateInverseRelation(
if ($this->multiple) {
foreach ($primaryModels as $primaryModel) {
$models = $primaryModel->relation($primaryName);
$this->populateRelationFromBuckets($models, $name, $link, $buckets);
$models = $this->populateRelationFromBuckets($models, $name, $link, $buckets);
$primaryModel->populateRelation($primaryName, $models);

Check warning on line 388 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L384-L388

Added lines #L384 - L388 were not covered by tests
}
} else {
foreach ($primaryModels as $primaryModel) {
$models = [$primaryModel->relation($primaryName)];
$this->populateRelationFromBuckets($models, $name, $link, $buckets);
$models = $this->populateRelationFromBuckets($models, $name, $link, $buckets);
$primaryModel->populateRelation($primaryName, $models[0]);

Check warning on line 394 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L391-L394

Added lines #L391 - L394 were not covered by tests
}
}
} else {
Expand All @@ -410,44 +412,31 @@ private function populateInverseRelation(
foreach ($primaryModels as $primaryModel) {
$models = $primaryModel->relation($primaryName);

Check warning on line 413 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L412-L413

Added lines #L412 - L413 were not covered by tests

$model = reset($models);
if ($model instanceof ActiveRecordInterface) {
foreach ($models as $model) {
$model->populateRelation($name, $primaryModel);
}
} else {
foreach ($models as &$model) {
$model[$name] = $primaryModel;
}
unset($model);

$primaryModel->populateRelation($primaryName, $models);
foreach ($models as &$model) {
$model[$name] = $primaryModel;

Check warning on line 416 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L415-L416

Added lines #L415 - L416 were not covered by tests
}
unset($model);

Check warning on line 418 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L418

Added line #L418 was not covered by tests

$primaryModel->populateRelation($primaryName, $models);

Check warning on line 420 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L420

Added line #L420 was not covered by tests
}
} else {
foreach ($primaryModels as $i => $primaryModel) {
foreach ($primaryModel[$primaryName] as $j => $model) {
if ($model instanceof ActiveRecordInterface) {
$model->populateRelation($name, $primaryModel);
} else {
$primaryModels[$i][$primaryName][$j][$name] = $primaryModel;
}
foreach ($primaryModels as &$primaryModel) {
foreach ($primaryModel[$primaryName] as &$model) {
$model[$name] = $primaryModel;
}
unset($model);
}
}
} else {
if ($primaryModel instanceof ActiveRecordInterface) {
foreach ($primaryModels as $primaryModel) {
$model = $primaryModel->relation($primaryName);
if ($model instanceof ActiveRecordInterface) {
$model->populateRelation($name, $primaryModel);
}
$model[$name] = $primaryModel;
$primaryModel->populateRelation($primaryName, $model);

Check warning on line 435 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L431-L435

Added lines #L431 - L435 were not covered by tests
}
} else {
foreach ($primaryModels as $i => $primaryModel) {
if (!empty($primaryModel[$primaryName])) {
$primaryModels[$i][$primaryName][$name] = $primaryModel;
}
foreach ($primaryModels as &$primaryModel) {
$primaryModel[$primaryName][$name] = $primaryModel;

Check warning on line 439 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L438-L439

Added lines #L438 - L439 were not covered by tests
}
}
}
Expand Down

0 comments on commit 924316d

Please sign in to comment.