Merge branch '5.1' into 5

This commit is contained in:
github-actions 2024-02-14 22:58:30 +00:00
commit 7e30e38ae6
3 changed files with 37 additions and 2 deletions

View File

@ -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");

View File

@ -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
{

View File

@ -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);
}
}
}
}