Skip to content

Commit 8064b21

Browse files
jsirishchrispenny
authored andcommitted
Fix polymorphic relationship handling in FixtureService
When has_one relationships are configured as arrays (polymorphic relationships), the code was failing with a fatal error trying to check if the array value existed as a class configuration. This fix: - Checks if relationClassName is an array - Extracts the 'class' key for polymorphic relationships - Adds proper warning handling for invalid configurations - Validates that field_classname_map returns valid class names Fixes fatal error: DataObject::get() cannot query non-subclass DataObject directly when processing polymorphic has_one relationships defined as arrays.
1 parent 6b7829e commit 8064b21

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Service/FixtureService.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
231231
}
232232

233233
foreach ($hasOneRelationships as $relationName => $relationClassName) {
234+
// Handle polymorphic relationships where relationClassName might be an array
235+
if (is_array($relationClassName)) {
236+
// For polymorphic relationships, get the base class
237+
if (isset($relationClassName['class'])) {
238+
$relationClassName = $relationClassName['class'];
239+
} else {
240+
$this->addWarning(sprintf(
241+
'Polymorphic relationship "%s" in class "%s" has invalid configuration: %s',
242+
$relationName,
243+
$dataObject->ClassName,
244+
var_export($relationClassName, true)
245+
));
246+
continue;
247+
}
248+
}
249+
234250
// Relationship field names (as represented in the Database) are always appended with `ID`
235251
$relationFieldName = sprintf('%sID', $relationName);
236252
// field_classname_map provides devs with the opportunity to describe polymorphic relationships (see the
@@ -240,6 +256,17 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
240256
// Apply the map that has been specified
241257
if ($fieldClassNameMap !== null && array_key_exists($relationFieldName, $fieldClassNameMap)) {
242258
$relationClassName = $dataObject->relField($fieldClassNameMap[$relationFieldName]);
259+
260+
// Ensure we have a valid class name string
261+
if (!is_string($relationClassName) || empty($relationClassName)) {
262+
$this->addWarning(sprintf(
263+
'field_classname_map for field "%s" in class "%s" returned invalid class name: %s',
264+
$relationFieldName,
265+
$dataObject->ClassName,
266+
is_array($relationClassName) ? 'array' : gettype($relationClassName)
267+
));
268+
continue;
269+
}
243270
}
244271

245272
// Check to see if class has requested that it not be included in relationship maps

0 commit comments

Comments
 (0)