Merge pull request #11185 from beerbohmdo/fix/issue-11170

FIX Ensure eagerLoading don't load has_one twice (#11170)
This commit is contained in:
Guy Sartorelli 2024-04-18 13:40:53 +12:00 committed by GitHub
commit cfeb678816
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 74 additions and 11 deletions

View File

@ -1129,18 +1129,18 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
// $parentData represents a record in this DataList // $parentData represents a record in this DataList
$hasOneID = $parentData[$hasOneIDField]; $hasOneID = $parentData[$hasOneIDField];
$fetchedIDs[] = $hasOneID; $fetchedIDs[] = $hasOneID;
$addTo[$hasOneID] = $parentData['ID']; $addTo[$hasOneID][] = $parentData['ID'];
} elseif ($parentData instanceof DataObject) { } elseif ($parentData instanceof DataObject) {
// $parentData represents another has_one record // $parentData represents another has_one record
$hasOneID = $parentData->$hasOneIDField; $hasOneID = $parentData->$hasOneIDField;
$fetchedIDs[] = $hasOneID; $fetchedIDs[] = $hasOneID;
$addTo[$hasOneID] = $parentData; $addTo[$hasOneID][] = $parentData;
} elseif ($parentData instanceof EagerLoadedList) { } elseif ($parentData instanceof EagerLoadedList) {
// $parentData represents a has_many or many_many relation // $parentData represents a has_many or many_many relation
foreach ($parentData->getRows() as $parentRow) { foreach ($parentData->getRows() as $parentRow) {
$hasOneID = $parentRow[$hasOneIDField]; $hasOneID = $parentRow[$hasOneIDField];
$fetchedIDs[] = $hasOneID; $fetchedIDs[] = $hasOneID;
$addTo[$hasOneID] = ['ID' => $parentRow['ID'], 'list' => $parentData]; $addTo[$hasOneID][] = ['ID' => $parentRow['ID'], 'list' => $parentData];
} }
} else { } else {
throw new LogicException("Invalid parent for eager loading $relationType relation $relationName"); throw new LogicException("Invalid parent for eager loading $relationType relation $relationName");
@ -1151,10 +1151,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
// Add each fetched record to the appropriate place // Add each fetched record to the appropriate place
foreach ($fetchedRecords as $fetched) { foreach ($fetchedRecords as $fetched) {
$fetchedID = $fetched->ID; if (isset($addTo[$fetched->ID])) {
$added = false; foreach ($addTo[$fetched->ID] as $addHere) {
foreach ($addTo as $matchID => $addHere) {
if ($matchID === $fetchedID) {
if ($addHere instanceof DataObject) { if ($addHere instanceof DataObject) {
$addHere->setEagerLoadedData($relationName, $fetched); $addHere->setEagerLoadedData($relationName, $fetched);
} elseif (is_array($addHere)) { } elseif (is_array($addHere)) {
@ -1162,11 +1160,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
} else { } else {
$this->eagerLoadedData[$relationChain][$addHere][$relationName] = $fetched; $this->eagerLoadedData[$relationChain][$addHere][$relationName] = $fetched;
} }
$added = true;
break;
} }
} } else {
if (!$added) {
throw new LogicException("Couldn't find parent for record $fetchedID on $relationType relation $relationName"); throw new LogicException("Couldn't find parent for record $fetchedID on $relationType relation $relationName");
} }
} }

View File

@ -1588,4 +1588,72 @@ class DataListEagerLoadingTest extends SapphireTest
} }
} }
} }
public function testHasOneMultipleAppearance(): void
{
$this->provideHasOneObjects();
$this->validateMultipleAppearance(6, EagerLoadObject::get());
$this->validateMultipleAppearance(2, EagerLoadObject::get()->eagerLoad('HasOneEagerLoadObject'));
}
protected function validateMultipleAppearance(int $expected, DataList $list): void
{
try {
$this->startCountingSelectQueries();
/** @var EagerLoadObject $item */
foreach ($list as $item) {
$item->HasOneEagerLoadObject()->Title;
}
$this->assertSame($expected, $this->stopCountingSelectQueries());
} finally {
$this->resetShowQueries();
}
}
protected function provideHasOneObjects(): void
{
$subA = new HasOneEagerLoadObject();
$subA->Title = 'A';
$subA->write();
$subB = new HasOneEagerLoadObject();
$subB->Title = 'B';
$subB->write();
$subC = new HasOneEagerLoadObject();
$subC->Title = 'C';
$subC->write();
$baseA = new EagerLoadObject();
$baseA->Title = 'A';
$baseA->HasOneEagerLoadObjectID = $subA->ID;
$baseA->write();
$baseB = new EagerLoadObject();
$baseB->Title = 'B';
$baseB->HasOneEagerLoadObjectID = $subA->ID;
$baseB->write();
$baseC = new EagerLoadObject();
$baseC->Title = 'C';
$baseC->HasOneEagerLoadObjectID = $subB->ID;
$baseC->write();
$baseD = new EagerLoadObject();
$baseD->Title = 'D';
$baseD->HasOneEagerLoadObjectID = $subC->ID;
$baseD->write();
$baseE = new EagerLoadObject();
$baseE->Title = 'E';
$baseE->HasOneEagerLoadObjectID = $subB->ID;
$baseE->write();
$baseF = new EagerLoadObject();
$baseF->Title = 'F';
$baseF->HasOneEagerLoadObjectID = 0;
$baseF->write();
}
} }