Skip to content

Commit

Permalink
FIX Handle polymorphic relationships that use Owner instead of Parent
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 9, 2024
1 parent b1a1d4b commit 9bfb731
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ORM/RelatedData/StandardRelatedDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9bfb731

Please sign in to comment.