1818use LogicException ;
1919use ReflectionClass ;
2020
21+ use function array_key_exists ;
2122use function array_map ;
2223use function array_merge ;
2324use function count ;
2425use function end ;
2526use function in_array ;
2627use function is_array ;
28+ use function ksort ;
2729
2830/**
2931 * Base class for all hydrators. A hydrator is a class that provides some form
@@ -263,6 +265,17 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
263265 {
264266 $ rowData = ['data ' => [], 'newObjects ' => []];
265267
268+ foreach ($ this ->rsm ->newObjectMappings as $ mapping ) {
269+ if (! array_key_exists ($ mapping ['objIndex ' ], $ this ->rsm ->newObject )) {
270+ $ this ->rsm ->newObject [$ mapping ['objIndex ' ]] = $ mapping ['className ' ];
271+ }
272+ }
273+
274+ foreach ($ this ->rsm ->newObject as $ objIndex => $ newObject ) {
275+ $ rowData ['newObjects ' ][$ objIndex ]['class ' ] = new ReflectionClass ($ newObject );
276+ $ rowData ['newObjects ' ][$ objIndex ]['args ' ] = [];
277+ }
278+
266279 foreach ($ data as $ key => $ value ) {
267280 $ cacheKeyInfo = $ this ->hydrateColumnInfo ($ key );
268281 if ($ cacheKeyInfo === null ) {
@@ -282,7 +295,6 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
282295 $ value = $ this ->buildEnum ($ value , $ cacheKeyInfo ['enumType ' ]);
283296 }
284297
285- $ rowData ['newObjects ' ][$ objIndex ]['class ' ] = $ cacheKeyInfo ['class ' ];
286298 $ rowData ['newObjects ' ][$ objIndex ]['args ' ][$ argIndex ] = $ value ;
287299 break ;
288300
@@ -336,21 +348,17 @@ protected function gatherRowData(array $data, array &$id, array &$nonemptyCompon
336348 }
337349 }
338350
339- foreach ($ this ->resultSetMapping ()->nestedNewObjectArguments as $ objIndex => ['ownerIndex ' => $ ownerIndex , 'argIndex ' => $ argIndex ]) {
340- if (! isset ($ rowData ['newObjects ' ][$ ownerIndex . ': ' . $ argIndex ])) {
341- continue ;
351+ foreach ($ this ->resultSetMapping ()->nestedNewObjectArguments as ['ownerIndex ' => $ ownerIndex , 'argIndex ' => $ argIndex , 'argAlias ' => $ argAlias ]) {
352+ if (array_key_exists ($ argAlias , $ rowData ['newObjects ' ])) {
353+ ksort ($ rowData ['newObjects ' ][$ argAlias ]['args ' ]);
354+ $ rowData ['newObjects ' ][$ ownerIndex ]['args ' ][$ argIndex ] = $ rowData ['newObjects ' ][$ argAlias ]['class ' ]->newInstanceArgs ($ rowData ['newObjects ' ][$ argAlias ]['args ' ]);
355+ unset($ rowData ['newObjects ' ][$ argAlias ]);
342356 }
343-
344- $ newObject = $ rowData ['newObjects ' ][$ ownerIndex . ': ' . $ argIndex ];
345- unset($ rowData ['newObjects ' ][$ ownerIndex . ': ' . $ argIndex ]);
346-
347- $ obj = $ newObject ['class ' ]->newInstanceArgs ($ newObject ['args ' ]);
348-
349- $ rowData ['newObjects ' ][$ ownerIndex ]['args ' ][$ argIndex ] = $ obj ;
350357 }
351358
352359 foreach ($ rowData ['newObjects ' ] as $ objIndex => $ newObject ) {
353- $ obj = $ newObject ['class ' ]->newInstanceArgs ($ newObject ['args ' ]);
360+ ksort ($ rowData ['newObjects ' ][$ objIndex ]['args ' ]);
361+ $ obj = $ rowData ['newObjects ' ][$ objIndex ]['class ' ]->newInstanceArgs ($ rowData ['newObjects ' ][$ objIndex ]['args ' ]);
354362
355363 $ rowData ['newObjects ' ][$ objIndex ]['obj ' ] = $ obj ;
356364 }
@@ -454,7 +462,6 @@ protected function hydrateColumnInfo(string $key): array|null
454462 'type ' => Type::getType ($ this ->rsm ->typeMappings [$ key ]),
455463 'argIndex ' => $ mapping ['argIndex ' ],
456464 'objIndex ' => $ mapping ['objIndex ' ],
457- 'class ' => new ReflectionClass ($ mapping ['className ' ]),
458465 'enumType ' => $ this ->rsm ->enumMappings [$ key ] ?? null ,
459466 ];
460467
0 commit comments