44
55namespace Bancer \NativeQueryMapper \ORM ;
66
7- use Cake \Database \StatementInterface ;
87use Cake \Datasource \EntityInterface ;
98use Cake \ORM \Table ;
109use Cake \ORM \Entity ;
@@ -141,7 +140,7 @@ public function hydrateMany(array $rows): array
141140 $ results = [];
142141 $ rootAlias = $ this ->rootTable ->getAlias ();
143142 foreach ($ rows as $ row ) {
144- $ tree = $ this ->buildEntityRecursive ($ this ->rootTable , $ row, $ rootAlias );
143+ $ tree = $ this ->buildEntityRecursive ($ this ->rootTable , $ row );
145144 $ root = $ tree [$ rootAlias ];
146145 $ key = $ this ->entityKey ($ root , $ this ->rootTable );
147146 if (!isset ($ results [$ key ])) {
@@ -156,14 +155,12 @@ public function hydrateMany(array $rows): array
156155 /**
157156 * @param Table $table
158157 * @param mixed[] $row
159- * @param string $expectedAlias
160158 * @param mixed[] $visited
161159 * @return \Cake\Datasource\EntityInterface[]
162160 */
163161 protected function buildEntityRecursive (
164162 Table $ table ,
165163 array $ row ,
166- string $ expectedAlias ,
167164 array &$ visited = []
168165 ): array {
169166 $ alias = $ table ->getAlias ();
@@ -203,7 +200,7 @@ protected function buildEntityRecursive(
203200 continue ;
204201 }
205202 if ($ assoc instanceof HasMany) {
206- $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ childAlias , $ visited );
203+ $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ visited );
207204 if ($ tree ) {
208205 $ list = $ entity ->get ($ assoc ->getProperty ());
209206 if (!is_array ($ list )) {
@@ -216,25 +213,25 @@ protected function buildEntityRecursive(
216213 continue ;
217214 }
218215 if ($ assoc instanceof BelongsTo || $ assoc instanceof HasOne) {
219- $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ childAlias , $ visited );
216+ $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ visited );
220217 if ($ tree ) {
221218 $ entity ->set ($ assoc ->getProperty (), $ tree [$ childAlias ]);
222219 $ out += $ tree ;
223220 }
224221 continue ;
225222 }
226223 if ($ assoc instanceof BelongsToMany) {
227- $ junctionAlias = $ assoc ->getThrough ();
228- if (is_object ($ junctionAlias )) {
229- $ junctionAlias = $ junctionAlias ->getAlias ();
230- }
231- $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ childAlias , $ visited );
224+ $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ visited );
232225 if ($ tree ) {
233226 $ child = $ tree [$ childAlias ];
227+ $ junctionAlias = $ assoc ->getThrough ();
228+ if (is_object ($ junctionAlias )) {
229+ $ junctionAlias = $ junctionAlias ->getAlias ();
230+ }
234231 // hydrate join data only if the row contains it
235- if (isset ($ this ->aliasMap [$ junctionAlias ])) {
232+ if ($ junctionAlias !== null && isset ($ this ->aliasMap [$ junctionAlias ])) {
236233 $ junctionTable = TableRegistry::getTableLocator ()->get ($ junctionAlias );
237- $ jTree = $ this ->buildEntityRecursive ($ junctionTable , $ row , $ junctionAlias , $ visited );
234+ $ jTree = $ this ->buildEntityRecursive ($ junctionTable , $ row , $ visited );
238235 if ($ jTree ) {
239236 $ child ->set ('_joinData ' , $ jTree [$ junctionAlias ]);
240237 $ out += $ jTree ;
@@ -251,7 +248,7 @@ protected function buildEntityRecursive(
251248 continue ;
252249 }
253250 // fallback
254- $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ childAlias , $ visited );
251+ $ tree = $ this ->buildEntityRecursive ($ target , $ row , $ visited );
255252 if ($ tree ) {
256253 $ entity ->set ($ assoc ->getProperty (), $ tree [$ childAlias ]);
257254 $ out += $ tree ;
0 commit comments