Skip to content

Commit

Permalink
FIX eagerLoad crash with nonterminal hasOne relation
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonD authored and Mason Dechaineux committed Nov 29, 2024
1 parent 4d78f78 commit 2378751
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ORM/DataList.php
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,14 @@ private function fetchEagerLoadRelations(Query $query): void
$parentIDs = $topLevelIDs;
$parentRelationData = $query;
$chainToDate = [];
$polymorphicEncountered = false;
foreach (explode('.', $relationChain) as $relationName) {
if ($polymorphicEncountered) {
$polymorphicRelation = $chainToDate[count($chainToDate) - 1];
throw new InvalidArgumentException(
"Invalid relation passed to eagerLoad() - $relationChain. Further nested relations are not supported after polymorphic has_one relation $polymorphicRelation."
);
}
/** @var Query|array<DataObject|EagerLoadedList> $parentRelationData */
$chainToDate[] = $relationName;
list(
Expand All @@ -1074,6 +1081,9 @@ private function fetchEagerLoadRelations(Query $query): void
$relationName,
$relationType
);
if ($relationComponent['joinClass']) {
$polymorphicEncountered = true;
}
break;
case 'belongs_to':
list($parentRelationData, $parentIDs) = $this->fetchEagerLoadBelongsTo(
Expand Down Expand Up @@ -1205,6 +1215,10 @@ private function fetchEagerLoadHasOne(
// into the has_one components - DataObject does that for us in getComponent() without any extra
// db calls.

// fetchEagerLoadRelations expects these to be flat arrays if the relation is not polymorphic
if (!$hasOneClassField) {
return [$fetchedRecords[$relationDataClass] ?? [], $fetchedIDs[$relationDataClass] ?? []];
}
return [$fetchedRecords, $fetchedIDs];
}

Expand Down

0 comments on commit 2378751

Please sign in to comment.