From 6743de43560b082d9f10708c35556ae7e53ca56a Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Fri, 12 Apr 2024 09:40:34 +1200 Subject: [PATCH] FIX Remove ambiguity for polymorphic queries (#11195) --- src/ORM/PolymorphicHasManyList.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ORM/PolymorphicHasManyList.php b/src/ORM/PolymorphicHasManyList.php index 261209f65..6f1908441 100644 --- a/src/ORM/PolymorphicHasManyList.php +++ b/src/ORM/PolymorphicHasManyList.php @@ -57,7 +57,8 @@ class PolymorphicHasManyList extends HasManyList public function setForeignRelation(string $relationName): static { Deprecation::notice('5.2.0', 'Will be replaced with a parameter in the constructor'); - $this->dataQuery->where(["\"{$this->relationForeignKey}\"" => $relationName]); + $foreignRelationColumn = DataObject::getSchema()->sqlColumnForField($this->dataClass, $this->relationForeignKey); + $this->dataQuery->where([$foreignRelationColumn => $relationName]); $this->dataQuery->setQueryParam('Foreign.Relation', $relationName); return $this; } @@ -102,8 +103,9 @@ class PolymorphicHasManyList extends HasManyList // For queries with multiple foreign IDs (such as that generated by // DataList::relation) the filter must be generalised to filter by subclasses $classNames = Convert::raw2sql(ClassInfo::subclassesFor($foreignClass)); + $foreignClassColumn = DataObject::getSchema()->sqlColumnForField($dataClass, $this->classForeignKey); $this->dataQuery->where(sprintf( - "\"{$this->classForeignKey}\" IN ('%s')", + "$foreignClassColumn IN ('%s')", implode("', '", $classNames) )); }