Skip to content

Commit

Permalink
Merge pull request #16583 from craftcms/bugfix/16576-invalid-numeric-…
Browse files Browse the repository at this point in the history
…value-eager-loading-owners

filter out mappings that have empty targets
  • Loading branch information
brandonkelly authored Jan 29, 2025
2 parents e8f8239 + 5edc858 commit 9505002
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft CMS 5

## Unreleased

- Fixed an error that could occur when eager-loading nested elements’ owners, if any of the queried elements didn’t have an owner ID. ([#16570](https://github.com/craftcms/cms/issues/16570), [#16572](https://github.com/craftcms/cms/issues/16572), [#16576](https://github.com/craftcms/cms/issues/16576))

## 5.6.2 - 2024-01-28

- The Login page now displays the Login Page Logo above the login form, rather than within the header. ([#16564](https://github.com/craftcms/cms/pull/16564))
Expand Down
13 changes: 8 additions & 5 deletions src/base/NestedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@ public static function eagerLoadingMap(array $sourceElements, string $handle): a

return [
'elementType' => $ownerType,
'map' => array_map(fn(NestedElementInterface $element) => [
'source' => $element->id,
'target' => match ($handle) {
'map' => array_filter(array_map(function(NestedElementInterface $element) use ($handle) {
$ownerId = match ($handle) {
'owner' => $element->getOwnerId(),
'primaryOwner' => $element->getPrimaryOwnerId(),
},
], $sourceElements),
};
return $ownerId ? [
'source' => $element->id,
'target' => $ownerId,
] : null;
}, $sourceElements)),
'criteria' => [
'status' => null,
],
Expand Down
6 changes: 4 additions & 2 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3235,8 +3235,10 @@ private function _eagerLoadElementsInternal(string $elementType, array $elements
$targetElementIdsBySourceIds = [];

foreach ($map['map'] as $mapping) {
$uniqueTargetElementIds[$mapping['target']] = true;
$targetElementIdsBySourceIds[$mapping['source']][$mapping['target']] = true;
if (!empty($mapping['target'])) {
$uniqueTargetElementIds[$mapping['target']] = true;
$targetElementIdsBySourceIds[$mapping['source']][$mapping['target']] = true;
}
}

// Get the target elements
Expand Down

0 comments on commit 9505002

Please sign in to comment.