Skip to content

Commit ff591ff

Browse files
committed
Merge branch '5.x' into 5.10
2 parents 51daa03 + 89e4326 commit ff591ff

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Fixed a bug where reference tags weren’t working with generated fields. ([#18692](https://github.com/craftcms/cms/issues/18692))
1010
- Fixed errors that could occur when applying project config changes. ([#18720](https://github.com/craftcms/cms/issues/18720))
1111
- Fixed a bug where it wasn’t always possible to sign into a user account that had the same email address as an inactive user. ([#18723](https://github.com/craftcms/cms/issues/18723))
12+
- Fixed a bug where relational fields’ element query results weren’t always limited to the selected relations if the `id` param was overridden. ([#15570](https://github.com/craftcms/cms/issues/15570))
1213

1314
## 5.9.20 - 2026-04-14
1415

src/fields/BaseRelationField.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -807,37 +807,35 @@ public function normalizeValue(mixed $value, ?ElementInterface $element): mixed
807807
CancelableEvent $event,
808808
ElementQuery $query,
809809
) use ($element, $relationsAlias) {
810-
if ($query->id === null) {
811-
// Make these changes directly on the prepared queries, so `sortOrder` doesn't ever make it into
812-
// the criteria. Otherwise, if the query ends up A) getting executed normally, then B) getting
813-
// eager-loaded with eagerly(), the `orderBy` value referencing the join table will get applied
814-
// to the eager-loading query and cause a SQL error.
815-
foreach ([$query->query, $query->subQuery] as $q) {
816-
$q->innerJoin(
817-
[$relationsAlias => DbTable::RELATIONS],
810+
// Make these changes directly on the prepared queries, so `sortOrder` doesn't ever make it into
811+
// the criteria. Otherwise, if the query ends up A) getting executed normally, then B) getting
812+
// eager-loaded with eagerly(), the `orderBy` value referencing the join table will get applied
813+
// to the eager-loading query and cause a SQL error.
814+
foreach ([$query->query, $query->subQuery] as $q) {
815+
$q->innerJoin(
816+
[$relationsAlias => DbTable::RELATIONS],
817+
[
818+
'and',
819+
"[[$relationsAlias.targetId]] = [[elements.id]]",
818820
[
819-
'and',
820-
"[[$relationsAlias.targetId]] = [[elements.id]]",
821-
[
822-
"$relationsAlias.sourceId" => $element->id,
823-
"$relationsAlias.fieldId" => $this->id,
824-
],
825-
[
826-
'or',
827-
["$relationsAlias.sourceSiteId" => null],
828-
["$relationsAlias.sourceSiteId" => $element->siteId],
829-
],
830-
]
831-
);
832-
833-
if (
834-
$this->sortable &&
835-
!$this->maintainHierarchy &&
836-
count($query->orderBy ?? []) === 1 &&
837-
($query->orderBy[0] ?? null) instanceof OrderByPlaceholderExpression
838-
) {
839-
$q->orderBy(["$relationsAlias.sortOrder" => SORT_ASC]);
840-
}
821+
"$relationsAlias.sourceId" => $element->id,
822+
"$relationsAlias.fieldId" => $this->id,
823+
],
824+
[
825+
'or',
826+
["$relationsAlias.sourceSiteId" => null],
827+
["$relationsAlias.sourceSiteId" => $element->siteId],
828+
],
829+
]
830+
);
831+
832+
if (
833+
$this->sortable &&
834+
!$this->maintainHierarchy &&
835+
count($query->orderBy ?? []) === 1 &&
836+
($query->orderBy[0] ?? null) instanceof OrderByPlaceholderExpression
837+
) {
838+
$q->orderBy(["$relationsAlias.sortOrder" => SORT_ASC]);
841839
}
842840
}
843841
},

0 commit comments

Comments
 (0)