Skip to content

Commit

Permalink
Merge pull request #16428 from craftcms/bugfix/16191-lazy-eager-loadi…
Browse files Browse the repository at this point in the history
…ng-and-multi-instance-relation-field

When normalising relation field value, respect the empty array
  • Loading branch information
brandonkelly authored Jan 14, 2025
2 parents bcf9a50 + 63cb6ee commit 38dc728
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed a bug where field layout elements’ action menus could have an empty action group.
- Fixed a bug where Single section entries could be duplicated after running the `entry-types/merge` command. ([#16394](https://github.com/craftcms/cms/issues/16394))
- Fixed a styling bug with the system message modal. ([#16410](https://github.com/craftcms/cms/issues/16410))
- Fixed a bug where relational fields could eager-load elements from a different instance of the same field, if one of the instances had no relations. ([#16191](https://github.com/craftcms/cms/issues/16191))

## 5.5.9 - 2025-01-06

Expand Down
7 changes: 2 additions & 5 deletions src/fields/BaseRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,9 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed

if (is_array($value)) {
$value = array_values(array_filter($value));
$query->andWhere(['elements.id' => $value]);
if (!empty($value)) {
$query
->andWhere(['elements.id' => $value])
->orderBy([new FixedOrderExpression('elements.id', $value, Craft::$app->getDb())]);
} else {
$query->andWhere('0 = 1');
$query->orderBy([new FixedOrderExpression('elements.id', $value, Craft::$app->getDb())]);
}
} elseif ($value === null && $element?->id && $this->isFirstInstance($element)) {
// If $value is null, the element + field haven’t been saved since updating to Craft 5.3+,
Expand Down

0 comments on commit 38dc728

Please sign in to comment.