From 9bfb731bc3a36deae2147d830aa8485584ff2f17 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 10 Jan 2024 11:43:26 +1300 Subject: [PATCH] FIX Handle polymorphic relationships that use Owner instead of Parent --- src/ORM/RelatedData/StandardRelatedDataService.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ORM/RelatedData/StandardRelatedDataService.php b/src/ORM/RelatedData/StandardRelatedDataService.php index c6be4632d4b..c8faa8168f9 100644 --- a/src/ORM/RelatedData/StandardRelatedDataService.php +++ b/src/ORM/RelatedData/StandardRelatedDataService.php @@ -313,9 +313,10 @@ private function fetchReverseHasOneResults(DataObject $record, string $class, st $tableName = $this->dataObjectSchema->tableName($class); $where = sprintf('"%s" = %u', $componentIDField, $record->ID); - // Polymorphic - if ($componentIDField === 'ParentID' && isset($dbFields['ParentClass'])) { - $where .= sprintf(' AND "ParentClass" = %s', $this->prepareClassNameLiteral(get_class($record))); + // Polymorphic - if $component is "Parent" or "Owner" then usually it will be polymorphic + $isPolymorphic = DataObject::getSchema()->hasOneComponent($class, $component) === DataObject::class; + if ($isPolymorphic) { + $where .= sprintf(' AND "' . $component . 'Class" = %s', $this->prepareClassNameLiteral(get_class($record))); } // Example SQL: