1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Internal\Hydration; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Event\PostLoadEventDispatcher; |
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\ORM\Mapping\ManyToManyAssociationMetadata; |
10
|
|
|
use Doctrine\ORM\Mapping\ToManyAssociationMetadata; |
11
|
|
|
use Doctrine\ORM\Mapping\ToOneAssociationMetadata; |
12
|
|
|
use Doctrine\ORM\PersistentCollection; |
13
|
|
|
use Doctrine\ORM\Query; |
14
|
|
|
use Doctrine\ORM\UnitOfWork; |
15
|
|
|
use PDO; |
16
|
|
|
use ProxyManager\Proxy\GhostObjectInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The ObjectHydrator constructs an object graph out of an SQL result set. |
20
|
|
|
* |
21
|
|
|
* Internal note: Highly performance-sensitive code. |
22
|
|
|
* |
23
|
|
|
* @since 2.0 |
24
|
|
|
* @author Roman Borschel <[email protected]> |
25
|
|
|
* @author Guilherme Blanco <[email protected]> |
26
|
|
|
* @author Fabio B. Silva <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ObjectHydrator extends AbstractHydrator |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $identifierMap = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $resultPointers = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $idTemplate = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
private $resultCounter = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $rootAliases = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $initializedCollections = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private $existingCollections = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function prepare() |
69
|
|
|
{ |
70
|
|
|
if (! isset($this->hints[UnitOfWork::HINT_DEFEREAGERLOAD])) { |
71
|
|
|
$this->hints[UnitOfWork::HINT_DEFEREAGERLOAD] = true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($this->rsm->aliasMap as $dqlAlias => $className) { |
75
|
|
|
$this->identifierMap[$dqlAlias] = []; |
76
|
|
|
$this->idTemplate[$dqlAlias] = ''; |
77
|
|
|
|
78
|
|
|
// Remember which associations are "fetch joined", so that we know where to inject |
79
|
|
|
// collection stubs or proxies and where not. |
80
|
|
|
if (! isset($this->rsm->relationMap[$dqlAlias])) { |
81
|
653 |
|
continue; |
82
|
|
|
} |
83
|
653 |
|
|
84
|
563 |
|
$parent = $this->rsm->parentAliasMap[$dqlAlias]; |
85
|
|
|
|
86
|
|
|
if (! isset($this->rsm->aliasMap[$parent])) { |
87
|
653 |
|
throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $parent); |
88
|
620 |
|
} |
89
|
620 |
|
|
90
|
|
|
$sourceClassName = $this->rsm->aliasMap[$parent]; |
91
|
|
|
$sourceClass = $this->getClassMetadata($sourceClassName); |
92
|
|
|
$association = $sourceClass->getProperty($this->rsm->relationMap[$dqlAlias]); |
93
|
620 |
|
|
94
|
620 |
|
$this->hints['fetched'][$parent][$association->getName()] = true; |
95
|
|
|
|
96
|
|
|
if ($association instanceof ManyToManyAssociationMetadata) { |
97
|
328 |
|
continue; |
98
|
|
|
} |
99
|
328 |
|
|
100
|
1 |
|
// Mark any non-collection opposite sides as fetched, too. |
101
|
|
|
if ($association->getMappedBy()) { |
102
|
|
|
$this->hints['fetched'][$dqlAlias][$association->getMappedBy()] = true; |
103
|
327 |
|
|
104
|
327 |
|
continue; |
105
|
327 |
|
} |
106
|
|
|
|
107
|
327 |
|
// handle fetch-joined owning side bi-directional one-to-one associations |
108
|
|
|
if ($association->getInversedBy()) { |
109
|
327 |
|
$class = $this->getClassMetadata($className); |
110
|
35 |
|
$inverseAssoc = $class->getProperty($association->getInversedBy()); |
111
|
|
|
|
112
|
|
|
if (! ($inverseAssoc instanceof ToOneAssociationMetadata)) { |
113
|
|
|
continue; |
114
|
306 |
|
} |
115
|
257 |
|
|
116
|
|
|
$this->hints['fetched'][$dqlAlias][$inverseAssoc->getName()] = true; |
117
|
257 |
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
64 |
|
/** |
122
|
44 |
|
* {@inheritdoc} |
123
|
44 |
|
*/ |
124
|
|
|
protected function cleanup() |
125
|
44 |
|
{ |
126
|
26 |
|
$eagerLoad = (isset($this->hints[UnitOfWork::HINT_DEFEREAGERLOAD])) && $this->hints[UnitOfWork::HINT_DEFEREAGERLOAD] == true; |
127
|
|
|
|
128
|
|
|
parent::cleanup(); |
129
|
39 |
|
|
130
|
|
|
$this->identifierMap = |
131
|
|
|
$this->initializedCollections = |
132
|
652 |
|
$this->existingCollections = |
133
|
|
|
$this->resultPointers = []; |
134
|
|
|
|
135
|
|
|
if ($eagerLoad) { |
136
|
|
|
$this->uow->triggerEagerLoads(); |
137
|
647 |
|
} |
138
|
|
|
|
139
|
647 |
|
$this->uow->hydrationComplete(); |
140
|
|
|
} |
141
|
647 |
|
|
142
|
|
|
/** |
143
|
647 |
|
* {@inheritdoc} |
144
|
647 |
|
*/ |
145
|
647 |
View Code Duplication |
protected function hydrateAllData() |
146
|
647 |
|
{ |
147
|
|
|
$result = []; |
148
|
647 |
|
|
149
|
647 |
|
while ($row = $this->stmt->fetch(PDO::FETCH_ASSOC)) { |
150
|
|
|
$this->hydrateRowData($row, $result); |
151
|
|
|
} |
152
|
647 |
|
|
153
|
647 |
|
// Take snapshots from all newly initialized collections |
154
|
|
|
foreach ($this->initializedCollections as $coll) { |
155
|
|
|
$coll->takeSnapshot(); |
156
|
|
|
} |
157
|
|
|
|
158
|
646 |
|
return $result; |
159
|
|
|
} |
160
|
646 |
|
|
161
|
|
|
/** |
162
|
646 |
|
* Initializes a related collection. |
163
|
612 |
|
* |
164
|
|
|
* @param object $entity The entity to which the collection belongs. |
165
|
|
|
* @param ClassMetadata $class |
166
|
|
|
* @param string $fieldName The name of the field on the entity that holds the collection. |
167
|
643 |
|
* @param string $parentDqlAlias Alias of the parent fetch joining this collection. |
168
|
67 |
|
* |
169
|
|
|
* @return \Doctrine\ORM\PersistentCollection |
170
|
|
|
*/ |
171
|
643 |
|
private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias) |
172
|
|
|
{ |
173
|
|
|
/** @var ToManyAssociationMetadata $association */ |
174
|
|
|
$association = $class->getProperty($fieldName); |
175
|
|
|
$value = $association->getValue($entity); |
176
|
|
|
$oid = spl_object_hash($entity); |
177
|
|
|
|
178
|
|
|
if (! $value instanceof PersistentCollection) { |
179
|
|
|
$value = $association->wrap($entity, $value, $this->em); |
180
|
|
|
|
181
|
|
|
$association->setValue($entity, $value); |
182
|
|
|
|
183
|
|
|
$this->uow->setOriginalEntityProperty($oid, $fieldName, $value); |
184
|
99 |
|
|
185
|
|
|
$this->initializedCollections[$oid . $fieldName] = $value; |
186
|
99 |
|
} else if ( |
187
|
99 |
|
isset($this->hints[Query::HINT_REFRESH]) || |
188
|
99 |
|
(isset($this->hints['fetched'][$parentDqlAlias][$fieldName]) && ! $value->isInitialized()) |
189
|
|
|
) { |
190
|
99 |
|
// Is already PersistentCollection, but either REFRESH or FETCH-JOIN and UNINITIALIZED! |
191
|
63 |
|
$value->setDirty(false); |
192
|
|
|
$value->setInitialized(true); |
193
|
|
|
$value->unwrap()->clear(); |
194
|
99 |
|
|
195
|
63 |
|
$this->initializedCollections[$oid . $fieldName] = $value; |
196
|
63 |
|
} else { |
197
|
|
|
// Is already PersistentCollection, and DON'T REFRESH or FETCH-JOIN! |
198
|
63 |
|
$this->existingCollections[$oid . $fieldName] = $value; |
199
|
|
|
} |
200
|
63 |
|
|
201
|
63 |
|
return $value; |
202
|
|
|
} |
203
|
63 |
|
|
204
|
|
|
/** |
205
|
39 |
|
* Gets an entity instance. |
206
|
39 |
|
* |
207
|
39 |
|
* @param array $data The instance data. |
208
|
|
|
* @param string $dqlAlias The DQL alias of the entity's class. |
209
|
|
|
* |
210
|
6 |
|
* @return object The entity. |
211
|
6 |
|
* |
212
|
6 |
|
* @throws HydrationException |
213
|
|
|
*/ |
214
|
6 |
|
private function getEntity(array $data, $dqlAlias) |
215
|
|
|
{ |
216
|
|
|
$className = $this->rsm->aliasMap[$dqlAlias]; |
217
|
34 |
|
|
218
|
|
|
if (isset($this->rsm->discriminatorColumns[$dqlAlias])) { |
219
|
|
|
$fieldName = $this->rsm->discriminatorColumns[$dqlAlias]; |
220
|
99 |
|
|
221
|
|
|
if ( ! isset($this->rsm->metaMappings[$fieldName])) { |
222
|
|
|
throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $fieldName, $dqlAlias); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$discrColumn = $this->rsm->metaMappings[$fieldName]; |
226
|
|
|
|
227
|
|
|
if ( ! isset($data[$discrColumn])) { |
228
|
|
|
throw HydrationException::missingDiscriminatorColumn($className, $discrColumn, $dqlAlias); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($data[$discrColumn] === "") { |
232
|
|
|
throw HydrationException::emptyDiscriminatorValue($dqlAlias); |
233
|
583 |
|
} |
234
|
|
|
|
235
|
583 |
|
$discrMap = $this->metadataCache[$className]->discriminatorMap; |
236
|
|
|
$discriminatorValue = (string) $data[$discrColumn]; |
237
|
583 |
|
|
238
|
56 |
|
if ( ! isset($discrMap[$discriminatorValue])) { |
239
|
|
|
throw HydrationException::invalidDiscriminatorValue($discriminatorValue, array_keys($discrMap)); |
240
|
56 |
|
} |
241
|
1 |
|
|
242
|
|
|
$className = $discrMap[$discriminatorValue]; |
243
|
|
|
|
244
|
55 |
|
unset($data[$discrColumn]); |
245
|
|
|
} |
246
|
55 |
|
|
247
|
1 |
|
if (isset($this->hints[Query::HINT_REFRESH_ENTITY], $this->rootAliases[$dqlAlias])) { |
248
|
|
|
$id = $this->em->getIdentifierFlattener()->flattenIdentifier($this->metadataCache[$className], $data); |
249
|
|
|
|
250
|
55 |
|
$this->em->getUnitOfWork()->registerManaged($this->hints[Query::HINT_REFRESH_ENTITY], $id, $data); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$this->hints['fetchAlias'] = $dqlAlias; |
254
|
55 |
|
|
255
|
|
|
return $this->uow->createEntity($className, $data, $this->hints); |
256
|
55 |
|
} |
257
|
1 |
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $className |
260
|
54 |
|
* @param array $data |
261
|
|
|
* |
262
|
54 |
|
* @return mixed |
263
|
|
|
*/ |
264
|
|
|
private function getEntityFromIdentityMap($className, array $data) |
265
|
581 |
|
{ |
266
|
24 |
|
/* @var ClassMetadata $class */ |
267
|
|
|
$class = $this->metadataCache[$className]; |
268
|
|
|
$id = $this->em->getIdentifierFlattener()->flattenIdentifier($class, $data); |
269
|
581 |
|
|
270
|
|
|
return $this->uow->tryGetById($id, $class->getRootClassName()); |
271
|
581 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Hydrates a single row in an SQL result set. |
275
|
|
|
* |
276
|
|
|
* @internal |
277
|
|
|
* First, the data of the row is split into chunks where each chunk contains data |
278
|
|
|
* that belongs to a particular component/class. Afterwards, all these chunks |
279
|
|
|
* are processed, one after the other. For each chunk of class data only one of the |
280
|
34 |
|
* following code paths is executed: |
281
|
|
|
* |
282
|
|
|
* Path A: The data chunk belongs to a joined/associated object and the association |
283
|
34 |
|
* is collection-valued. |
284
|
|
|
* Path B: The data chunk belongs to a joined/associated object and the association |
285
|
|
|
* is single-valued. |
286
|
34 |
|
* Path C: The data chunk belongs to a root result element/object that appears in the topmost |
287
|
1 |
|
* level of the hydrated result. A typical example are the objects of the type |
288
|
|
|
* specified by the FROM clause in a DQL query. |
289
|
1 |
|
* |
290
|
1 |
|
* @param array $row The data of the row to process. |
291
|
1 |
|
* @param array $result The result array to fill. |
292
|
1 |
|
* |
293
|
|
|
* @return void |
294
|
|
|
*/ |
295
|
1 |
|
protected function hydrateRowData(array $row, array &$result) |
296
|
33 |
|
{ |
297
|
|
|
// Initialize |
298
|
|
|
$id = $this->idTemplate; // initialize the id-memory |
299
|
|
|
$nonemptyComponents = []; |
300
|
33 |
|
// Split the row data into chunks of class data. |
301
|
|
|
$rowData = $this->gatherRowData($row, $id, $nonemptyComponents); |
302
|
|
|
|
303
|
|
|
// reset result pointers for each data row |
304
|
|
|
$this->resultPointers = []; |
305
|
|
|
|
306
|
|
|
// Hydrate the data chunks |
307
|
|
|
foreach ($rowData['data'] as $dqlAlias => $data) { |
308
|
|
|
$entityName = $this->rsm->aliasMap[$dqlAlias]; |
309
|
|
|
|
310
|
|
|
if (isset($this->rsm->parentAliasMap[$dqlAlias])) { |
311
|
|
|
// It's a joined result |
312
|
|
|
|
313
|
|
|
$parentAlias = $this->rsm->parentAliasMap[$dqlAlias]; |
314
|
|
|
// we need the $path to save into the identifier map which entities were already |
315
|
|
|
// seen for this parent-child relationship |
316
|
|
|
$path = $parentAlias . '.' . $dqlAlias; |
317
|
|
|
|
318
|
|
|
// We have a RIGHT JOIN result here. Doctrine cannot hydrate RIGHT JOIN Object-Graphs |
319
|
|
|
if (! isset($nonemptyComponents[$parentAlias])) { |
320
|
|
|
// TODO: Add special case code where we hydrate the right join objects into identity map at least |
321
|
|
|
continue; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
$parentClass = $this->metadataCache[$this->rsm->aliasMap[$parentAlias]]; |
325
|
617 |
|
$relationField = $this->rsm->relationMap[$dqlAlias]; |
326
|
|
|
$association = $parentClass->getProperty($relationField); |
327
|
|
|
|
328
|
617 |
|
// Get a reference to the parent object to which the joined element belongs. |
329
|
617 |
|
if ($this->rsm->isMixed && isset($this->rootAliases[$parentAlias])) { |
330
|
|
|
$objectClass = $this->resultPointers[$parentAlias]; |
331
|
617 |
|
$parentObject = $objectClass[key($objectClass)]; |
332
|
|
|
} else if (isset($this->resultPointers[$parentAlias])) { |
333
|
|
|
$parentObject = $this->resultPointers[$parentAlias]; |
334
|
617 |
|
} else { |
335
|
583 |
|
// Parent object of relation not found, mark as not-fetched again |
336
|
|
|
$element = $this->getEntity($data, $dqlAlias); |
337
|
583 |
|
|
338
|
|
|
// Update result pointer and provide initial fetch data for parent |
339
|
|
|
$this->resultPointers[$dqlAlias] = $element; |
340
|
320 |
|
$rowData['data'][$parentAlias][$relationField] = $element; |
341
|
|
|
|
342
|
|
|
// Mark as not-fetched again |
343
|
320 |
|
unset($this->hints['fetched'][$parentAlias][$relationField]); |
344
|
|
|
continue; |
345
|
|
|
} |
346
|
320 |
|
|
347
|
|
|
$oid = spl_object_hash($parentObject); |
348
|
2 |
|
|
349
|
|
|
// Check the type of the relation (many or single-valued) |
350
|
|
|
if (! ($association instanceof ToOneAssociationMetadata)) { |
351
|
320 |
|
// PATH A: Collection-valued association |
352
|
320 |
|
$reflFieldValue = $association->getValue($parentObject); |
353
|
320 |
|
|
354
|
320 |
|
if (isset($nonemptyComponents[$dqlAlias])) { |
355
|
|
|
$collKey = $oid . $relationField; |
356
|
|
|
if (isset($this->initializedCollections[$collKey])) { |
357
|
320 |
|
$reflFieldValue = $this->initializedCollections[$collKey]; |
358
|
17 |
|
} else if (! isset($this->existingCollections[$collKey])) { |
359
|
17 |
|
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); |
360
|
305 |
|
} |
361
|
305 |
|
|
362
|
|
|
$indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]); |
363
|
|
|
$index = $indexExists ? $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false; |
364
|
1 |
|
$indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false; |
365
|
|
|
|
366
|
|
|
if (! $indexExists || ! $indexIsValid) { |
367
|
1 |
|
if (isset($this->existingCollections[$collKey])) { |
368
|
1 |
|
// Collection exists, only look for the element in the identity map. |
369
|
|
|
if ($element = $this->getEntityFromIdentityMap($entityName, $data)) { |
370
|
|
|
$this->resultPointers[$dqlAlias] = $element; |
371
|
1 |
|
} else { |
372
|
1 |
|
unset($this->resultPointers[$dqlAlias]); |
373
|
|
|
} |
374
|
|
|
} else { |
375
|
320 |
|
$element = $this->getEntity($data, $dqlAlias); |
376
|
|
|
|
377
|
|
|
if (isset($this->rsm->indexByMap[$dqlAlias])) { |
378
|
320 |
|
$indexValue = $row[$this->rsm->indexByMap[$dqlAlias]]; |
379
|
|
|
$reflFieldValue->hydrateSet($indexValue, $element); |
380
|
100 |
|
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue; |
381
|
|
|
} else { |
382
|
100 |
|
$reflFieldValue->hydrateAdd($element); |
383
|
96 |
|
$reflFieldValue->last(); |
384
|
96 |
|
$this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key(); |
385
|
44 |
|
} |
386
|
96 |
|
// Update result pointer |
387
|
96 |
|
$this->resultPointers[$dqlAlias] = $element; |
388
|
|
|
} |
389
|
|
|
} else { |
390
|
96 |
|
// Update result pointer |
391
|
96 |
|
$this->resultPointers[$dqlAlias] = $reflFieldValue[$index]; |
392
|
96 |
|
} |
393
|
|
|
} else if ( ! $reflFieldValue) { |
394
|
96 |
|
$reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); |
|
|
|
|
395
|
96 |
|
} else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) { |
396
|
|
|
$reflFieldValue->setInitialized(true); |
397
|
34 |
|
} |
398
|
34 |
|
} else { |
399
|
|
|
// PATH B: Single-valued association |
400
|
34 |
|
$reflFieldValue = $association->getValue($parentObject); |
401
|
|
|
|
402
|
|
|
if (! $reflFieldValue || isset($this->hints[Query::HINT_REFRESH]) || |
403
|
64 |
|
($reflFieldValue instanceof GhostObjectInterface && ! $reflFieldValue->isProxyInitialized())) { |
404
|
|
|
// we only need to take action if this value is null, |
405
|
64 |
|
// we refresh the entity or its an uninitialized proxy. |
406
|
11 |
|
if (isset($nonemptyComponents[$dqlAlias])) { |
407
|
11 |
|
$element = $this->getEntity($data, $dqlAlias); |
408
|
11 |
|
|
409
|
|
|
$association->setValue($parentObject, $element); |
410
|
53 |
|
$this->uow->setOriginalEntityProperty($oid, $relationField, $element); |
411
|
53 |
|
|
412
|
53 |
|
$mappedBy = $association->getMappedBy(); |
413
|
|
|
$targetClass = $this->metadataCache[$association->getTargetEntity()]; |
414
|
|
|
|
415
|
96 |
|
if ($association->isOwningSide()) { |
416
|
|
|
// TODO: Just check hints['fetched'] here? |
417
|
|
|
// If there is an inverse mapping on the target class its bidirectional |
418
|
|
|
if ($association->getInversedBy()) { |
|
|
|
|
419
|
96 |
|
$inverseAssociation = $targetClass->getProperty($association->getInversedBy()); |
420
|
|
|
|
421
|
9 |
|
if ($inverseAssociation instanceof ToOneAssociationMetadata) { |
422
|
5 |
|
$inverseAssociation->setValue($element, $parentObject); |
423
|
6 |
|
|
424
|
100 |
|
$this->uow->setOriginalEntityProperty( |
425
|
|
|
spl_object_hash($element), |
426
|
|
|
$inverseAssociation->getName(), |
427
|
|
|
$parentObject |
428
|
|
|
); |
429
|
240 |
|
} |
430
|
|
|
} |
431
|
240 |
|
} else { |
432
|
|
|
// For sure bidirectional, as there is no inverse side in unidirectional mappings |
433
|
|
|
$inverseAssociation = $targetClass->getProperty($mappedBy); |
434
|
225 |
|
|
435
|
116 |
|
$inverseAssociation->setValue($element, $parentObject); |
436
|
115 |
|
|
437
|
115 |
|
$this->uow->setOriginalEntityProperty( |
438
|
115 |
|
spl_object_hash($element), |
439
|
|
|
$mappedBy, |
440
|
115 |
|
$parentObject |
441
|
|
|
); |
442
|
|
|
} |
443
|
51 |
|
|
444
|
34 |
|
// Update result pointer |
445
|
34 |
|
$this->resultPointers[$dqlAlias] = $element; |
446
|
12 |
|
} else { |
447
|
34 |
|
$association->setValue($parentObject, null); |
448
|
|
|
|
449
|
18 |
|
$this->uow->setOriginalEntityProperty($oid, $relationField, null); |
450
|
|
|
} |
451
|
51 |
|
// else leave $reflFieldValue null for single-valued associations |
452
|
|
|
} else { |
453
|
|
|
// Update result pointer |
454
|
|
|
$this->resultPointers[$dqlAlias] = $reflFieldValue; |
455
|
66 |
|
} |
456
|
66 |
|
} |
457
|
|
|
} else { |
458
|
|
|
// PATH C: Its a root result element |
459
|
115 |
|
$this->rootAliases[$dqlAlias] = true; // Mark as root alias |
460
|
|
|
$entityKey = $this->rsm->entityMappings[$dqlAlias] ?: 0; |
461
|
130 |
|
|
462
|
224 |
|
// if this row has a NULL value for the root result id then make it a null result. |
463
|
|
View Code Duplication |
if ( ! isset($nonemptyComponents[$dqlAlias]) ) { |
|
|
|
|
464
|
|
|
if ($this->rsm->isMixed) { |
465
|
|
|
$result[] = [$entityKey => null]; |
466
|
|
|
} else { |
467
|
319 |
|
$result[] = null; |
468
|
|
|
} |
469
|
|
|
$resultKey = $this->resultCounter; |
470
|
|
|
++$this->resultCounter; |
471
|
|
|
continue; |
472
|
583 |
|
} |
473
|
583 |
|
|
474
|
|
|
// check for existing result from the iterations before |
475
|
|
|
if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) { |
476
|
583 |
|
$element = $this->getEntity($data, $dqlAlias); |
477
|
3 |
|
|
478
|
2 |
|
if ($this->rsm->isMixed) { |
479
|
|
|
$element = [$entityKey => $element]; |
480
|
1 |
|
} |
481
|
|
|
|
482
|
3 |
|
if (isset($this->rsm->indexByMap[$dqlAlias])) { |
483
|
3 |
|
$resultKey = $row[$this->rsm->indexByMap[$dqlAlias]]; |
484
|
3 |
|
|
485
|
|
|
if (isset($this->hints['collection'])) { |
486
|
|
|
$this->hints['collection']->hydrateSet($resultKey, $element); |
487
|
|
|
} |
488
|
583 |
|
|
489
|
583 |
|
$result[$resultKey] = $element; |
490
|
|
|
} else { |
491
|
581 |
|
$resultKey = $this->resultCounter; |
492
|
38 |
|
++$this->resultCounter; |
493
|
|
|
|
494
|
|
|
if (isset($this->hints['collection'])) { |
495
|
581 |
|
$this->hints['collection']->hydrateAdd($element); |
496
|
26 |
|
} |
497
|
|
|
|
498
|
26 |
|
$result[] = $element; |
499
|
10 |
|
} |
500
|
|
|
|
501
|
|
|
$this->identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey; |
502
|
26 |
|
|
503
|
|
|
// Update result pointer |
504
|
564 |
|
$this->resultPointers[$dqlAlias] = $element; |
505
|
564 |
|
|
506
|
|
|
} else { |
507
|
564 |
|
// Update result pointer |
508
|
109 |
|
$index = $this->identifierMap[$dqlAlias][$id[$dqlAlias]]; |
509
|
|
|
$this->resultPointers[$dqlAlias] = $result[$index]; |
510
|
|
|
$resultKey = $index; |
511
|
564 |
|
} |
512
|
|
|
} |
513
|
|
|
|
514
|
581 |
View Code Duplication |
if (isset($this->hints[Query::HINT_INTERNAL_ITERATION]) && $this->hints[Query::HINT_INTERNAL_ITERATION]) { |
|
|
|
|
515
|
|
|
$this->uow->hydrationComplete(); |
516
|
|
|
} |
517
|
581 |
|
} |
518
|
|
|
|
519
|
|
|
if ( ! isset($resultKey) ) { |
520
|
|
|
$this->resultCounter++; |
521
|
75 |
|
} |
522
|
75 |
|
|
523
|
75 |
|
// Append scalar values to mixed result sets |
524
|
|
View Code Duplication |
if (isset($rowData['scalars'])) { |
|
|
|
|
525
|
|
|
if ( ! isset($resultKey) ) { |
526
|
|
|
$resultKey = (isset($this->rsm->indexByMap['scalars'])) |
527
|
581 |
|
? $row[$this->rsm->indexByMap['scalars']] |
528
|
581 |
|
: $this->resultCounter - 1; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
foreach ($rowData['scalars'] as $name => $value) { |
532
|
614 |
|
$result[$resultKey][$name] = $value; |
533
|
34 |
|
} |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
// Append new object to mixed result sets |
537
|
614 |
|
if (isset($rowData['newObjects'])) { |
538
|
51 |
|
if ( ! isset($resultKey) ) { |
539
|
21 |
|
$resultKey = $this->resultCounter - 1; |
540
|
2 |
|
} |
541
|
21 |
|
|
542
|
|
|
|
543
|
|
|
$hasNoScalars = ! (isset($rowData['scalars']) && $rowData['scalars']); |
544
|
51 |
|
|
545
|
51 |
View Code Duplication |
foreach ($rowData['newObjects'] as $objIndex => $newObject) { |
|
|
|
|
546
|
|
|
$class = $newObject['class']; |
547
|
|
|
$args = $newObject['args']; |
548
|
|
|
$obj = $class->newInstanceArgs($args); |
549
|
|
|
|
550
|
614 |
|
if ($hasNoScalars && \count($rowData['newObjects']) === 1 ) { |
551
|
19 |
|
$result[$resultKey] = $obj; |
552
|
13 |
|
|
553
|
|
|
continue; |
554
|
|
|
} |
555
|
|
|
|
556
|
19 |
|
$result[$resultKey][$objIndex] = $obj; |
557
|
|
|
} |
558
|
19 |
|
} |
559
|
19 |
|
} |
560
|
19 |
|
|
561
|
19 |
|
/** |
562
|
|
|
* When executed in a hydrate() loop we may have to clear internal state to |
563
|
19 |
|
* decrease memory consumption. |
564
|
10 |
|
* |
565
|
|
|
* @param mixed $eventArgs |
566
|
10 |
|
* |
567
|
|
|
* @return void |
568
|
|
|
*/ |
569
|
9 |
|
public function onClear($eventArgs) |
570
|
|
|
{ |
571
|
|
|
parent::onClear($eventArgs); |
572
|
614 |
|
|
573
|
|
|
$aliases = array_keys($this->identifierMap); |
574
|
|
|
|
575
|
|
|
$this->identifierMap = array_fill_keys($aliases, []); |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.