Merge pull request #11107 from creative-commoners/pulls/5/related-data

FIX Handle polymorphic relationships that use Owner instead of Parent
This commit is contained in:
Guy Sartorelli 2024-01-10 11:59:26 +13:00 committed by GitHub
commit 524e27f7ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -313,9 +313,10 @@ class StandardRelatedDataService implements RelatedDataService
$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: