@@ -231,11 +231,23 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
231231 }
232232
233233 foreach ($ hasOneRelationships as $ relationName => $ relationClassName ) {
234- $ relationClassName = $ this ->handlePolymorphicRelationship ($ relationName , $ relationClassName , $ dataObject );
235- if ($ relationClassName === null ) {
236- continue ;
234+ // Polymorphic has_one relationships can be defined as arrays, eg:
235+ // ['class' => DataObject::class, 'type' => 'polymorphic']
236+ // Extract the class name so the rest of the method can treat it as a string
237+ if (is_array ($ relationClassName )) {
238+ $ relationClassName = $ relationClassName ['class ' ] ?? null ;
239+
240+ if ($ relationClassName === null ) {
241+ $ this ->addWarning (sprintf (
242+ 'Polymorphic relationship "%s" in class "%s" has no "class" key defined ' ,
243+ $ relationName ,
244+ $ dataObject ->ClassName
245+ ));
246+
247+ continue ;
248+ }
237249 }
238-
250+
239251 // Relationship field names (as represented in the Database) are always appended with `ID`
240252 $ relationFieldName = sprintf ('%sID ' , $ relationName );
241253 // field_classname_map provides devs with the opportunity to describe polymorphic relationships (see the
@@ -245,15 +257,14 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
245257 // Apply the map that has been specified
246258 if ($ fieldClassNameMap !== null && array_key_exists ($ relationFieldName , $ fieldClassNameMap )) {
247259 $ relationClassName = $ dataObject ->relField ($ fieldClassNameMap [$ relationFieldName ]);
248-
249- // Ensure we have a valid class name string
250- if (!is_string ($ relationClassName ) || empty ($ relationClassName )) {
260+
261+ if (!is_string ($ relationClassName ) || $ relationClassName === '' ) {
251262 $ this ->addWarning (sprintf (
252- 'field_classname_map for field "%s" in class "%s" returned invalid class name: %s ' ,
263+ 'field_classname_map for "%s" in "%s" did not resolve to a valid class name ' ,
253264 $ relationFieldName ,
254- $ dataObject ->ClassName ,
255- is_array ($ relationClassName ) ? 'array ' : gettype ($ relationClassName )
265+ $ dataObject ->ClassName
256266 ));
267+
257268 continue ;
258269 }
259270 }
@@ -331,48 +342,6 @@ private function addDataObjectHasOneFields(DataObject $dataObject): void
331342 }
332343 }
333344
334- /**
335- * Handle polymorphic relationship configurations where the relationship class is defined as an array
336- *
337- * @param string $relationName The name of the relationship
338- * @param string|array $relationClassName The class name or polymorphic configuration array
339- * @param DataObject $dataObject The data object being processed
340- * @return string|null The resolved class name, or null if the relationship should be skipped
341- */
342- private function handlePolymorphicRelationship (string $ relationName , string |array $ relationClassName , DataObject $ dataObject ): ?string
343- {
344- // If it's already a string, no processing needed
345- if (is_string ($ relationClassName )) {
346- return $ relationClassName ;
347- }
348-
349- // Handle polymorphic relationships where relationClassName is an array
350- if (is_array ($ relationClassName )) {
351- // For polymorphic relationships, get the base class
352- if (isset ($ relationClassName ['class ' ])) {
353- return $ relationClassName ['class ' ];
354- } else {
355- $ this ->addWarning (sprintf (
356- 'Polymorphic relationship "%s" in class "%s" has invalid configuration: %s ' ,
357- $ relationName ,
358- $ dataObject ->ClassName ,
359- json_encode ($ relationClassName , JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES , 2 )
360- ));
361- return null ;
362- }
363- }
364-
365- // If it's neither string nor array, something's wrong
366- $ this ->addWarning (sprintf (
367- 'Relationship "%s" in class "%s" has unexpected type "%s": %s ' ,
368- $ relationName ,
369- $ dataObject ->ClassName ,
370- gettype ($ relationClassName ),
371- json_encode ($ relationClassName , JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES , 2 )
372- ));
373- return null ;
374- }
375-
376345 /**
377346 * @throws Exception
378347 */
0 commit comments