diff --git a/src/ORM/DataList.php b/src/ORM/DataList.php index ec30649d2..3601c4eca 100644 --- a/src/ORM/DataList.php +++ b/src/ORM/DataList.php @@ -1386,7 +1386,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li 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"); diff --git a/src/ORM/EagerLoadedList.php b/src/ORM/EagerLoadedList.php index 605d54215..8bec6c948 100644 --- a/src/ORM/EagerLoadedList.php +++ b/src/ORM/EagerLoadedList.php @@ -968,7 +968,7 @@ class EagerLoadedList extends ViewableData implements Relation, SS_List, Filtera /** * 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 { diff --git a/tests/php/ORM/DataListEagerLoadingTest.php b/tests/php/ORM/DataListEagerLoadingTest.php index 2871ce834..b27bf050f 100644 --- a/tests/php/ORM/DataListEagerLoadingTest.php +++ b/tests/php/ORM/DataListEagerLoadingTest.php @@ -1254,4 +1254,39 @@ class DataListEagerLoadingTest extends SapphireTest $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); + } + } + } }