Skip to content

Commit

Permalink
FIX Add eagerloaded data to ALL relevant lists (#11139)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Feb 14, 2024
1 parent c493485 commit d33332c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ORM/DataList.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ private function addEagerLoadedDataToParent(
if ($parentData->hasID($parentID)) {
$parentData->addEagerLoadedData($relationName, $parentID, $eagerLoadedData);
$added = true;
break;
// can't break here, because the parent might be in multiple relation lists
}
} else {
throw new LogicException("Invalid parent for eager loading $relationType relation $relationName");
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/EagerLoadedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ private function setDataObjectEagerLoadedData(int $id, DataObject $item): void

/**
* Gets the final rows for this list after applying all transformations.
* Currently only limit is applied lazily, but others could be done this was as well.
* Currently only limit and sort are applied lazily, but filter could be done this was as well in the future.
*/
private function getFinalisedRows(): array
{
Expand Down
35 changes: 35 additions & 0 deletions tests/php/ORM/DataListEagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,4 +1254,39 @@ public function testLastHasEagerloadedRelation()
$obj = EagerLoadObject::get()->eagerLoad('HasManyEagerLoadObjects')->last();
$this->assertInstanceOf(EagerLoadedList::class, $obj->HasManyEagerLoadObjects());
}

/**
* Tests that if the same record exists in multiple relations, its data is
* eagerloaded without extra unnecessary queries.
*/
public function testEagerLoadingSharedRelations()
{
$record1 = EagerLoadObject::create(['Title' => 'My obj1']);
$record1->write();
$record2 = EagerLoadObject::create(['Title' => 'My obj2']);
$record2->write();
$manyMany = ManyManyEagerLoadObject::create(['Title' => 'My manymany']);
$manyMany->write();
$record1->ManyManyEagerLoadObjects()->add($manyMany);
$record2->ManyManyEagerLoadObjects()->add($manyMany);
$subManyMany = ManyManySubEagerLoadObject::create(['Title' => 'My submanymany']);
$subManyMany->write();
$manyMany->ManyManySubEagerLoadObjects()->add($subManyMany);

$eagerLoadQuery = EagerLoadObject::get()
->filter(['ID' => [$record1->ID, $record2->ID]])
->eagerLoad('ManyManyEagerLoadObjects.ManyManySubEagerLoadObjects');
$loop1Count = 0;
$loop2Count = 0;
foreach ($eagerLoadQuery as $record) {
$loop1Count++;
$eagerLoaded1 = $record->ManyManyEagerLoadObjects();
$this->assertInstanceOf(EagerLoadedList::class, $eagerLoaded1);
foreach ($eagerLoaded1 as $manyManyRecord) {
$loop2Count++;
$eagerLoaded2 = $manyManyRecord->ManyManySubEagerLoadObjects();
$this->assertInstanceOf(EagerLoadedList::class, $eagerLoaded2);
}
}
}
}

0 comments on commit d33332c

Please sign in to comment.