@@ -21,14 +21,6 @@ class AutoHydratorRecursive
2121 'belongsToMany ' ,
2222 ];
2323
24- /**
25- * @var string[]
26- */
27- protected array $ aliases ;
28-
29- /** @var array<string,string[]> SQL alias => fields */
30- protected array $ aliasMap = [];
31-
3224 /**
3325 * Precomputed mapping strategy.
3426 *
@@ -54,40 +46,12 @@ class AutoHydratorRecursive
5446
5547 /**
5648 * @param \Cake\ORM\Table $rootTable
57- * @param mixed[] $rows The result set of `$this->stmt->fetchAll(\PDO::FETCH_ASSOC);` .
49+ * @param mixed[] $mappingStrategy Mapping strategy .
5850 */
59- public function __construct (Table $ rootTable , array $ rows )
51+ public function __construct (Table $ rootTable , array $ mappingStrategy )
6052 {
6153 $ this ->rootTable = $ rootTable ;
62- $ firstRow = $ rows [0 ] ?? [];
63- if (!is_array ($ firstRow )) {
64- throw new \InvalidArgumentException ('First element of the result set is not an array ' );
65- }
66- $ keys = array_keys ($ firstRow );
67- $ this ->aliasMap = $ this ->buildAliasMap ($ keys );
68- $ this ->aliases = array_keys ($ this ->aliasMap );
69- $ strategy = new MappingStrategy ($ rootTable , $ this ->aliases );
70- $ this ->mappingStrategy = $ strategy ->build ()->toArray ();
71- }
72-
73- /**
74- * @param (string|null)[] $keys
75- * @return string[][]
76- */
77- protected function buildAliasMap (array $ keys ): array
78- {
79- $ map = [];
80- foreach ($ keys as $ key ) {
81- if (!is_string ($ key ) || !str_contains ($ key , '__ ' )) {
82- throw new UnknownAliasException ("Alias ' $ key' is invalid " );
83- }
84- [$ alias , $ field ] = explode ('__ ' , $ key , 2 );
85- if (mb_strlen ($ alias ) <= 0 || mb_strlen ($ field ) <= 0 ) {
86- throw new UnknownAliasException ("Alias ' $ key' is invalid " );
87- }
88- $ map [$ alias ][] = $ field ;
89- }
90- return $ map ;
54+ $ this ->mappingStrategy = $ mappingStrategy ;
9155 }
9256
9357 /**
@@ -153,7 +117,6 @@ protected function map(
153117 if (in_array ($ parentAssociation , ['hasOne ' , 'belongsTo ' ])) {
154118 if (!$ parent ->has ($ node ['propertyName ' ])) {
155119 // create new entity
156- //TODO: do not create entity if all fields are null
157120 $ entity = $ this ->constructEntity ($ className , $ row [$ alias ]);
158121 $ parent ->set ($ node ['propertyName ' ], $ entity );
159122 $ parent ->clean ();
@@ -171,7 +134,6 @@ protected function map(
171134 $ hash = $ this ->computeFieldsHash ($ row [$ alias ], $ parentHash );
172135 if (!isset ($ this ->entitiesMap [$ alias ][$ hash ])) {
173136 // create new entity
174- //TODO: do not create entity if all fields are null
175137 $ entity = $ this ->constructEntity ($ className , $ row [$ alias ]);
176138 if ($ entity !== null ) {
177139 $ siblings [] = $ entity ;
0 commit comments