FIX Ensure DataList::eagerLoad() returns a clone (#10899)

This commit is contained in:
Guy Sartorelli 2023-08-04 12:04:20 +12:00 committed by GitHub
parent 3628cec1f3
commit c9f48089c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1478,9 +1478,10 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*/
public function eagerLoad(...$relationChains): static
{
$list = clone $this;
foreach ($relationChains as $relationChain) {
// Don't add any relations we've added before
if (array_key_exists($relationChain, $this->eagerLoadAllRelations)) {
if (array_key_exists($relationChain, $list->eagerLoadAllRelations)) {
continue;
}
$parts = explode('.', $relationChain);
@ -1496,15 +1497,15 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
foreach ($parts as $part) {
$usedParts[] = $part;
$item = implode('.', $usedParts);
unset($this->eagerLoadRelationChains[$item]);
unset($list->eagerLoadRelationChains[$item]);
// Keep track of what we've seen before so we don't accidentally add a level 1 relation
// (e.g. "Players") to the chains list when we already have it as part of a longer chain
// (e.g. "Players.Teams")
$this->eagerLoadAllRelations[$item] = $item;
$list->eagerLoadAllRelations[$item] = $item;
}
$this->eagerLoadRelationChains[$relationChain] = $relationChain;
$list->eagerLoadRelationChains[$relationChain] = $relationChain;
}
return $this;
return $list;
}
/**