From 4c67b6915a5d7d5740ec362604d8b95e7c943f70 Mon Sep 17 00:00:00 2001 From: i-just Date: Tue, 14 Jan 2025 10:05:32 +0000 Subject: [PATCH 1/3] when normalising relation field value, respect the empty array --- src/fields/BaseRelationField.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fields/BaseRelationField.php b/src/fields/BaseRelationField.php index af0355e247c..6b68ec53dc7 100644 --- a/src/fields/BaseRelationField.php +++ b/src/fields/BaseRelationField.php @@ -674,7 +674,11 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed ->andWhere(['elements.id' => $value]) ->orderBy([new FixedOrderExpression('elements.id', $value, Craft::$app->getDb())]); } else { - $query->andWhere('0 = 1'); + // if the value here is an empty array, the target ids are already stored + // in the elements_sites.content column, as an empty array; + // meaning the content was saved since v5.3.0 and the field is supposed to be empty; + // see https://github.com/craftcms/cms/issues/16191 + $query->andWhere(['elements.id' => []]); } } 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+, From 569594b5627b10ae145a6d7f70a16f5d4607d4af Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 14 Jan 2025 10:38:37 -0800 Subject: [PATCH 2/3] Cleanup --- src/fields/BaseRelationField.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/fields/BaseRelationField.php b/src/fields/BaseRelationField.php index 6b68ec53dc7..8e82b45d0d4 100644 --- a/src/fields/BaseRelationField.php +++ b/src/fields/BaseRelationField.php @@ -669,16 +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 { - // if the value here is an empty array, the target ids are already stored - // in the elements_sites.content column, as an empty array; - // meaning the content was saved since v5.3.0 and the field is supposed to be empty; - // see https://github.com/craftcms/cms/issues/16191 - $query->andWhere(['elements.id' => []]); + $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+, From 63cb6ee0f4bad989c39c5fe085b0915fc1135420 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 14 Jan 2025 10:41:16 -0800 Subject: [PATCH 3/3] Release note [ci skip] --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a051e11a0..bff6af210d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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