1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
23
|
|
|
use Doctrine\Common\Collections\Collection; |
24
|
|
|
use Doctrine\Common\NotifyPropertyChanged; |
25
|
|
|
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService; |
26
|
|
|
use Doctrine\Common\Persistence\ObjectManagerAware; |
27
|
|
|
use Doctrine\Common\PropertyChangedListener; |
28
|
|
|
use Doctrine\DBAL\LockMode; |
29
|
|
|
use Doctrine\ORM\Cache\Persister\CachedPersister; |
30
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
31
|
|
|
use Doctrine\ORM\Event\ListenersInvoker; |
32
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
33
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
34
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
35
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
36
|
|
|
use Doctrine\ORM\Internal\HydrationCompleteHandler; |
37
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
38
|
|
|
use Doctrine\ORM\Mapping\Reflection\ReflectionPropertiesGetter; |
39
|
|
|
use Doctrine\ORM\Persisters\Collection\ManyToManyPersister; |
40
|
|
|
use Doctrine\ORM\Persisters\Collection\OneToManyPersister; |
41
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister; |
42
|
|
|
use Doctrine\ORM\Persisters\Entity\JoinedSubclassPersister; |
43
|
|
|
use Doctrine\ORM\Persisters\Entity\SingleTablePersister; |
44
|
|
|
use Doctrine\ORM\Proxy\Proxy; |
45
|
|
|
use Doctrine\ORM\Utility\IdentifierFlattener; |
46
|
|
|
use InvalidArgumentException; |
47
|
|
|
use Throwable; |
48
|
|
|
use UnexpectedValueException; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The UnitOfWork is responsible for tracking changes to objects during an |
52
|
|
|
* "object-level" transaction and for writing out changes to the database |
53
|
|
|
* in the correct order. |
54
|
|
|
* |
55
|
|
|
* Internal note: This class contains highly performance-sensitive code. |
56
|
|
|
* |
57
|
|
|
* @since 2.0 |
58
|
|
|
* @author Benjamin Eberlei <[email protected]> |
59
|
|
|
* @author Guilherme Blanco <[email protected]> |
60
|
|
|
* @author Jonathan Wage <[email protected]> |
61
|
|
|
* @author Roman Borschel <[email protected]> |
62
|
|
|
* @author Rob Caiger <[email protected]> |
63
|
|
|
*/ |
64
|
|
|
class UnitOfWork implements PropertyChangedListener |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* An entity is in MANAGED state when its persistence is managed by an EntityManager. |
68
|
|
|
*/ |
69
|
|
|
const STATE_MANAGED = 1; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* An entity is new if it has just been instantiated (i.e. using the "new" operator) |
73
|
|
|
* and is not (yet) managed by an EntityManager. |
74
|
|
|
*/ |
75
|
|
|
const STATE_NEW = 2; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* A detached entity is an instance with persistent state and identity that is not |
79
|
|
|
* (or no longer) associated with an EntityManager (and a UnitOfWork). |
80
|
|
|
*/ |
81
|
|
|
const STATE_DETACHED = 3; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* A removed entity instance is an instance with a persistent identity, |
85
|
|
|
* associated with an EntityManager, whose persistent state will be deleted |
86
|
|
|
* on commit. |
87
|
|
|
*/ |
88
|
|
|
const STATE_REMOVED = 4; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Hint used to collect all primary keys of associated entities during hydration |
92
|
|
|
* and execute it in a dedicated query afterwards |
93
|
|
|
* @see https://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html?highlight=eager#temporarily-change-fetch-mode-in-dql |
94
|
|
|
*/ |
95
|
|
|
const HINT_DEFEREAGERLOAD = 'deferEagerLoad'; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The identity map that holds references to all managed entities that have |
99
|
|
|
* an identity. The entities are grouped by their class name. |
100
|
|
|
* Since all classes in a hierarchy must share the same identifier set, |
101
|
|
|
* we always take the root class name of the hierarchy. |
102
|
|
|
* |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private $identityMap = []; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Associate entities with OIDs to ensure the GC won't recycle a managed entity |
109
|
|
|
* |
110
|
|
|
* DDC-2332 / #3037 |
111
|
|
|
* |
112
|
|
|
* @var array |
113
|
|
|
*/ |
114
|
|
|
private $oidMap = array(); |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Map of all identifiers of managed entities. |
118
|
|
|
* Keys are object ids (spl_object_hash). |
119
|
|
|
* |
120
|
|
|
* @var array |
121
|
|
|
*/ |
122
|
|
|
private $entityIdentifiers = []; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Map of the original entity data of managed entities. |
126
|
|
|
* Keys are object ids (spl_object_hash). This is used for calculating changesets |
127
|
|
|
* at commit time. |
128
|
|
|
* |
129
|
|
|
* Internal note: Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
130
|
|
|
* A value will only really be copied if the value in the entity is modified |
131
|
|
|
* by the user. |
132
|
|
|
* |
133
|
|
|
* @var array |
134
|
|
|
*/ |
135
|
|
|
private $originalEntityData = []; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Map of entity changes. Keys are object ids (spl_object_hash). |
139
|
|
|
* Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
140
|
|
|
* |
141
|
|
|
* @var array |
142
|
|
|
*/ |
143
|
|
|
private $entityChangeSets = []; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* The (cached) states of any known entities. |
147
|
|
|
* Keys are object ids (spl_object_hash). |
148
|
|
|
* |
149
|
|
|
* @var array |
150
|
|
|
*/ |
151
|
|
|
private $entityStates = []; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Map of entities that are scheduled for dirty checking at commit time. |
155
|
|
|
* This is only used for entities with a change tracking policy of DEFERRED_EXPLICIT. |
156
|
|
|
* Keys are object ids (spl_object_hash). |
157
|
|
|
* |
158
|
|
|
* @var array |
159
|
|
|
*/ |
160
|
|
|
private $scheduledForSynchronization = []; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* A list of all pending entity insertions. |
164
|
|
|
* |
165
|
|
|
* @var array |
166
|
|
|
*/ |
167
|
|
|
private $entityInsertions = []; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* A list of all pending entity updates. |
171
|
|
|
* |
172
|
|
|
* @var array |
173
|
|
|
*/ |
174
|
|
|
private $entityUpdates = []; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Any pending extra updates that have been scheduled by persisters. |
178
|
|
|
* |
179
|
|
|
* @var array |
180
|
|
|
*/ |
181
|
|
|
private $extraUpdates = []; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* A list of all pending entity deletions. |
185
|
|
|
* |
186
|
|
|
* @var array |
187
|
|
|
*/ |
188
|
|
|
private $entityDeletions = []; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* New entities that were discovered through relationships that were not |
192
|
|
|
* marked as cascade-persist. During flush, this array is populated and |
193
|
|
|
* then pruned of any entities that were discovered through a valid |
194
|
|
|
* cascade-persist path. (Leftovers cause an error.) |
195
|
|
|
* |
196
|
|
|
* Keys are OIDs, payload is a two-item array describing the association |
197
|
|
|
* and the entity. |
198
|
|
|
* |
199
|
|
|
* @var object[][]|array[][] indexed by respective object spl_object_hash() |
200
|
|
|
*/ |
201
|
|
|
private $nonCascadedNewDetectedEntities = []; |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* All pending collection deletions. |
205
|
|
|
* |
206
|
|
|
* @var array |
207
|
|
|
*/ |
208
|
|
|
private $collectionDeletions = []; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* All pending collection updates. |
212
|
|
|
* |
213
|
|
|
* @var array |
214
|
|
|
*/ |
215
|
|
|
private $collectionUpdates = []; |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
219
|
|
|
* At the end of the UnitOfWork all these collections will make new snapshots |
220
|
|
|
* of their data. |
221
|
|
|
* |
222
|
|
|
* @var array |
223
|
|
|
*/ |
224
|
|
|
private $visitedCollections = []; |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* The EntityManager that "owns" this UnitOfWork instance. |
228
|
|
|
* |
229
|
|
|
* @var EntityManagerInterface |
230
|
|
|
*/ |
231
|
|
|
private $em; |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* The entity persister instances used to persist entity instances. |
235
|
|
|
* |
236
|
|
|
* @var array |
237
|
|
|
*/ |
238
|
|
|
private $persisters = []; |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* The collection persister instances used to persist collections. |
242
|
|
|
* |
243
|
|
|
* @var array |
244
|
|
|
*/ |
245
|
|
|
private $collectionPersisters = []; |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* The EventManager used for dispatching events. |
249
|
|
|
* |
250
|
|
|
* @var \Doctrine\Common\EventManager |
251
|
|
|
*/ |
252
|
|
|
private $evm; |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* The ListenersInvoker used for dispatching events. |
256
|
|
|
* |
257
|
|
|
* @var \Doctrine\ORM\Event\ListenersInvoker |
258
|
|
|
*/ |
259
|
|
|
private $listenersInvoker; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* The IdentifierFlattener used for manipulating identifiers |
263
|
|
|
* |
264
|
|
|
* @var \Doctrine\ORM\Utility\IdentifierFlattener |
265
|
|
|
*/ |
266
|
|
|
private $identifierFlattener; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Orphaned entities that are scheduled for removal. |
270
|
|
|
* |
271
|
|
|
* @var array |
272
|
|
|
*/ |
273
|
|
|
private $orphanRemovals = []; |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Read-Only objects are never evaluated |
277
|
|
|
* |
278
|
|
|
* @var array |
279
|
|
|
*/ |
280
|
|
|
private $readOnlyObjects = []; |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Map of Entity Class-Names and corresponding IDs that should eager loaded when requested. |
284
|
|
|
* |
285
|
|
|
* @var array |
286
|
|
|
*/ |
287
|
|
|
private $eagerLoadingEntities = []; |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @var boolean |
291
|
|
|
*/ |
292
|
|
|
protected $hasCache = false; |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Helper for handling completion of hydration |
296
|
|
|
* |
297
|
|
|
* @var HydrationCompleteHandler |
298
|
|
|
*/ |
299
|
|
|
private $hydrationCompleteHandler; |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @var ReflectionPropertiesGetter |
303
|
|
|
*/ |
304
|
|
|
private $reflectionPropertiesGetter; |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Initializes a new UnitOfWork instance, bound to the given EntityManager. |
308
|
|
|
* |
309
|
|
|
* @param EntityManagerInterface $em |
310
|
|
|
*/ |
311
|
2477 |
|
public function __construct(EntityManagerInterface $em) |
312
|
|
|
{ |
313
|
2477 |
|
$this->em = $em; |
314
|
2477 |
|
$this->evm = $em->getEventManager(); |
315
|
2477 |
|
$this->listenersInvoker = new ListenersInvoker($em); |
316
|
2477 |
|
$this->hasCache = $em->getConfiguration()->isSecondLevelCacheEnabled(); |
317
|
2477 |
|
$this->identifierFlattener = new IdentifierFlattener($this, $em->getMetadataFactory()); |
318
|
2477 |
|
$this->hydrationCompleteHandler = new HydrationCompleteHandler($this->listenersInvoker, $em); |
319
|
2477 |
|
$this->reflectionPropertiesGetter = new ReflectionPropertiesGetter(new RuntimeReflectionService()); |
320
|
2477 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Commits the UnitOfWork, executing all operations that have been postponed |
324
|
|
|
* up to this point. The state of all managed entities will be synchronized with |
325
|
|
|
* the database. |
326
|
|
|
* |
327
|
|
|
* The operations are executed in the following order: |
328
|
|
|
* |
329
|
|
|
* 1) All entity insertions |
330
|
|
|
* 2) All entity updates |
331
|
|
|
* 3) All collection deletions |
332
|
|
|
* 4) All collection updates |
333
|
|
|
* 5) All entity deletions |
334
|
|
|
* |
335
|
|
|
* @param null|object|array $entity |
336
|
|
|
* |
337
|
|
|
* @return void |
338
|
|
|
* |
339
|
|
|
* @throws \Exception |
340
|
|
|
*/ |
341
|
1088 |
|
public function commit($entity = null) |
342
|
|
|
{ |
343
|
|
|
// Raise preFlush |
344
|
1088 |
|
if ($this->evm->hasListeners(Events::preFlush)) { |
345
|
2 |
|
$this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em)); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
// Compute changes done since last commit. |
349
|
1088 |
|
if (null === $entity) { |
350
|
1078 |
|
$this->computeChangeSets(); |
351
|
19 |
|
} elseif (is_object($entity)) { |
352
|
17 |
|
$this->computeSingleEntityChangeSet($entity); |
353
|
2 |
|
} elseif (is_array($entity)) { |
|
|
|
|
354
|
2 |
|
foreach ($entity as $object) { |
355
|
2 |
|
$this->computeSingleEntityChangeSet($object); |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
1085 |
|
if ( ! ($this->entityInsertions || |
|
|
|
|
360
|
175 |
|
$this->entityDeletions || |
|
|
|
|
361
|
138 |
|
$this->entityUpdates || |
|
|
|
|
362
|
42 |
|
$this->collectionUpdates || |
|
|
|
|
363
|
38 |
|
$this->collectionDeletions || |
|
|
|
|
364
|
1085 |
|
$this->orphanRemovals)) { |
|
|
|
|
365
|
26 |
|
$this->dispatchOnFlushEvent(); |
366
|
26 |
|
$this->dispatchPostFlushEvent(); |
367
|
|
|
|
368
|
26 |
|
return; // Nothing to do. |
369
|
|
|
} |
370
|
|
|
|
371
|
1081 |
|
$this->assertThatThereAreNoUnintentionallyNonPersistedAssociations(); |
372
|
|
|
|
373
|
1079 |
|
if ($this->orphanRemovals) { |
|
|
|
|
374
|
16 |
|
foreach ($this->orphanRemovals as $orphan) { |
375
|
16 |
|
$this->remove($orphan); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
1079 |
|
$this->dispatchOnFlushEvent(); |
380
|
|
|
|
381
|
|
|
// Now we need a commit order to maintain referential integrity |
382
|
1079 |
|
$commitOrder = $this->getCommitOrder(); |
383
|
|
|
|
384
|
1079 |
|
$conn = $this->em->getConnection(); |
385
|
1079 |
|
$conn->beginTransaction(); |
386
|
|
|
|
387
|
|
|
try { |
388
|
|
|
// Collection deletions (deletions of complete collections) |
389
|
1079 |
|
foreach ($this->collectionDeletions as $collectionToDelete) { |
390
|
19 |
|
$this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); |
391
|
|
|
} |
392
|
|
|
|
393
|
1079 |
|
if ($this->entityInsertions) { |
|
|
|
|
394
|
1075 |
|
foreach ($commitOrder as $class) { |
395
|
1075 |
|
$this->executeInserts($class); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
1078 |
|
if ($this->entityUpdates) { |
|
|
|
|
400
|
123 |
|
foreach ($commitOrder as $class) { |
401
|
123 |
|
$this->executeUpdates($class); |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
// Extra updates that were requested by persisters. |
406
|
1074 |
|
if ($this->extraUpdates) { |
|
|
|
|
407
|
44 |
|
$this->executeExtraUpdates(); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
// Collection updates (deleteRows, updateRows, insertRows) |
411
|
1074 |
|
foreach ($this->collectionUpdates as $collectionToUpdate) { |
412
|
543 |
|
$this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
// Entity deletions come last and need to be in reverse commit order |
416
|
1074 |
|
if ($this->entityDeletions) { |
|
|
|
|
417
|
64 |
|
for ($count = count($commitOrder), $i = $count - 1; $i >= 0 && $this->entityDeletions; --$i) { |
|
|
|
|
418
|
64 |
|
$this->executeDeletions($commitOrder[$i]); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
1074 |
|
$conn->commit(); |
423
|
11 |
|
} catch (Throwable $e) { |
424
|
11 |
|
$this->em->close(); |
425
|
11 |
|
$conn->rollBack(); |
426
|
|
|
|
427
|
11 |
|
$this->afterTransactionRolledBack(); |
428
|
|
|
|
429
|
11 |
|
throw $e; |
430
|
|
|
} |
431
|
|
|
|
432
|
1074 |
|
$this->afterTransactionComplete(); |
433
|
|
|
|
434
|
|
|
// Take new snapshots from visited collections |
435
|
1074 |
|
foreach ($this->visitedCollections as $coll) { |
436
|
542 |
|
$coll->takeSnapshot(); |
437
|
|
|
} |
438
|
|
|
|
439
|
1074 |
|
$this->dispatchPostFlushEvent(); |
440
|
|
|
|
441
|
1073 |
|
$this->postCommitCleanup($entity); |
442
|
1073 |
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param null|object|object[] $entity |
446
|
|
|
*/ |
447
|
1073 |
|
private function postCommitCleanup($entity) : void |
448
|
|
|
{ |
449
|
1073 |
|
$this->entityInsertions = |
450
|
1073 |
|
$this->entityUpdates = |
451
|
1073 |
|
$this->entityDeletions = |
452
|
1073 |
|
$this->extraUpdates = |
453
|
1073 |
|
$this->collectionUpdates = |
454
|
1073 |
|
$this->nonCascadedNewDetectedEntities = |
455
|
1073 |
|
$this->collectionDeletions = |
456
|
1073 |
|
$this->visitedCollections = |
457
|
1073 |
|
$this->orphanRemovals = []; |
458
|
|
|
|
459
|
1073 |
|
if (null === $entity) { |
460
|
1063 |
|
$this->entityChangeSets = $this->scheduledForSynchronization = []; |
461
|
|
|
|
462
|
1063 |
|
return; |
463
|
|
|
} |
464
|
|
|
|
465
|
16 |
|
$entities = \is_object($entity) |
466
|
14 |
|
? [$entity] |
467
|
16 |
|
: $entity; |
468
|
|
|
|
469
|
16 |
|
foreach ($entities as $object) { |
470
|
16 |
|
$oid = \spl_object_hash($object); |
471
|
|
|
|
472
|
16 |
|
$this->clearEntityChangeSet($oid); |
473
|
|
|
|
474
|
16 |
|
unset($this->scheduledForSynchronization[$this->em->getClassMetadata(\get_class($object))->rootEntityName][$oid]); |
|
|
|
|
475
|
|
|
} |
476
|
16 |
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Computes the changesets of all entities scheduled for insertion. |
480
|
|
|
* |
481
|
|
|
* @return void |
482
|
|
|
*/ |
483
|
1087 |
|
private function computeScheduleInsertsChangeSets() |
484
|
|
|
{ |
485
|
1087 |
|
foreach ($this->entityInsertions as $entity) { |
486
|
1079 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
487
|
|
|
|
488
|
1079 |
|
$this->computeChangeSet($class, $entity); |
489
|
|
|
} |
490
|
1085 |
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Only flushes the given entity according to a ruleset that keeps the UoW consistent. |
494
|
|
|
* |
495
|
|
|
* 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well! |
496
|
|
|
* 2. Read Only entities are skipped. |
497
|
|
|
* 3. Proxies are skipped. |
498
|
|
|
* 4. Only if entity is properly managed. |
499
|
|
|
* |
500
|
|
|
* @param object $entity |
501
|
|
|
* |
502
|
|
|
* @return void |
503
|
|
|
* |
504
|
|
|
* @throws \InvalidArgumentException |
505
|
|
|
*/ |
506
|
19 |
|
private function computeSingleEntityChangeSet($entity) |
507
|
|
|
{ |
508
|
19 |
|
$state = $this->getEntityState($entity); |
509
|
|
|
|
510
|
19 |
|
if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
511
|
1 |
|
throw new \InvalidArgumentException("Entity has to be managed or scheduled for removal for single computation " . self::objToStr($entity)); |
512
|
|
|
} |
513
|
|
|
|
514
|
18 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
515
|
|
|
|
516
|
18 |
|
if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
517
|
17 |
|
$this->persist($entity); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
// Compute changes for INSERTed entities first. This must always happen even in this case. |
521
|
18 |
|
$this->computeScheduleInsertsChangeSets(); |
522
|
|
|
|
523
|
18 |
|
if ($class->isReadOnly) { |
|
|
|
|
524
|
|
|
return; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
// Ignore uninitialized proxy objects |
528
|
18 |
|
if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
|
|
|
|
529
|
2 |
|
return; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
// Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
533
|
16 |
|
$oid = spl_object_hash($entity); |
534
|
|
|
|
535
|
16 |
|
if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { |
536
|
7 |
|
$this->computeChangeSet($class, $entity); |
537
|
|
|
} |
538
|
16 |
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Executes any extra updates that have been scheduled. |
542
|
|
|
*/ |
543
|
44 |
|
private function executeExtraUpdates() |
544
|
|
|
{ |
545
|
44 |
|
foreach ($this->extraUpdates as $oid => $update) { |
546
|
44 |
|
list ($entity, $changeset) = $update; |
547
|
|
|
|
548
|
44 |
|
$this->entityChangeSets[$oid] = $changeset; |
549
|
44 |
|
$this->getEntityPersister(get_class($entity))->update($entity); |
550
|
|
|
} |
551
|
|
|
|
552
|
44 |
|
$this->extraUpdates = []; |
553
|
44 |
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Gets the changeset for an entity. |
557
|
|
|
* |
558
|
|
|
* @param object $entity |
559
|
|
|
* |
560
|
|
|
* @return array |
561
|
|
|
*/ |
562
|
1074 |
|
public function & getEntityChangeSet($entity) |
563
|
|
|
{ |
564
|
1074 |
|
$oid = spl_object_hash($entity); |
565
|
1074 |
|
$data = []; |
566
|
|
|
|
567
|
1074 |
|
if (!isset($this->entityChangeSets[$oid])) { |
568
|
4 |
|
return $data; |
569
|
|
|
} |
570
|
|
|
|
571
|
1074 |
|
return $this->entityChangeSets[$oid]; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Computes the changes that happened to a single entity. |
576
|
|
|
* |
577
|
|
|
* Modifies/populates the following properties: |
578
|
|
|
* |
579
|
|
|
* {@link _originalEntityData} |
580
|
|
|
* If the entity is NEW or MANAGED but not yet fully persisted (only has an id) |
581
|
|
|
* then it was not fetched from the database and therefore we have no original |
582
|
|
|
* entity data yet. All of the current entity data is stored as the original entity data. |
583
|
|
|
* |
584
|
|
|
* {@link _entityChangeSets} |
585
|
|
|
* The changes detected on all properties of the entity are stored there. |
586
|
|
|
* A change is a tuple array where the first entry is the old value and the second |
587
|
|
|
* entry is the new value of the property. Changesets are used by persisters |
588
|
|
|
* to INSERT/UPDATE the persistent entity state. |
589
|
|
|
* |
590
|
|
|
* {@link _entityUpdates} |
591
|
|
|
* If the entity is already fully MANAGED (has been fetched from the database before) |
592
|
|
|
* and any changes to its properties are detected, then a reference to the entity is stored |
593
|
|
|
* there to mark it for an update. |
594
|
|
|
* |
595
|
|
|
* {@link _collectionDeletions} |
596
|
|
|
* If a PersistentCollection has been de-referenced in a fully MANAGED entity, |
597
|
|
|
* then this collection is marked for deletion. |
598
|
|
|
* |
599
|
|
|
* @ignore |
600
|
|
|
* |
601
|
|
|
* @internal Don't call from the outside. |
602
|
|
|
* |
603
|
|
|
* @param ClassMetadata $class The class descriptor of the entity. |
604
|
|
|
* @param object $entity The entity for which to compute the changes. |
605
|
|
|
* |
606
|
|
|
* @return void |
607
|
|
|
*/ |
608
|
1089 |
|
public function computeChangeSet(ClassMetadata $class, $entity) |
609
|
|
|
{ |
610
|
1089 |
|
$oid = spl_object_hash($entity); |
611
|
|
|
|
612
|
1089 |
|
if (isset($this->readOnlyObjects[$oid])) { |
613
|
2 |
|
return; |
614
|
|
|
} |
615
|
|
|
|
616
|
1089 |
|
if ( ! $class->isInheritanceTypeNone()) { |
617
|
337 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
618
|
|
|
} |
619
|
|
|
|
620
|
1089 |
|
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preFlush) & ~ListenersInvoker::INVOKE_MANAGER; |
621
|
|
|
|
622
|
1089 |
|
if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
623
|
138 |
|
$this->listenersInvoker->invoke($class, Events::preFlush, $entity, new PreFlushEventArgs($this->em), $invoke); |
624
|
|
|
} |
625
|
|
|
|
626
|
1089 |
|
$actualData = []; |
627
|
|
|
|
628
|
1089 |
|
foreach ($class->reflFields as $name => $refProp) { |
629
|
1089 |
|
$value = $refProp->getValue($entity); |
630
|
|
|
|
631
|
1089 |
|
if ($class->isCollectionValuedAssociation($name) && $value !== null) { |
632
|
816 |
|
if ($value instanceof PersistentCollection) { |
633
|
206 |
|
if ($value->getOwner() === $entity) { |
634
|
206 |
|
continue; |
635
|
|
|
} |
636
|
|
|
|
637
|
5 |
|
$value = new ArrayCollection($value->getValues()); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
// If $value is not a Collection then use an ArrayCollection. |
641
|
811 |
|
if ( ! $value instanceof Collection) { |
642
|
243 |
|
$value = new ArrayCollection($value); |
643
|
|
|
} |
644
|
|
|
|
645
|
811 |
|
$assoc = $class->associationMappings[$name]; |
646
|
|
|
|
647
|
|
|
// Inject PersistentCollection |
648
|
811 |
|
$value = new PersistentCollection( |
649
|
811 |
|
$this->em, $this->em->getClassMetadata($assoc['targetEntity']), $value |
650
|
|
|
); |
651
|
811 |
|
$value->setOwner($entity, $assoc); |
652
|
811 |
|
$value->setDirty( ! $value->isEmpty()); |
653
|
|
|
|
654
|
811 |
|
$class->reflFields[$name]->setValue($entity, $value); |
655
|
|
|
|
656
|
811 |
|
$actualData[$name] = $value; |
657
|
|
|
|
658
|
811 |
|
continue; |
659
|
|
|
} |
660
|
|
|
|
661
|
1089 |
|
if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) { |
662
|
1089 |
|
$actualData[$name] = $value; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
|
666
|
1089 |
|
if ( ! isset($this->originalEntityData[$oid])) { |
667
|
|
|
// Entity is either NEW or MANAGED but not yet fully persisted (only has an id). |
668
|
|
|
// These result in an INSERT. |
669
|
1085 |
|
$this->originalEntityData[$oid] = $actualData; |
670
|
1085 |
|
$changeSet = []; |
671
|
|
|
|
672
|
1085 |
|
foreach ($actualData as $propName => $actualValue) { |
673
|
1062 |
|
if ( ! isset($class->associationMappings[$propName])) { |
674
|
1005 |
|
$changeSet[$propName] = [null, $actualValue]; |
675
|
|
|
|
676
|
1005 |
|
continue; |
677
|
|
|
} |
678
|
|
|
|
679
|
945 |
|
$assoc = $class->associationMappings[$propName]; |
680
|
|
|
|
681
|
945 |
|
if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
682
|
945 |
|
$changeSet[$propName] = [null, $actualValue]; |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
686
|
1085 |
|
$this->entityChangeSets[$oid] = $changeSet; |
687
|
|
|
} else { |
688
|
|
|
// Entity is "fully" MANAGED: it was already fully persisted before |
689
|
|
|
// and we have a copy of the original data |
690
|
278 |
|
$originalData = $this->originalEntityData[$oid]; |
691
|
278 |
|
$isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
692
|
278 |
|
$changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) |
693
|
|
|
? $this->entityChangeSets[$oid] |
694
|
278 |
|
: []; |
695
|
|
|
|
696
|
278 |
|
foreach ($actualData as $propName => $actualValue) { |
697
|
|
|
// skip field, its a partially omitted one! |
698
|
262 |
|
if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) { |
699
|
8 |
|
continue; |
700
|
|
|
} |
701
|
|
|
|
702
|
262 |
|
$orgValue = $originalData[$propName]; |
703
|
|
|
|
704
|
|
|
// skip if value haven't changed |
705
|
262 |
|
if ($orgValue === $actualValue) { |
706
|
245 |
|
continue; |
707
|
|
|
} |
708
|
|
|
|
709
|
|
|
// if regular field |
710
|
119 |
|
if ( ! isset($class->associationMappings[$propName])) { |
711
|
64 |
|
if ($isChangeTrackingNotify) { |
712
|
|
|
continue; |
713
|
|
|
} |
714
|
|
|
|
715
|
64 |
|
$changeSet[$propName] = [$orgValue, $actualValue]; |
716
|
|
|
|
717
|
64 |
|
continue; |
718
|
|
|
} |
719
|
|
|
|
720
|
59 |
|
$assoc = $class->associationMappings[$propName]; |
721
|
|
|
|
722
|
|
|
// Persistent collection was exchanged with the "originally" |
723
|
|
|
// created one. This can only mean it was cloned and replaced |
724
|
|
|
// on another entity. |
725
|
59 |
|
if ($actualValue instanceof PersistentCollection) { |
726
|
8 |
|
$owner = $actualValue->getOwner(); |
727
|
8 |
|
if ($owner === null) { // cloned |
728
|
|
|
$actualValue->setOwner($entity, $assoc); |
729
|
8 |
|
} else if ($owner !== $entity) { // no clone, we have to fix |
730
|
|
|
if (!$actualValue->isInitialized()) { |
731
|
|
|
$actualValue->initialize(); // we have to do this otherwise the cols share state |
732
|
|
|
} |
733
|
|
|
$newValue = clone $actualValue; |
734
|
|
|
$newValue->setOwner($entity, $assoc); |
735
|
|
|
$class->reflFields[$propName]->setValue($entity, $newValue); |
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
|
739
|
59 |
|
if ($orgValue instanceof PersistentCollection) { |
740
|
|
|
// A PersistentCollection was de-referenced, so delete it. |
741
|
8 |
|
$coid = spl_object_hash($orgValue); |
742
|
|
|
|
743
|
8 |
|
if (isset($this->collectionDeletions[$coid])) { |
744
|
|
|
continue; |
745
|
|
|
} |
746
|
|
|
|
747
|
8 |
|
$this->collectionDeletions[$coid] = $orgValue; |
748
|
8 |
|
$changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored. |
749
|
|
|
|
750
|
8 |
|
continue; |
751
|
|
|
} |
752
|
|
|
|
753
|
51 |
|
if ($assoc['type'] & ClassMetadata::TO_ONE) { |
754
|
50 |
|
if ($assoc['isOwningSide']) { |
755
|
22 |
|
$changeSet[$propName] = [$orgValue, $actualValue]; |
756
|
|
|
} |
757
|
|
|
|
758
|
50 |
|
if ($orgValue !== null && $assoc['orphanRemoval']) { |
759
|
51 |
|
$this->scheduleOrphanRemoval($orgValue); |
760
|
|
|
} |
761
|
|
|
} |
762
|
|
|
} |
763
|
|
|
|
764
|
278 |
|
if ($changeSet) { |
765
|
92 |
|
$this->entityChangeSets[$oid] = $changeSet; |
766
|
92 |
|
$this->originalEntityData[$oid] = $actualData; |
767
|
92 |
|
$this->entityUpdates[$oid] = $entity; |
768
|
|
|
} |
769
|
|
|
} |
770
|
|
|
|
771
|
|
|
// Look for changes in associations of the entity |
772
|
1089 |
|
foreach ($class->associationMappings as $field => $assoc) { |
773
|
945 |
|
if (($val = $class->reflFields[$field]->getValue($entity)) === null) { |
774
|
664 |
|
continue; |
775
|
|
|
} |
776
|
|
|
|
777
|
916 |
|
$this->computeAssociationChanges($assoc, $val); |
778
|
|
|
|
779
|
908 |
|
if ( ! isset($this->entityChangeSets[$oid]) && |
780
|
908 |
|
$assoc['isOwningSide'] && |
781
|
908 |
|
$assoc['type'] == ClassMetadata::MANY_TO_MANY && |
782
|
908 |
|
$val instanceof PersistentCollection && |
783
|
908 |
|
$val->isDirty()) { |
784
|
|
|
|
785
|
35 |
|
$this->entityChangeSets[$oid] = []; |
786
|
35 |
|
$this->originalEntityData[$oid] = $actualData; |
787
|
908 |
|
$this->entityUpdates[$oid] = $entity; |
788
|
|
|
} |
789
|
|
|
} |
790
|
1081 |
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Computes all the changes that have been done to entities and collections |
794
|
|
|
* since the last commit and stores these changes in the _entityChangeSet map |
795
|
|
|
* temporarily for access by the persisters, until the UoW commit is finished. |
796
|
|
|
* |
797
|
|
|
* @return void |
798
|
|
|
*/ |
799
|
1078 |
|
public function computeChangeSets() |
800
|
|
|
{ |
801
|
|
|
// Compute changes for INSERTed entities first. This must always happen. |
802
|
1078 |
|
$this->computeScheduleInsertsChangeSets(); |
803
|
|
|
|
804
|
|
|
// Compute changes for other MANAGED entities. Change tracking policies take effect here. |
805
|
1076 |
|
foreach ($this->identityMap as $className => $entities) { |
806
|
472 |
|
$class = $this->em->getClassMetadata($className); |
807
|
|
|
|
808
|
|
|
// Skip class if instances are read-only |
809
|
472 |
|
if ($class->isReadOnly) { |
|
|
|
|
810
|
1 |
|
continue; |
811
|
|
|
} |
812
|
|
|
|
813
|
|
|
// If change tracking is explicit or happens through notification, then only compute |
814
|
|
|
// changes on entities of that type that are explicitly marked for synchronization. |
815
|
|
|
switch (true) { |
816
|
471 |
|
case ($class->isChangeTrackingDeferredImplicit()): |
817
|
469 |
|
$entitiesToProcess = $entities; |
818
|
469 |
|
break; |
819
|
|
|
|
820
|
3 |
|
case (isset($this->scheduledForSynchronization[$className])): |
821
|
3 |
|
$entitiesToProcess = $this->scheduledForSynchronization[$className]; |
822
|
3 |
|
break; |
823
|
|
|
|
824
|
|
|
default: |
825
|
1 |
|
$entitiesToProcess = []; |
826
|
|
|
|
827
|
|
|
} |
828
|
|
|
|
829
|
471 |
|
foreach ($entitiesToProcess as $entity) { |
830
|
|
|
// Ignore uninitialized proxy objects |
831
|
451 |
|
if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
|
|
|
|
832
|
37 |
|
continue; |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
// Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here. |
836
|
450 |
|
$oid = spl_object_hash($entity); |
837
|
|
|
|
838
|
450 |
|
if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) { |
839
|
471 |
|
$this->computeChangeSet($class, $entity); |
840
|
|
|
} |
841
|
|
|
} |
842
|
|
|
} |
843
|
1076 |
|
} |
844
|
|
|
|
845
|
|
|
/** |
846
|
|
|
* Computes the changes of an association. |
847
|
|
|
* |
848
|
|
|
* @param array $assoc The association mapping. |
849
|
|
|
* @param mixed $value The value of the association. |
850
|
|
|
* |
851
|
|
|
* @throws ORMInvalidArgumentException |
852
|
|
|
* @throws ORMException |
853
|
|
|
* |
854
|
|
|
* @return void |
855
|
|
|
*/ |
856
|
916 |
|
private function computeAssociationChanges($assoc, $value) |
857
|
|
|
{ |
858
|
916 |
|
if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
|
|
|
859
|
30 |
|
return; |
860
|
|
|
} |
861
|
|
|
|
862
|
915 |
|
if ($value instanceof PersistentCollection && $value->isDirty()) { |
863
|
547 |
|
$coid = spl_object_hash($value); |
864
|
|
|
|
865
|
547 |
|
$this->collectionUpdates[$coid] = $value; |
866
|
547 |
|
$this->visitedCollections[$coid] = $value; |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
// Look through the entities, and in any of their associations, |
870
|
|
|
// for transient (new) entities, recursively. ("Persistence by reachability") |
871
|
|
|
// Unwrap. Uninitialized collections will simply be empty. |
872
|
915 |
|
$unwrappedValue = ($assoc['type'] & ClassMetadata::TO_ONE) ? [$value] : $value->unwrap(); |
873
|
915 |
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
874
|
|
|
|
875
|
915 |
|
foreach ($unwrappedValue as $key => $entry) { |
876
|
755 |
|
if (! ($entry instanceof $targetClass->name)) { |
|
|
|
|
877
|
8 |
|
throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry); |
878
|
|
|
} |
879
|
|
|
|
880
|
747 |
|
$state = $this->getEntityState($entry, self::STATE_NEW); |
881
|
|
|
|
882
|
747 |
|
if ( ! ($entry instanceof $assoc['targetEntity'])) { |
883
|
|
|
throw ORMException::unexpectedAssociationValue($assoc['sourceEntity'], $assoc['fieldName'], get_class($entry), $assoc['targetEntity']); |
884
|
|
|
} |
885
|
|
|
|
886
|
|
|
switch ($state) { |
887
|
747 |
|
case self::STATE_NEW: |
888
|
42 |
|
if ( ! $assoc['isCascadePersist']) { |
889
|
|
|
/* |
890
|
|
|
* For now just record the details, because this may |
891
|
|
|
* not be an issue if we later discover another pathway |
892
|
|
|
* through the object-graph where cascade-persistence |
893
|
|
|
* is enabled for this object. |
894
|
|
|
*/ |
895
|
6 |
|
$this->nonCascadedNewDetectedEntities[\spl_object_hash($entry)] = [$assoc, $entry]; |
896
|
|
|
|
897
|
6 |
|
break; |
898
|
|
|
} |
899
|
|
|
|
900
|
37 |
|
$this->persistNew($targetClass, $entry); |
901
|
37 |
|
$this->computeChangeSet($targetClass, $entry); |
902
|
|
|
|
903
|
37 |
|
break; |
904
|
|
|
|
905
|
739 |
|
case self::STATE_REMOVED: |
906
|
|
|
// Consume the $value as array (it's either an array or an ArrayAccess) |
907
|
|
|
// and remove the element from Collection. |
908
|
4 |
|
if ($assoc['type'] & ClassMetadata::TO_MANY) { |
909
|
3 |
|
unset($value[$key]); |
910
|
|
|
} |
911
|
4 |
|
break; |
912
|
|
|
|
913
|
739 |
|
case self::STATE_DETACHED: |
914
|
|
|
// Can actually not happen right now as we assume STATE_NEW, |
915
|
|
|
// so the exception will be raised from the DBAL layer (constraint violation). |
916
|
|
|
throw ORMInvalidArgumentException::detachedEntityFoundThroughRelationship($assoc, $entry); |
917
|
|
|
break; |
918
|
|
|
|
919
|
747 |
|
default: |
920
|
|
|
// MANAGED associated entities are already taken into account |
921
|
|
|
// during changeset calculation anyway, since they are in the identity map. |
922
|
|
|
} |
923
|
|
|
} |
924
|
907 |
|
} |
925
|
|
|
|
926
|
|
|
/** |
927
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $class |
928
|
|
|
* @param object $entity |
929
|
|
|
* |
930
|
|
|
* @return void |
931
|
|
|
*/ |
932
|
1108 |
|
private function persistNew($class, $entity) |
933
|
|
|
{ |
934
|
1108 |
|
$oid = spl_object_hash($entity); |
935
|
1108 |
|
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::prePersist); |
936
|
|
|
|
937
|
1108 |
|
if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
938
|
141 |
|
$this->listenersInvoker->invoke($class, Events::prePersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
939
|
|
|
} |
940
|
|
|
|
941
|
1108 |
|
$idGen = $class->idGenerator; |
942
|
|
|
|
943
|
1108 |
|
if ( ! $idGen->isPostInsertGenerator()) { |
944
|
289 |
|
$idValue = $idGen->generate($this->em, $entity); |
945
|
|
|
|
946
|
289 |
|
if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) { |
947
|
2 |
|
$idValue = [$class->getSingleIdentifierFieldName() => $this->convertSingleFieldIdentifierToPHPValue($class, $idValue)]; |
948
|
|
|
|
949
|
2 |
|
$class->setIdentifierValues($entity, $idValue); |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
// Some identifiers may be foreign keys to new entities. |
953
|
|
|
// In this case, we don't have the value yet and should treat it as if we have a post-insert generator |
954
|
289 |
|
if (! $this->hasMissingIdsWhichAreForeignKeys($class, $idValue)) { |
955
|
286 |
|
$this->entityIdentifiers[$oid] = $idValue; |
956
|
|
|
} |
957
|
|
|
} |
958
|
|
|
|
959
|
1108 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
960
|
|
|
|
961
|
1108 |
|
$this->scheduleForInsert($entity); |
962
|
1108 |
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* @param mixed[] $idValue |
966
|
|
|
*/ |
967
|
289 |
|
private function hasMissingIdsWhichAreForeignKeys(ClassMetadata $class, array $idValue) : bool |
968
|
|
|
{ |
969
|
289 |
|
foreach ($idValue as $idField => $idFieldValue) { |
970
|
289 |
|
if ($idFieldValue === null && isset($class->associationMappings[$idField])) { |
971
|
289 |
|
return true; |
972
|
|
|
} |
973
|
|
|
} |
974
|
|
|
|
975
|
286 |
|
return false; |
976
|
|
|
} |
977
|
|
|
|
978
|
|
|
/** |
979
|
|
|
* INTERNAL: |
980
|
|
|
* Computes the changeset of an individual entity, independently of the |
981
|
|
|
* computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
982
|
|
|
* |
983
|
|
|
* The passed entity must be a managed entity. If the entity already has a change set |
984
|
|
|
* because this method is invoked during a commit cycle then the change sets are added. |
985
|
|
|
* whereby changes detected in this method prevail. |
986
|
|
|
* |
987
|
|
|
* @ignore |
988
|
|
|
* |
989
|
|
|
* @param ClassMetadata $class The class descriptor of the entity. |
990
|
|
|
* @param object $entity The entity for which to (re)calculate the change set. |
991
|
|
|
* |
992
|
|
|
* @return void |
993
|
|
|
* |
994
|
|
|
* @throws ORMInvalidArgumentException If the passed entity is not MANAGED. |
995
|
|
|
*/ |
996
|
16 |
|
public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity) |
997
|
|
|
{ |
998
|
16 |
|
$oid = spl_object_hash($entity); |
999
|
|
|
|
1000
|
16 |
|
if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) { |
1001
|
|
|
throw ORMInvalidArgumentException::entityNotManaged($entity); |
1002
|
|
|
} |
1003
|
|
|
|
1004
|
|
|
// skip if change tracking is "NOTIFY" |
1005
|
16 |
|
if ($class->isChangeTrackingNotify()) { |
1006
|
|
|
return; |
1007
|
|
|
} |
1008
|
|
|
|
1009
|
16 |
|
if ( ! $class->isInheritanceTypeNone()) { |
1010
|
3 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
16 |
|
$actualData = []; |
1014
|
|
|
|
1015
|
16 |
|
foreach ($class->reflFields as $name => $refProp) { |
1016
|
16 |
|
if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) |
1017
|
16 |
|
&& ($name !== $class->versionField) |
1018
|
16 |
|
&& ! $class->isCollectionValuedAssociation($name)) { |
1019
|
16 |
|
$actualData[$name] = $refProp->getValue($entity); |
1020
|
|
|
} |
1021
|
|
|
} |
1022
|
|
|
|
1023
|
16 |
|
if ( ! isset($this->originalEntityData[$oid])) { |
1024
|
|
|
throw new \RuntimeException('Cannot call recomputeSingleEntityChangeSet before computeChangeSet on an entity.'); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
16 |
|
$originalData = $this->originalEntityData[$oid]; |
1028
|
16 |
|
$changeSet = []; |
1029
|
|
|
|
1030
|
16 |
|
foreach ($actualData as $propName => $actualValue) { |
1031
|
16 |
|
$orgValue = $originalData[$propName] ?? null; |
1032
|
|
|
|
1033
|
16 |
|
if ($orgValue !== $actualValue) { |
1034
|
16 |
|
$changeSet[$propName] = [$orgValue, $actualValue]; |
1035
|
|
|
} |
1036
|
|
|
} |
1037
|
|
|
|
1038
|
16 |
|
if ($changeSet) { |
1039
|
7 |
|
if (isset($this->entityChangeSets[$oid])) { |
1040
|
6 |
|
$this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet); |
1041
|
1 |
|
} else if ( ! isset($this->entityInsertions[$oid])) { |
1042
|
1 |
|
$this->entityChangeSets[$oid] = $changeSet; |
1043
|
1 |
|
$this->entityUpdates[$oid] = $entity; |
1044
|
|
|
} |
1045
|
7 |
|
$this->originalEntityData[$oid] = $actualData; |
1046
|
|
|
} |
1047
|
16 |
|
} |
1048
|
|
|
|
1049
|
|
|
/** |
1050
|
|
|
* Executes all entity insertions for entities of the specified type. |
1051
|
|
|
* |
1052
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $class |
1053
|
|
|
* |
1054
|
|
|
* @return void |
1055
|
|
|
*/ |
1056
|
1075 |
|
private function executeInserts($class) |
1057
|
|
|
{ |
1058
|
1075 |
|
$entities = []; |
1059
|
1075 |
|
$className = $class->name; |
1060
|
1075 |
|
$persister = $this->getEntityPersister($className); |
1061
|
1075 |
|
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postPersist); |
1062
|
|
|
|
1063
|
1075 |
|
$insertionsForClass = []; |
1064
|
|
|
|
1065
|
1075 |
|
foreach ($this->entityInsertions as $oid => $entity) { |
1066
|
|
|
|
1067
|
1075 |
|
if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { |
|
|
|
|
1068
|
908 |
|
continue; |
1069
|
|
|
} |
1070
|
|
|
|
1071
|
1075 |
|
$insertionsForClass[$oid] = $entity; |
1072
|
|
|
|
1073
|
1075 |
|
$persister->addInsert($entity); |
1074
|
|
|
|
1075
|
1075 |
|
unset($this->entityInsertions[$oid]); |
1076
|
|
|
|
1077
|
1075 |
|
if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
1078
|
1075 |
|
$entities[] = $entity; |
1079
|
|
|
} |
1080
|
|
|
} |
1081
|
|
|
|
1082
|
1075 |
|
$postInsertIds = $persister->executeInserts(); |
1083
|
|
|
|
1084
|
1075 |
|
if ($postInsertIds) { |
|
|
|
|
1085
|
|
|
// Persister returned post-insert IDs |
1086
|
975 |
|
foreach ($postInsertIds as $postInsertId) { |
1087
|
975 |
|
$idField = $class->getSingleIdentifierFieldName(); |
1088
|
975 |
|
$idValue = $this->convertSingleFieldIdentifierToPHPValue($class, $postInsertId['generatedId']); |
1089
|
|
|
|
1090
|
975 |
|
$entity = $postInsertId['entity']; |
1091
|
975 |
|
$oid = spl_object_hash($entity); |
1092
|
|
|
|
1093
|
975 |
|
$class->reflFields[$idField]->setValue($entity, $idValue); |
1094
|
|
|
|
1095
|
975 |
|
$this->entityIdentifiers[$oid] = [$idField => $idValue]; |
1096
|
975 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
1097
|
975 |
|
$this->originalEntityData[$oid][$idField] = $idValue; |
1098
|
|
|
|
1099
|
975 |
|
$this->addToIdentityMap($entity); |
1100
|
|
|
} |
1101
|
|
|
} else { |
1102
|
815 |
|
foreach ($insertionsForClass as $oid => $entity) { |
1103
|
276 |
|
if (! isset($this->entityIdentifiers[$oid])) { |
1104
|
|
|
//entity was not added to identity map because some identifiers are foreign keys to new entities. |
1105
|
|
|
//add it now |
1106
|
276 |
|
$this->addToEntityIdentifiersAndEntityMap($class, $oid, $entity); |
1107
|
|
|
} |
1108
|
|
|
} |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
1075 |
|
foreach ($entities as $entity) { |
1112
|
136 |
|
$this->listenersInvoker->invoke($class, Events::postPersist, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
1113
|
|
|
} |
1114
|
1075 |
|
} |
1115
|
|
|
|
1116
|
|
|
/** |
1117
|
|
|
* @param object $entity |
1118
|
|
|
*/ |
1119
|
3 |
|
private function addToEntityIdentifiersAndEntityMap(ClassMetadata $class, string $oid, $entity): void |
1120
|
|
|
{ |
1121
|
3 |
|
$identifier = []; |
1122
|
|
|
|
1123
|
3 |
|
foreach ($class->getIdentifierFieldNames() as $idField) { |
1124
|
3 |
|
$value = $class->getFieldValue($entity, $idField); |
1125
|
|
|
|
1126
|
3 |
|
if (isset($class->associationMappings[$idField])) { |
1127
|
|
|
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. |
1128
|
3 |
|
$value = $this->getSingleIdentifierValue($value); |
1129
|
|
|
} |
1130
|
|
|
|
1131
|
3 |
|
$identifier[$idField] = $this->originalEntityData[$oid][$idField] = $value; |
1132
|
|
|
} |
1133
|
|
|
|
1134
|
3 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
1135
|
3 |
|
$this->entityIdentifiers[$oid] = $identifier; |
1136
|
|
|
|
1137
|
3 |
|
$this->addToIdentityMap($entity); |
1138
|
3 |
|
} |
1139
|
|
|
|
1140
|
|
|
/** |
1141
|
|
|
* Executes all entity updates for entities of the specified type. |
1142
|
|
|
* |
1143
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $class |
1144
|
|
|
* |
1145
|
|
|
* @return void |
1146
|
|
|
*/ |
1147
|
123 |
|
private function executeUpdates($class) |
1148
|
|
|
{ |
1149
|
123 |
|
$className = $class->name; |
1150
|
123 |
|
$persister = $this->getEntityPersister($className); |
1151
|
123 |
|
$preUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preUpdate); |
1152
|
123 |
|
$postUpdateInvoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postUpdate); |
1153
|
|
|
|
1154
|
123 |
|
foreach ($this->entityUpdates as $oid => $entity) { |
1155
|
123 |
|
if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { |
|
|
|
|
1156
|
79 |
|
continue; |
1157
|
|
|
} |
1158
|
|
|
|
1159
|
123 |
|
if ($preUpdateInvoke != ListenersInvoker::INVOKE_NONE) { |
1160
|
13 |
|
$this->listenersInvoker->invoke($class, Events::preUpdate, $entity, new PreUpdateEventArgs($entity, $this->em, $this->getEntityChangeSet($entity)), $preUpdateInvoke); |
1161
|
|
|
|
1162
|
13 |
|
$this->recomputeSingleEntityChangeSet($class, $entity); |
1163
|
|
|
} |
1164
|
|
|
|
1165
|
123 |
|
if ( ! empty($this->entityChangeSets[$oid])) { |
1166
|
89 |
|
$persister->update($entity); |
1167
|
|
|
} |
1168
|
|
|
|
1169
|
119 |
|
unset($this->entityUpdates[$oid]); |
1170
|
|
|
|
1171
|
119 |
|
if ($postUpdateInvoke != ListenersInvoker::INVOKE_NONE) { |
1172
|
119 |
|
$this->listenersInvoker->invoke($class, Events::postUpdate, $entity, new LifecycleEventArgs($entity, $this->em), $postUpdateInvoke); |
1173
|
|
|
} |
1174
|
|
|
} |
1175
|
119 |
|
} |
1176
|
|
|
|
1177
|
|
|
/** |
1178
|
|
|
* Executes all entity deletions for entities of the specified type. |
1179
|
|
|
* |
1180
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadata $class |
1181
|
|
|
* |
1182
|
|
|
* @return void |
1183
|
|
|
*/ |
1184
|
64 |
|
private function executeDeletions($class) |
1185
|
|
|
{ |
1186
|
64 |
|
$className = $class->name; |
1187
|
64 |
|
$persister = $this->getEntityPersister($className); |
1188
|
64 |
|
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::postRemove); |
1189
|
|
|
|
1190
|
64 |
|
foreach ($this->entityDeletions as $oid => $entity) { |
1191
|
64 |
|
if ($this->em->getClassMetadata(get_class($entity))->name !== $className) { |
|
|
|
|
1192
|
26 |
|
continue; |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
64 |
|
$persister->delete($entity); |
1196
|
|
|
|
1197
|
|
|
unset( |
1198
|
64 |
|
$this->entityDeletions[$oid], |
1199
|
64 |
|
$this->entityIdentifiers[$oid], |
1200
|
64 |
|
$this->originalEntityData[$oid], |
1201
|
64 |
|
$this->entityStates[$oid] |
1202
|
|
|
); |
1203
|
|
|
|
1204
|
|
|
// Entity with this $oid after deletion treated as NEW, even if the $oid |
1205
|
|
|
// is obtained by a new entity because the old one went out of scope. |
1206
|
|
|
//$this->entityStates[$oid] = self::STATE_NEW; |
1207
|
64 |
|
if ( ! $class->isIdentifierNatural()) { |
1208
|
53 |
|
$class->reflFields[$class->identifier[0]]->setValue($entity, null); |
1209
|
|
|
} |
1210
|
|
|
|
1211
|
64 |
|
if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
1212
|
64 |
|
$this->listenersInvoker->invoke($class, Events::postRemove, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
1213
|
|
|
} |
1214
|
|
|
} |
1215
|
63 |
|
} |
1216
|
|
|
|
1217
|
|
|
/** |
1218
|
|
|
* Gets the commit order. |
1219
|
|
|
* |
1220
|
|
|
* @param array|null $entityChangeSet |
1221
|
|
|
* |
1222
|
|
|
* @return array |
1223
|
|
|
*/ |
1224
|
1079 |
|
private function getCommitOrder(array $entityChangeSet = null) |
1225
|
|
|
{ |
1226
|
1079 |
|
if ($entityChangeSet === null) { |
1227
|
1079 |
|
$entityChangeSet = array_merge($this->entityInsertions, $this->entityUpdates, $this->entityDeletions); |
1228
|
|
|
} |
1229
|
|
|
|
1230
|
1079 |
|
$calc = $this->getCommitOrderCalculator(); |
1231
|
|
|
|
1232
|
|
|
// See if there are any new classes in the changeset, that are not in the |
1233
|
|
|
// commit order graph yet (don't have a node). |
1234
|
|
|
// We have to inspect changeSet to be able to correctly build dependencies. |
1235
|
|
|
// It is not possible to use IdentityMap here because post inserted ids |
1236
|
|
|
// are not yet available. |
1237
|
1079 |
|
$newNodes = []; |
1238
|
|
|
|
1239
|
1079 |
|
foreach ($entityChangeSet as $entity) { |
1240
|
1079 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1241
|
|
|
|
1242
|
1079 |
|
if ($calc->hasNode($class->name)) { |
|
|
|
|
1243
|
658 |
|
continue; |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
1079 |
|
$calc->addNode($class->name, $class); |
1247
|
|
|
|
1248
|
1079 |
|
$newNodes[] = $class; |
1249
|
|
|
} |
1250
|
|
|
|
1251
|
|
|
// Calculate dependencies for new nodes |
1252
|
1079 |
|
while ($class = array_pop($newNodes)) { |
1253
|
1079 |
|
foreach ($class->associationMappings as $assoc) { |
1254
|
934 |
|
if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { |
1255
|
886 |
|
continue; |
1256
|
|
|
} |
1257
|
|
|
|
1258
|
884 |
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
1259
|
|
|
|
1260
|
884 |
|
if ( ! $calc->hasNode($targetClass->name)) { |
1261
|
682 |
|
$calc->addNode($targetClass->name, $targetClass); |
1262
|
|
|
|
1263
|
682 |
|
$newNodes[] = $targetClass; |
1264
|
|
|
} |
1265
|
|
|
|
1266
|
884 |
|
$joinColumns = reset($assoc['joinColumns']); |
1267
|
|
|
|
1268
|
884 |
|
$calc->addDependency($targetClass->name, $class->name, (int)empty($joinColumns['nullable'])); |
1269
|
|
|
|
1270
|
|
|
// If the target class has mapped subclasses, these share the same dependency. |
1271
|
884 |
|
if ( ! $targetClass->subClasses) { |
|
|
|
|
1272
|
877 |
|
continue; |
1273
|
|
|
} |
1274
|
|
|
|
1275
|
238 |
|
foreach ($targetClass->subClasses as $subClassName) { |
1276
|
238 |
|
$targetSubClass = $this->em->getClassMetadata($subClassName); |
1277
|
|
|
|
1278
|
238 |
|
if ( ! $calc->hasNode($subClassName)) { |
1279
|
208 |
|
$calc->addNode($targetSubClass->name, $targetSubClass); |
1280
|
|
|
|
1281
|
208 |
|
$newNodes[] = $targetSubClass; |
1282
|
|
|
} |
1283
|
|
|
|
1284
|
238 |
|
$calc->addDependency($targetSubClass->name, $class->name, 1); |
1285
|
|
|
} |
1286
|
|
|
} |
1287
|
|
|
} |
1288
|
|
|
|
1289
|
1079 |
|
return $calc->sort(); |
1290
|
|
|
} |
1291
|
|
|
|
1292
|
|
|
/** |
1293
|
|
|
* Schedules an entity for insertion into the database. |
1294
|
|
|
* If the entity already has an identifier, it will be added to the identity map. |
1295
|
|
|
* |
1296
|
|
|
* @param object $entity The entity to schedule for insertion. |
1297
|
|
|
* |
1298
|
|
|
* @return void |
1299
|
|
|
* |
1300
|
|
|
* @throws ORMInvalidArgumentException |
1301
|
|
|
* @throws \InvalidArgumentException |
1302
|
|
|
*/ |
1303
|
1109 |
|
public function scheduleForInsert($entity) |
1304
|
|
|
{ |
1305
|
1109 |
|
$oid = spl_object_hash($entity); |
1306
|
|
|
|
1307
|
1109 |
|
if (isset($this->entityUpdates[$oid])) { |
1308
|
|
|
throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion."); |
1309
|
|
|
} |
1310
|
|
|
|
1311
|
1109 |
|
if (isset($this->entityDeletions[$oid])) { |
1312
|
1 |
|
throw ORMInvalidArgumentException::scheduleInsertForRemovedEntity($entity); |
1313
|
|
|
} |
1314
|
1109 |
|
if (isset($this->originalEntityData[$oid]) && ! isset($this->entityInsertions[$oid])) { |
1315
|
1 |
|
throw ORMInvalidArgumentException::scheduleInsertForManagedEntity($entity); |
1316
|
|
|
} |
1317
|
|
|
|
1318
|
1109 |
|
if (isset($this->entityInsertions[$oid])) { |
1319
|
1 |
|
throw ORMInvalidArgumentException::scheduleInsertTwice($entity); |
1320
|
|
|
} |
1321
|
|
|
|
1322
|
1109 |
|
$this->entityInsertions[$oid] = $entity; |
1323
|
|
|
|
1324
|
1109 |
|
if (isset($this->entityIdentifiers[$oid])) { |
1325
|
286 |
|
$this->addToIdentityMap($entity); |
1326
|
|
|
} |
1327
|
|
|
|
1328
|
1109 |
|
if ($entity instanceof NotifyPropertyChanged) { |
1329
|
8 |
|
$entity->addPropertyChangedListener($this); |
1330
|
|
|
} |
1331
|
1109 |
|
} |
1332
|
|
|
|
1333
|
|
|
/** |
1334
|
|
|
* Checks whether an entity is scheduled for insertion. |
1335
|
|
|
* |
1336
|
|
|
* @param object $entity |
1337
|
|
|
* |
1338
|
|
|
* @return boolean |
1339
|
|
|
*/ |
1340
|
656 |
|
public function isScheduledForInsert($entity) |
1341
|
|
|
{ |
1342
|
656 |
|
return isset($this->entityInsertions[spl_object_hash($entity)]); |
1343
|
|
|
} |
1344
|
|
|
|
1345
|
|
|
/** |
1346
|
|
|
* Schedules an entity for being updated. |
1347
|
|
|
* |
1348
|
|
|
* @param object $entity The entity to schedule for being updated. |
1349
|
|
|
* |
1350
|
|
|
* @return void |
1351
|
|
|
* |
1352
|
|
|
* @throws ORMInvalidArgumentException |
1353
|
|
|
*/ |
1354
|
1 |
|
public function scheduleForUpdate($entity) |
1355
|
|
|
{ |
1356
|
1 |
|
$oid = spl_object_hash($entity); |
1357
|
|
|
|
1358
|
1 |
|
if ( ! isset($this->entityIdentifiers[$oid])) { |
1359
|
|
|
throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update"); |
1360
|
|
|
} |
1361
|
|
|
|
1362
|
1 |
|
if (isset($this->entityDeletions[$oid])) { |
1363
|
|
|
throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update"); |
1364
|
|
|
} |
1365
|
|
|
|
1366
|
1 |
|
if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) { |
1367
|
1 |
|
$this->entityUpdates[$oid] = $entity; |
1368
|
|
|
} |
1369
|
1 |
|
} |
1370
|
|
|
|
1371
|
|
|
/** |
1372
|
|
|
* INTERNAL: |
1373
|
|
|
* Schedules an extra update that will be executed immediately after the |
1374
|
|
|
* regular entity updates within the currently running commit cycle. |
1375
|
|
|
* |
1376
|
|
|
* Extra updates for entities are stored as (entity, changeset) tuples. |
1377
|
|
|
* |
1378
|
|
|
* @ignore |
1379
|
|
|
* |
1380
|
|
|
* @param object $entity The entity for which to schedule an extra update. |
1381
|
|
|
* @param array $changeset The changeset of the entity (what to update). |
1382
|
|
|
* |
1383
|
|
|
* @return void |
1384
|
|
|
*/ |
1385
|
44 |
|
public function scheduleExtraUpdate($entity, array $changeset) |
1386
|
|
|
{ |
1387
|
44 |
|
$oid = spl_object_hash($entity); |
1388
|
44 |
|
$extraUpdate = [$entity, $changeset]; |
1389
|
|
|
|
1390
|
44 |
|
if (isset($this->extraUpdates[$oid])) { |
1391
|
1 |
|
list(, $changeset2) = $this->extraUpdates[$oid]; |
1392
|
|
|
|
1393
|
1 |
|
$extraUpdate = [$entity, $changeset + $changeset2]; |
1394
|
|
|
} |
1395
|
|
|
|
1396
|
44 |
|
$this->extraUpdates[$oid] = $extraUpdate; |
1397
|
44 |
|
} |
1398
|
|
|
|
1399
|
|
|
/** |
1400
|
|
|
* Checks whether an entity is registered as dirty in the unit of work. |
1401
|
|
|
* Note: Is not very useful currently as dirty entities are only registered |
1402
|
|
|
* at commit time. |
1403
|
|
|
* |
1404
|
|
|
* @param object $entity |
1405
|
|
|
* |
1406
|
|
|
* @return boolean |
1407
|
|
|
*/ |
1408
|
|
|
public function isScheduledForUpdate($entity) |
1409
|
|
|
{ |
1410
|
|
|
return isset($this->entityUpdates[spl_object_hash($entity)]); |
1411
|
|
|
} |
1412
|
|
|
|
1413
|
|
|
/** |
1414
|
|
|
* Checks whether an entity is registered to be checked in the unit of work. |
1415
|
|
|
* |
1416
|
|
|
* @param object $entity |
1417
|
|
|
* |
1418
|
|
|
* @return boolean |
1419
|
|
|
*/ |
1420
|
2 |
|
public function isScheduledForDirtyCheck($entity) |
1421
|
|
|
{ |
1422
|
2 |
|
$rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName; |
|
|
|
|
1423
|
|
|
|
1424
|
2 |
|
return isset($this->scheduledForSynchronization[$rootEntityName][spl_object_hash($entity)]); |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
/** |
1428
|
|
|
* INTERNAL: |
1429
|
|
|
* Schedules an entity for deletion. |
1430
|
|
|
* |
1431
|
|
|
* @param object $entity |
1432
|
|
|
* |
1433
|
|
|
* @return void |
1434
|
|
|
*/ |
1435
|
67 |
|
public function scheduleForDelete($entity) |
1436
|
|
|
{ |
1437
|
67 |
|
$oid = spl_object_hash($entity); |
1438
|
|
|
|
1439
|
67 |
|
if (isset($this->entityInsertions[$oid])) { |
1440
|
1 |
|
if ($this->isInIdentityMap($entity)) { |
1441
|
|
|
$this->removeFromIdentityMap($entity); |
1442
|
|
|
} |
1443
|
|
|
|
1444
|
1 |
|
unset($this->entityInsertions[$oid], $this->entityStates[$oid]); |
1445
|
|
|
|
1446
|
1 |
|
return; // entity has not been persisted yet, so nothing more to do. |
1447
|
|
|
} |
1448
|
|
|
|
1449
|
67 |
|
if ( ! $this->isInIdentityMap($entity)) { |
1450
|
1 |
|
return; |
1451
|
|
|
} |
1452
|
|
|
|
1453
|
66 |
|
$this->removeFromIdentityMap($entity); |
1454
|
|
|
|
1455
|
66 |
|
unset($this->entityUpdates[$oid]); |
1456
|
|
|
|
1457
|
66 |
|
if ( ! isset($this->entityDeletions[$oid])) { |
1458
|
66 |
|
$this->entityDeletions[$oid] = $entity; |
1459
|
66 |
|
$this->entityStates[$oid] = self::STATE_REMOVED; |
1460
|
|
|
} |
1461
|
66 |
|
} |
1462
|
|
|
|
1463
|
|
|
/** |
1464
|
|
|
* Checks whether an entity is registered as removed/deleted with the unit |
1465
|
|
|
* of work. |
1466
|
|
|
* |
1467
|
|
|
* @param object $entity |
1468
|
|
|
* |
1469
|
|
|
* @return boolean |
1470
|
|
|
*/ |
1471
|
17 |
|
public function isScheduledForDelete($entity) |
1472
|
|
|
{ |
1473
|
17 |
|
return isset($this->entityDeletions[spl_object_hash($entity)]); |
1474
|
|
|
} |
1475
|
|
|
|
1476
|
|
|
/** |
1477
|
|
|
* Checks whether an entity is scheduled for insertion, update or deletion. |
1478
|
|
|
* |
1479
|
|
|
* @param object $entity |
1480
|
|
|
* |
1481
|
|
|
* @return boolean |
1482
|
|
|
*/ |
1483
|
|
|
public function isEntityScheduled($entity) |
1484
|
|
|
{ |
1485
|
|
|
$oid = spl_object_hash($entity); |
1486
|
|
|
|
1487
|
|
|
return isset($this->entityInsertions[$oid]) |
1488
|
|
|
|| isset($this->entityUpdates[$oid]) |
1489
|
|
|
|| isset($this->entityDeletions[$oid]); |
1490
|
|
|
} |
1491
|
|
|
|
1492
|
|
|
/** |
1493
|
|
|
* INTERNAL: |
1494
|
|
|
* Registers an entity in the identity map. |
1495
|
|
|
* Note that entities in a hierarchy are registered with the class name of |
1496
|
|
|
* the root entity. |
1497
|
|
|
* |
1498
|
|
|
* @ignore |
1499
|
|
|
* |
1500
|
|
|
* @param object $entity The entity to register. |
1501
|
|
|
* |
1502
|
|
|
* @return boolean TRUE if the registration was successful, FALSE if the identity of |
1503
|
|
|
* the entity in question is already managed. |
1504
|
|
|
* |
1505
|
|
|
* @throws ORMInvalidArgumentException |
1506
|
|
|
*/ |
1507
|
1173 |
|
public function addToIdentityMap($entity) |
1508
|
|
|
{ |
1509
|
1173 |
|
$classMetadata = $this->em->getClassMetadata(get_class($entity)); |
1510
|
1173 |
|
$oid = spl_object_hash($entity); |
1511
|
1173 |
|
$identifier = $this->entityIdentifiers[$oid]; |
1512
|
|
|
|
1513
|
1173 |
|
if (empty($identifier) || in_array(null, $identifier, true)) { |
1514
|
6 |
|
throw ORMInvalidArgumentException::entityWithoutIdentity($classMetadata->name, $entity); |
|
|
|
|
1515
|
|
|
} |
1516
|
|
|
|
1517
|
1167 |
|
$idHash = implode(' ', $identifier); |
1518
|
1167 |
|
$className = $classMetadata->rootEntityName; |
|
|
|
|
1519
|
|
|
|
1520
|
1167 |
|
$this->oidMap[$oid] = $entity; |
1521
|
1167 |
|
if (isset($this->identityMap[$className][$idHash])) { |
1522
|
87 |
|
return false; |
1523
|
|
|
} |
1524
|
|
|
|
1525
|
1167 |
|
$this->identityMap[$className][$idHash] = $entity; |
1526
|
|
|
|
1527
|
1167 |
|
return true; |
1528
|
|
|
} |
1529
|
|
|
|
1530
|
|
|
/** |
1531
|
|
|
* Gets the state of an entity with regard to the current unit of work. |
1532
|
|
|
* |
1533
|
|
|
* @param object $entity |
1534
|
|
|
* @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
1535
|
|
|
* This parameter can be set to improve performance of entity state detection |
1536
|
|
|
* by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
1537
|
|
|
* is either known or does not matter for the caller of the method. |
1538
|
|
|
* |
1539
|
|
|
* @return int The entity state. |
1540
|
|
|
*/ |
1541
|
1123 |
|
public function getEntityState($entity, $assume = null) |
1542
|
|
|
{ |
1543
|
1123 |
|
$oid = spl_object_hash($entity); |
1544
|
|
|
|
1545
|
1123 |
|
if (isset($this->entityStates[$oid])) { |
1546
|
818 |
|
return $this->entityStates[$oid]; |
1547
|
|
|
} |
1548
|
|
|
|
1549
|
1117 |
|
if ($assume !== null) { |
1550
|
1113 |
|
return $assume; |
1551
|
|
|
} |
1552
|
|
|
|
1553
|
|
|
// State can only be NEW or DETACHED, because MANAGED/REMOVED states are known. |
1554
|
|
|
// Note that you can not remember the NEW or DETACHED state in _entityStates since |
1555
|
|
|
// the UoW does not hold references to such objects and the object hash can be reused. |
1556
|
|
|
// More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it. |
1557
|
14 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1558
|
14 |
|
$id = $class->getIdentifierValues($entity); |
1559
|
|
|
|
1560
|
14 |
|
if ( ! $id) { |
|
|
|
|
1561
|
5 |
|
return self::STATE_NEW; |
1562
|
|
|
} |
1563
|
|
|
|
1564
|
11 |
|
if ($class->containsForeignIdentifier) { |
|
|
|
|
1565
|
1 |
|
$id = $this->identifierFlattener->flattenIdentifier($class, $id); |
1566
|
|
|
} |
1567
|
|
|
|
1568
|
|
|
switch (true) { |
1569
|
11 |
|
case ($class->isIdentifierNatural()): |
|
|
|
|
1570
|
|
|
// Check for a version field, if available, to avoid a db lookup. |
1571
|
5 |
|
if ($class->isVersioned) { |
|
|
|
|
1572
|
1 |
|
return ($class->getFieldValue($entity, $class->versionField)) |
|
|
|
|
1573
|
|
|
? self::STATE_DETACHED |
1574
|
1 |
|
: self::STATE_NEW; |
1575
|
|
|
} |
1576
|
|
|
|
1577
|
|
|
// Last try before db lookup: check the identity map. |
1578
|
4 |
|
if ($this->tryGetById($id, $class->rootEntityName)) { |
|
|
|
|
1579
|
1 |
|
return self::STATE_DETACHED; |
1580
|
|
|
} |
1581
|
|
|
|
1582
|
|
|
// db lookup |
1583
|
4 |
|
if ($this->getEntityPersister($class->name)->exists($entity)) { |
|
|
|
|
1584
|
|
|
return self::STATE_DETACHED; |
1585
|
|
|
} |
1586
|
|
|
|
1587
|
4 |
|
return self::STATE_NEW; |
1588
|
|
|
|
1589
|
6 |
|
case ( ! $class->idGenerator->isPostInsertGenerator()): |
|
|
|
|
1590
|
|
|
// if we have a pre insert generator we can't be sure that having an id |
1591
|
|
|
// really means that the entity exists. We have to verify this through |
1592
|
|
|
// the last resort: a db lookup |
1593
|
|
|
|
1594
|
|
|
// Last try before db lookup: check the identity map. |
1595
|
|
|
if ($this->tryGetById($id, $class->rootEntityName)) { |
1596
|
|
|
return self::STATE_DETACHED; |
1597
|
|
|
} |
1598
|
|
|
|
1599
|
|
|
// db lookup |
1600
|
|
|
if ($this->getEntityPersister($class->name)->exists($entity)) { |
1601
|
|
|
return self::STATE_DETACHED; |
1602
|
|
|
} |
1603
|
|
|
|
1604
|
|
|
return self::STATE_NEW; |
1605
|
|
|
|
1606
|
|
|
default: |
1607
|
6 |
|
return self::STATE_DETACHED; |
1608
|
|
|
} |
1609
|
|
|
} |
1610
|
|
|
|
1611
|
|
|
/** |
1612
|
|
|
* INTERNAL: |
1613
|
|
|
* Removes an entity from the identity map. This effectively detaches the |
1614
|
|
|
* entity from the persistence management of Doctrine. |
1615
|
|
|
* |
1616
|
|
|
* @ignore |
1617
|
|
|
* |
1618
|
|
|
* @param object $entity |
1619
|
|
|
* |
1620
|
|
|
* @return boolean |
1621
|
|
|
* |
1622
|
|
|
* @throws ORMInvalidArgumentException |
1623
|
|
|
*/ |
1624
|
79 |
|
public function removeFromIdentityMap($entity) |
1625
|
|
|
{ |
1626
|
79 |
|
$oid = spl_object_hash($entity); |
1627
|
79 |
|
$classMetadata = $this->em->getClassMetadata(get_class($entity)); |
1628
|
79 |
|
$idHash = implode(' ', $this->entityIdentifiers[$oid]); |
1629
|
|
|
|
1630
|
79 |
|
if ($idHash === '') { |
1631
|
|
|
throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "remove from identity map"); |
1632
|
|
|
} |
1633
|
|
|
|
1634
|
79 |
|
$className = $classMetadata->rootEntityName; |
|
|
|
|
1635
|
|
|
|
1636
|
79 |
|
unset($this->oidMap[$oid]); |
1637
|
79 |
|
if (isset($this->identityMap[$className][$idHash])) { |
1638
|
79 |
|
unset($this->identityMap[$className][$idHash]); |
1639
|
79 |
|
unset($this->readOnlyObjects[$oid]); |
1640
|
|
|
|
1641
|
|
|
//$this->entityStates[$oid] = self::STATE_DETACHED; |
1642
|
|
|
|
1643
|
79 |
|
return true; |
1644
|
|
|
} |
1645
|
|
|
|
1646
|
|
|
return false; |
1647
|
|
|
} |
1648
|
|
|
|
1649
|
|
|
/** |
1650
|
|
|
* INTERNAL: |
1651
|
|
|
* Gets an entity in the identity map by its identifier hash. |
1652
|
|
|
* |
1653
|
|
|
* @ignore |
1654
|
|
|
* |
1655
|
|
|
* @param string $idHash |
1656
|
|
|
* @param string $rootClassName |
1657
|
|
|
* |
1658
|
|
|
* @return object |
1659
|
|
|
*/ |
1660
|
6 |
|
public function getByIdHash($idHash, $rootClassName) |
1661
|
|
|
{ |
1662
|
6 |
|
return $this->identityMap[$rootClassName][$idHash]; |
1663
|
|
|
} |
1664
|
|
|
|
1665
|
|
|
/** |
1666
|
|
|
* INTERNAL: |
1667
|
|
|
* Tries to get an entity by its identifier hash. If no entity is found for |
1668
|
|
|
* the given hash, FALSE is returned. |
1669
|
|
|
* |
1670
|
|
|
* @ignore |
1671
|
|
|
* |
1672
|
|
|
* @param mixed $idHash (must be possible to cast it to string) |
1673
|
|
|
* @param string $rootClassName |
1674
|
|
|
* |
1675
|
|
|
* @return object|bool The found entity or FALSE. |
1676
|
|
|
*/ |
1677
|
35 |
|
public function tryGetByIdHash($idHash, $rootClassName) |
1678
|
|
|
{ |
1679
|
35 |
|
$stringIdHash = (string) $idHash; |
1680
|
|
|
|
1681
|
35 |
|
return isset($this->identityMap[$rootClassName][$stringIdHash]) |
1682
|
35 |
|
? $this->identityMap[$rootClassName][$stringIdHash] |
1683
|
35 |
|
: false; |
1684
|
|
|
} |
1685
|
|
|
|
1686
|
|
|
/** |
1687
|
|
|
* Checks whether an entity is registered in the identity map of this UnitOfWork. |
1688
|
|
|
* |
1689
|
|
|
* @param object $entity |
1690
|
|
|
* |
1691
|
|
|
* @return boolean |
1692
|
|
|
*/ |
1693
|
225 |
|
public function isInIdentityMap($entity) |
1694
|
|
|
{ |
1695
|
225 |
|
$oid = spl_object_hash($entity); |
1696
|
|
|
|
1697
|
225 |
|
if (empty($this->entityIdentifiers[$oid])) { |
1698
|
37 |
|
return false; |
1699
|
|
|
} |
1700
|
|
|
|
1701
|
208 |
|
$classMetadata = $this->em->getClassMetadata(get_class($entity)); |
1702
|
208 |
|
$idHash = implode(' ', $this->entityIdentifiers[$oid]); |
1703
|
|
|
|
1704
|
208 |
|
return isset($this->identityMap[$classMetadata->rootEntityName][$idHash]); |
|
|
|
|
1705
|
|
|
} |
1706
|
|
|
|
1707
|
|
|
/** |
1708
|
|
|
* INTERNAL: |
1709
|
|
|
* Checks whether an identifier hash exists in the identity map. |
1710
|
|
|
* |
1711
|
|
|
* @ignore |
1712
|
|
|
* |
1713
|
|
|
* @param string $idHash |
1714
|
|
|
* @param string $rootClassName |
1715
|
|
|
* |
1716
|
|
|
* @return boolean |
1717
|
|
|
*/ |
1718
|
|
|
public function containsIdHash($idHash, $rootClassName) |
1719
|
|
|
{ |
1720
|
|
|
return isset($this->identityMap[$rootClassName][$idHash]); |
1721
|
|
|
} |
1722
|
|
|
|
1723
|
|
|
/** |
1724
|
|
|
* Persists an entity as part of the current unit of work. |
1725
|
|
|
* |
1726
|
|
|
* @param object $entity The entity to persist. |
1727
|
|
|
* |
1728
|
|
|
* @return void |
1729
|
|
|
*/ |
1730
|
1104 |
|
public function persist($entity) |
1731
|
|
|
{ |
1732
|
1104 |
|
$visited = []; |
1733
|
|
|
|
1734
|
1104 |
|
$this->doPersist($entity, $visited); |
1735
|
1097 |
|
} |
1736
|
|
|
|
1737
|
|
|
/** |
1738
|
|
|
* Persists an entity as part of the current unit of work. |
1739
|
|
|
* |
1740
|
|
|
* This method is internally called during persist() cascades as it tracks |
1741
|
|
|
* the already visited entities to prevent infinite recursions. |
1742
|
|
|
* |
1743
|
|
|
* @param object $entity The entity to persist. |
1744
|
|
|
* @param array $visited The already visited entities. |
1745
|
|
|
* |
1746
|
|
|
* @return void |
1747
|
|
|
* |
1748
|
|
|
* @throws ORMInvalidArgumentException |
1749
|
|
|
* @throws UnexpectedValueException |
1750
|
|
|
*/ |
1751
|
1104 |
|
private function doPersist($entity, array &$visited) |
1752
|
|
|
{ |
1753
|
1104 |
|
$oid = spl_object_hash($entity); |
1754
|
|
|
|
1755
|
1104 |
|
if (isset($visited[$oid])) { |
1756
|
110 |
|
return; // Prevent infinite recursion |
1757
|
|
|
} |
1758
|
|
|
|
1759
|
1104 |
|
$visited[$oid] = $entity; // Mark visited |
1760
|
|
|
|
1761
|
1104 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1762
|
|
|
|
1763
|
|
|
// We assume NEW, so DETACHED entities result in an exception on flush (constraint violation). |
1764
|
|
|
// If we would detect DETACHED here we would throw an exception anyway with the same |
1765
|
|
|
// consequences (not recoverable/programming error), so just assuming NEW here |
1766
|
|
|
// lets us avoid some database lookups for entities with natural identifiers. |
1767
|
1104 |
|
$entityState = $this->getEntityState($entity, self::STATE_NEW); |
1768
|
|
|
|
1769
|
|
|
switch ($entityState) { |
1770
|
1104 |
|
case self::STATE_MANAGED: |
1771
|
|
|
// Nothing to do, except if policy is "deferred explicit" |
1772
|
239 |
|
if ($class->isChangeTrackingDeferredExplicit()) { |
1773
|
2 |
|
$this->scheduleForDirtyCheck($entity); |
1774
|
|
|
} |
1775
|
239 |
|
break; |
1776
|
|
|
|
1777
|
1104 |
|
case self::STATE_NEW: |
1778
|
1103 |
|
$this->persistNew($class, $entity); |
1779
|
1103 |
|
break; |
1780
|
|
|
|
1781
|
1 |
|
case self::STATE_REMOVED: |
1782
|
|
|
// Entity becomes managed again |
1783
|
1 |
|
unset($this->entityDeletions[$oid]); |
1784
|
1 |
|
$this->addToIdentityMap($entity); |
1785
|
|
|
|
1786
|
1 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
1787
|
1 |
|
break; |
1788
|
|
|
|
1789
|
|
|
case self::STATE_DETACHED: |
1790
|
|
|
// Can actually not happen right now since we assume STATE_NEW. |
1791
|
|
|
throw ORMInvalidArgumentException::detachedEntityCannot($entity, "persisted"); |
1792
|
|
|
|
1793
|
|
|
default: |
1794
|
|
|
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); |
1795
|
|
|
} |
1796
|
|
|
|
1797
|
1104 |
|
$this->cascadePersist($entity, $visited); |
1798
|
1097 |
|
} |
1799
|
|
|
|
1800
|
|
|
/** |
1801
|
|
|
* Deletes an entity as part of the current unit of work. |
1802
|
|
|
* |
1803
|
|
|
* @param object $entity The entity to remove. |
1804
|
|
|
* |
1805
|
|
|
* @return void |
1806
|
|
|
*/ |
1807
|
66 |
|
public function remove($entity) |
1808
|
|
|
{ |
1809
|
66 |
|
$visited = []; |
1810
|
|
|
|
1811
|
66 |
|
$this->doRemove($entity, $visited); |
1812
|
66 |
|
} |
1813
|
|
|
|
1814
|
|
|
/** |
1815
|
|
|
* Deletes an entity as part of the current unit of work. |
1816
|
|
|
* |
1817
|
|
|
* This method is internally called during delete() cascades as it tracks |
1818
|
|
|
* the already visited entities to prevent infinite recursions. |
1819
|
|
|
* |
1820
|
|
|
* @param object $entity The entity to delete. |
1821
|
|
|
* @param array $visited The map of the already visited entities. |
1822
|
|
|
* |
1823
|
|
|
* @return void |
1824
|
|
|
* |
1825
|
|
|
* @throws ORMInvalidArgumentException If the instance is a detached entity. |
1826
|
|
|
* @throws UnexpectedValueException |
1827
|
|
|
*/ |
1828
|
66 |
|
private function doRemove($entity, array &$visited) |
1829
|
|
|
{ |
1830
|
66 |
|
$oid = spl_object_hash($entity); |
1831
|
|
|
|
1832
|
66 |
|
if (isset($visited[$oid])) { |
1833
|
1 |
|
return; // Prevent infinite recursion |
1834
|
|
|
} |
1835
|
|
|
|
1836
|
66 |
|
$visited[$oid] = $entity; // mark visited |
1837
|
|
|
|
1838
|
|
|
// Cascade first, because scheduleForDelete() removes the entity from the identity map, which |
1839
|
|
|
// can cause problems when a lazy proxy has to be initialized for the cascade operation. |
1840
|
66 |
|
$this->cascadeRemove($entity, $visited); |
1841
|
|
|
|
1842
|
66 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1843
|
66 |
|
$entityState = $this->getEntityState($entity); |
1844
|
|
|
|
1845
|
|
|
switch ($entityState) { |
1846
|
66 |
|
case self::STATE_NEW: |
1847
|
66 |
|
case self::STATE_REMOVED: |
1848
|
|
|
// nothing to do |
1849
|
2 |
|
break; |
1850
|
|
|
|
1851
|
66 |
|
case self::STATE_MANAGED: |
1852
|
66 |
|
$invoke = $this->listenersInvoker->getSubscribedSystems($class, Events::preRemove); |
1853
|
|
|
|
1854
|
66 |
|
if ($invoke !== ListenersInvoker::INVOKE_NONE) { |
1855
|
8 |
|
$this->listenersInvoker->invoke($class, Events::preRemove, $entity, new LifecycleEventArgs($entity, $this->em), $invoke); |
1856
|
|
|
} |
1857
|
|
|
|
1858
|
66 |
|
$this->scheduleForDelete($entity); |
1859
|
66 |
|
break; |
1860
|
|
|
|
1861
|
|
|
case self::STATE_DETACHED: |
1862
|
|
|
throw ORMInvalidArgumentException::detachedEntityCannot($entity, "removed"); |
1863
|
|
|
default: |
1864
|
|
|
throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); |
1865
|
|
|
} |
1866
|
|
|
|
1867
|
66 |
|
} |
1868
|
|
|
|
1869
|
|
|
/** |
1870
|
|
|
* Merges the state of the given detached entity into this UnitOfWork. |
1871
|
|
|
* |
1872
|
|
|
* @param object $entity |
1873
|
|
|
* |
1874
|
|
|
* @return object The managed copy of the entity. |
1875
|
|
|
* |
1876
|
|
|
* @throws OptimisticLockException If the entity uses optimistic locking through a version |
1877
|
|
|
* attribute and the version check against the managed copy fails. |
1878
|
|
|
* |
1879
|
|
|
* @todo Require active transaction!? OptimisticLockException may result in undefined state!? |
1880
|
|
|
*/ |
1881
|
44 |
|
public function merge($entity) |
1882
|
|
|
{ |
1883
|
44 |
|
$visited = []; |
1884
|
|
|
|
1885
|
44 |
|
return $this->doMerge($entity, $visited); |
1886
|
|
|
} |
1887
|
|
|
|
1888
|
|
|
/** |
1889
|
|
|
* Executes a merge operation on an entity. |
1890
|
|
|
* |
1891
|
|
|
* @param object $entity |
1892
|
|
|
* @param array $visited |
1893
|
|
|
* @param object|null $prevManagedCopy |
1894
|
|
|
* @param array|null $assoc |
1895
|
|
|
* |
1896
|
|
|
* @return object The managed copy of the entity. |
1897
|
|
|
* |
1898
|
|
|
* @throws OptimisticLockException If the entity uses optimistic locking through a version |
1899
|
|
|
* attribute and the version check against the managed copy fails. |
1900
|
|
|
* @throws ORMInvalidArgumentException If the entity instance is NEW. |
1901
|
|
|
* @throws EntityNotFoundException if an assigned identifier is used in the entity, but none is provided |
1902
|
|
|
*/ |
1903
|
44 |
|
private function doMerge($entity, array &$visited, $prevManagedCopy = null, array $assoc = []) |
1904
|
|
|
{ |
1905
|
44 |
|
$oid = spl_object_hash($entity); |
1906
|
|
|
|
1907
|
44 |
|
if (isset($visited[$oid])) { |
1908
|
4 |
|
$managedCopy = $visited[$oid]; |
1909
|
|
|
|
1910
|
4 |
|
if ($prevManagedCopy !== null) { |
1911
|
4 |
|
$this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy); |
1912
|
|
|
} |
1913
|
|
|
|
1914
|
4 |
|
return $managedCopy; |
1915
|
|
|
} |
1916
|
|
|
|
1917
|
44 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
1918
|
|
|
|
1919
|
|
|
// First we assume DETACHED, although it can still be NEW but we can avoid |
1920
|
|
|
// an extra db-roundtrip this way. If it is not MANAGED but has an identity, |
1921
|
|
|
// we need to fetch it from the db anyway in order to merge. |
1922
|
|
|
// MANAGED entities are ignored by the merge operation. |
1923
|
44 |
|
$managedCopy = $entity; |
1924
|
|
|
|
1925
|
44 |
|
if ($this->getEntityState($entity, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
1926
|
|
|
// Try to look the entity up in the identity map. |
1927
|
43 |
|
$id = $class->getIdentifierValues($entity); |
1928
|
|
|
|
1929
|
|
|
// If there is no ID, it is actually NEW. |
1930
|
43 |
|
if ( ! $id) { |
|
|
|
|
1931
|
6 |
|
$managedCopy = $this->newInstance($class); |
1932
|
|
|
|
1933
|
6 |
|
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); |
1934
|
6 |
|
$this->persistNew($class, $managedCopy); |
1935
|
|
|
} else { |
1936
|
38 |
|
$flatId = ($class->containsForeignIdentifier) |
|
|
|
|
1937
|
3 |
|
? $this->identifierFlattener->flattenIdentifier($class, $id) |
1938
|
38 |
|
: $id; |
1939
|
|
|
|
1940
|
38 |
|
$managedCopy = $this->tryGetById($flatId, $class->rootEntityName); |
|
|
|
|
1941
|
|
|
|
1942
|
38 |
|
if ($managedCopy) { |
1943
|
|
|
// We have the entity in-memory already, just make sure its not removed. |
1944
|
15 |
|
if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) { |
|
|
|
|
1945
|
15 |
|
throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge"); |
|
|
|
|
1946
|
|
|
} |
1947
|
|
|
} else { |
1948
|
|
|
// We need to fetch the managed copy in order to merge. |
1949
|
26 |
|
$managedCopy = $this->em->find($class->name, $flatId); |
|
|
|
|
1950
|
|
|
} |
1951
|
|
|
|
1952
|
38 |
|
if ($managedCopy === null) { |
1953
|
|
|
// If the identifier is ASSIGNED, it is NEW, otherwise an error |
1954
|
|
|
// since the managed entity was not found. |
1955
|
3 |
|
if ( ! $class->isIdentifierNatural()) { |
1956
|
1 |
|
throw EntityNotFoundException::fromClassNameAndIdentifier( |
1957
|
1 |
|
$class->getName(), |
1958
|
1 |
|
$this->identifierFlattener->flattenIdentifier($class, $id) |
1959
|
|
|
); |
1960
|
|
|
} |
1961
|
|
|
|
1962
|
2 |
|
$managedCopy = $this->newInstance($class); |
1963
|
2 |
|
$class->setIdentifierValues($managedCopy, $id); |
1964
|
|
|
|
1965
|
2 |
|
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); |
1966
|
2 |
|
$this->persistNew($class, $managedCopy); |
1967
|
|
|
} else { |
1968
|
35 |
|
$this->ensureVersionMatch($class, $entity, $managedCopy); |
|
|
|
|
1969
|
34 |
|
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy); |
|
|
|
|
1970
|
|
|
} |
1971
|
|
|
} |
1972
|
|
|
|
1973
|
41 |
|
$visited[$oid] = $managedCopy; // mark visited |
1974
|
|
|
|
1975
|
41 |
|
if ($class->isChangeTrackingDeferredExplicit()) { |
1976
|
|
|
$this->scheduleForDirtyCheck($entity); |
1977
|
|
|
} |
1978
|
|
|
} |
1979
|
|
|
|
1980
|
42 |
|
if ($prevManagedCopy !== null) { |
1981
|
6 |
|
$this->updateAssociationWithMergedEntity($entity, $assoc, $prevManagedCopy, $managedCopy); |
|
|
|
|
1982
|
|
|
} |
1983
|
|
|
|
1984
|
|
|
// Mark the managed copy visited as well |
1985
|
42 |
|
$visited[spl_object_hash($managedCopy)] = $managedCopy; |
|
|
|
|
1986
|
|
|
|
1987
|
42 |
|
$this->cascadeMerge($entity, $managedCopy, $visited); |
|
|
|
|
1988
|
|
|
|
1989
|
42 |
|
return $managedCopy; |
|
|
|
|
1990
|
|
|
} |
1991
|
|
|
|
1992
|
|
|
/** |
1993
|
|
|
* @param ClassMetadata $class |
1994
|
|
|
* @param object $entity |
1995
|
|
|
* @param object $managedCopy |
1996
|
|
|
* |
1997
|
|
|
* @return void |
1998
|
|
|
* |
1999
|
|
|
* @throws OptimisticLockException |
2000
|
|
|
*/ |
2001
|
35 |
|
private function ensureVersionMatch(ClassMetadata $class, $entity, $managedCopy) |
2002
|
|
|
{ |
2003
|
35 |
|
if (! ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity))) { |
2004
|
32 |
|
return; |
2005
|
|
|
} |
2006
|
|
|
|
2007
|
5 |
|
$reflField = $class->reflFields[$class->versionField]; |
2008
|
5 |
|
$managedCopyVersion = $reflField->getValue($managedCopy); |
2009
|
5 |
|
$entityVersion = $reflField->getValue($entity); |
2010
|
|
|
|
2011
|
|
|
// Throw exception if versions don't match. |
2012
|
5 |
|
if ($managedCopyVersion == $entityVersion) { |
2013
|
4 |
|
return; |
2014
|
|
|
} |
2015
|
|
|
|
2016
|
1 |
|
throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion); |
2017
|
|
|
} |
2018
|
|
|
|
2019
|
|
|
/** |
2020
|
|
|
* Tests if an entity is loaded - must either be a loaded proxy or not a proxy |
2021
|
|
|
* |
2022
|
|
|
* @param object $entity |
2023
|
|
|
* |
2024
|
|
|
* @return bool |
2025
|
|
|
*/ |
2026
|
42 |
|
private function isLoaded($entity) |
2027
|
|
|
{ |
2028
|
42 |
|
return !($entity instanceof Proxy) || $entity->__isInitialized(); |
2029
|
|
|
} |
2030
|
|
|
|
2031
|
|
|
/** |
2032
|
|
|
* Sets/adds associated managed copies into the previous entity's association field |
2033
|
|
|
* |
2034
|
|
|
* @param object $entity |
2035
|
|
|
* @param array $association |
2036
|
|
|
* @param object $previousManagedCopy |
2037
|
|
|
* @param object $managedCopy |
2038
|
|
|
* |
2039
|
|
|
* @return void |
2040
|
|
|
*/ |
2041
|
6 |
|
private function updateAssociationWithMergedEntity($entity, array $association, $previousManagedCopy, $managedCopy) |
2042
|
|
|
{ |
2043
|
6 |
|
$assocField = $association['fieldName']; |
2044
|
6 |
|
$prevClass = $this->em->getClassMetadata(get_class($previousManagedCopy)); |
2045
|
|
|
|
2046
|
6 |
|
if ($association['type'] & ClassMetadata::TO_ONE) { |
2047
|
6 |
|
$prevClass->reflFields[$assocField]->setValue($previousManagedCopy, $managedCopy); |
|
|
|
|
2048
|
|
|
|
2049
|
6 |
|
return; |
2050
|
|
|
} |
2051
|
|
|
|
2052
|
1 |
|
$value = $prevClass->reflFields[$assocField]->getValue($previousManagedCopy); |
2053
|
1 |
|
$value[] = $managedCopy; |
2054
|
|
|
|
2055
|
1 |
|
if ($association['type'] == ClassMetadata::ONE_TO_MANY) { |
2056
|
1 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2057
|
|
|
|
2058
|
1 |
|
$class->reflFields[$association['mappedBy']]->setValue($managedCopy, $previousManagedCopy); |
2059
|
|
|
} |
2060
|
1 |
|
} |
2061
|
|
|
|
2062
|
|
|
/** |
2063
|
|
|
* Detaches an entity from the persistence management. It's persistence will |
2064
|
|
|
* no longer be managed by Doctrine. |
2065
|
|
|
* |
2066
|
|
|
* @param object $entity The entity to detach. |
2067
|
|
|
* |
2068
|
|
|
* @return void |
2069
|
|
|
*/ |
2070
|
12 |
|
public function detach($entity) |
2071
|
|
|
{ |
2072
|
12 |
|
$visited = []; |
2073
|
|
|
|
2074
|
12 |
|
$this->doDetach($entity, $visited); |
2075
|
12 |
|
} |
2076
|
|
|
|
2077
|
|
|
/** |
2078
|
|
|
* Executes a detach operation on the given entity. |
2079
|
|
|
* |
2080
|
|
|
* @param object $entity |
2081
|
|
|
* @param array $visited |
2082
|
|
|
* @param boolean $noCascade if true, don't cascade detach operation. |
2083
|
|
|
* |
2084
|
|
|
* @return void |
2085
|
|
|
*/ |
2086
|
16 |
|
private function doDetach($entity, array &$visited, $noCascade = false) |
2087
|
|
|
{ |
2088
|
16 |
|
$oid = spl_object_hash($entity); |
2089
|
|
|
|
2090
|
16 |
|
if (isset($visited[$oid])) { |
2091
|
|
|
return; // Prevent infinite recursion |
2092
|
|
|
} |
2093
|
|
|
|
2094
|
16 |
|
$visited[$oid] = $entity; // mark visited |
2095
|
|
|
|
2096
|
16 |
|
switch ($this->getEntityState($entity, self::STATE_DETACHED)) { |
2097
|
16 |
|
case self::STATE_MANAGED: |
2098
|
14 |
|
if ($this->isInIdentityMap($entity)) { |
2099
|
13 |
|
$this->removeFromIdentityMap($entity); |
2100
|
|
|
} |
2101
|
|
|
|
2102
|
|
|
unset( |
2103
|
14 |
|
$this->entityInsertions[$oid], |
2104
|
14 |
|
$this->entityUpdates[$oid], |
2105
|
14 |
|
$this->entityDeletions[$oid], |
2106
|
14 |
|
$this->entityIdentifiers[$oid], |
2107
|
14 |
|
$this->entityStates[$oid], |
2108
|
14 |
|
$this->originalEntityData[$oid] |
2109
|
|
|
); |
2110
|
14 |
|
break; |
2111
|
3 |
|
case self::STATE_NEW: |
2112
|
3 |
|
case self::STATE_DETACHED: |
2113
|
3 |
|
return; |
2114
|
|
|
} |
2115
|
|
|
|
2116
|
14 |
|
if ( ! $noCascade) { |
2117
|
14 |
|
$this->cascadeDetach($entity, $visited); |
2118
|
|
|
} |
2119
|
14 |
|
} |
2120
|
|
|
|
2121
|
|
|
/** |
2122
|
|
|
* Refreshes the state of the given entity from the database, overwriting |
2123
|
|
|
* any local, unpersisted changes. |
2124
|
|
|
* |
2125
|
|
|
* @param object $entity The entity to refresh. |
2126
|
|
|
* |
2127
|
|
|
* @return void |
2128
|
|
|
* |
2129
|
|
|
* @throws InvalidArgumentException If the entity is not MANAGED. |
2130
|
|
|
*/ |
2131
|
17 |
|
public function refresh($entity) |
2132
|
|
|
{ |
2133
|
17 |
|
$visited = []; |
2134
|
|
|
|
2135
|
17 |
|
$this->doRefresh($entity, $visited); |
2136
|
17 |
|
} |
2137
|
|
|
|
2138
|
|
|
/** |
2139
|
|
|
* Executes a refresh operation on an entity. |
2140
|
|
|
* |
2141
|
|
|
* @param object $entity The entity to refresh. |
2142
|
|
|
* @param array $visited The already visited entities during cascades. |
2143
|
|
|
* |
2144
|
|
|
* @return void |
2145
|
|
|
* |
2146
|
|
|
* @throws ORMInvalidArgumentException If the entity is not MANAGED. |
2147
|
|
|
*/ |
2148
|
17 |
|
private function doRefresh($entity, array &$visited) |
2149
|
|
|
{ |
2150
|
17 |
|
$oid = spl_object_hash($entity); |
2151
|
|
|
|
2152
|
17 |
|
if (isset($visited[$oid])) { |
2153
|
|
|
return; // Prevent infinite recursion |
2154
|
|
|
} |
2155
|
|
|
|
2156
|
17 |
|
$visited[$oid] = $entity; // mark visited |
2157
|
|
|
|
2158
|
17 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2159
|
|
|
|
2160
|
17 |
|
if ($this->getEntityState($entity) !== self::STATE_MANAGED) { |
2161
|
|
|
throw ORMInvalidArgumentException::entityNotManaged($entity); |
2162
|
|
|
} |
2163
|
|
|
|
2164
|
17 |
|
$this->getEntityPersister($class->name)->refresh( |
|
|
|
|
2165
|
17 |
|
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), |
2166
|
17 |
|
$entity |
2167
|
|
|
); |
2168
|
|
|
|
2169
|
17 |
|
$this->cascadeRefresh($entity, $visited); |
2170
|
17 |
|
} |
2171
|
|
|
|
2172
|
|
|
/** |
2173
|
|
|
* Cascades a refresh operation to associated entities. |
2174
|
|
|
* |
2175
|
|
|
* @param object $entity |
2176
|
|
|
* @param array $visited |
2177
|
|
|
* |
2178
|
|
|
* @return void |
2179
|
|
|
*/ |
2180
|
17 |
|
private function cascadeRefresh($entity, array &$visited) |
2181
|
|
|
{ |
2182
|
17 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2183
|
|
|
|
2184
|
17 |
|
$associationMappings = array_filter( |
2185
|
17 |
|
$class->associationMappings, |
|
|
|
|
2186
|
|
|
function ($assoc) { return $assoc['isCascadeRefresh']; } |
2187
|
|
|
); |
2188
|
|
|
|
2189
|
17 |
|
foreach ($associationMappings as $assoc) { |
2190
|
5 |
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
|
|
|
|
2191
|
|
|
|
2192
|
|
|
switch (true) { |
2193
|
5 |
|
case ($relatedEntities instanceof PersistentCollection): |
2194
|
|
|
// Unwrap so that foreach() does not initialize |
2195
|
5 |
|
$relatedEntities = $relatedEntities->unwrap(); |
2196
|
|
|
// break; is commented intentionally! |
2197
|
|
|
|
2198
|
|
|
case ($relatedEntities instanceof Collection): |
2199
|
|
|
case (is_array($relatedEntities)): |
2200
|
5 |
|
foreach ($relatedEntities as $relatedEntity) { |
2201
|
|
|
$this->doRefresh($relatedEntity, $visited); |
2202
|
|
|
} |
2203
|
5 |
|
break; |
2204
|
|
|
|
2205
|
|
|
case ($relatedEntities !== null): |
2206
|
|
|
$this->doRefresh($relatedEntities, $visited); |
2207
|
|
|
break; |
2208
|
|
|
|
2209
|
5 |
|
default: |
2210
|
|
|
// Do nothing |
2211
|
|
|
} |
2212
|
|
|
} |
2213
|
17 |
|
} |
2214
|
|
|
|
2215
|
|
|
/** |
2216
|
|
|
* Cascades a detach operation to associated entities. |
2217
|
|
|
* |
2218
|
|
|
* @param object $entity |
2219
|
|
|
* @param array $visited |
2220
|
|
|
* |
2221
|
|
|
* @return void |
2222
|
|
|
*/ |
2223
|
14 |
|
private function cascadeDetach($entity, array &$visited) |
2224
|
|
|
{ |
2225
|
14 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2226
|
|
|
|
2227
|
14 |
|
$associationMappings = array_filter( |
2228
|
14 |
|
$class->associationMappings, |
|
|
|
|
2229
|
|
|
function ($assoc) { return $assoc['isCascadeDetach']; } |
2230
|
|
|
); |
2231
|
|
|
|
2232
|
14 |
|
foreach ($associationMappings as $assoc) { |
2233
|
3 |
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
|
|
|
|
2234
|
|
|
|
2235
|
|
|
switch (true) { |
2236
|
3 |
|
case ($relatedEntities instanceof PersistentCollection): |
2237
|
|
|
// Unwrap so that foreach() does not initialize |
2238
|
2 |
|
$relatedEntities = $relatedEntities->unwrap(); |
2239
|
|
|
// break; is commented intentionally! |
2240
|
|
|
|
2241
|
1 |
|
case ($relatedEntities instanceof Collection): |
2242
|
|
|
case (is_array($relatedEntities)): |
2243
|
3 |
|
foreach ($relatedEntities as $relatedEntity) { |
2244
|
1 |
|
$this->doDetach($relatedEntity, $visited); |
2245
|
|
|
} |
2246
|
3 |
|
break; |
2247
|
|
|
|
2248
|
|
|
case ($relatedEntities !== null): |
2249
|
|
|
$this->doDetach($relatedEntities, $visited); |
2250
|
|
|
break; |
2251
|
|
|
|
2252
|
3 |
|
default: |
2253
|
|
|
// Do nothing |
2254
|
|
|
} |
2255
|
|
|
} |
2256
|
14 |
|
} |
2257
|
|
|
|
2258
|
|
|
/** |
2259
|
|
|
* Cascades a merge operation to associated entities. |
2260
|
|
|
* |
2261
|
|
|
* @param object $entity |
2262
|
|
|
* @param object $managedCopy |
2263
|
|
|
* @param array $visited |
2264
|
|
|
* |
2265
|
|
|
* @return void |
2266
|
|
|
*/ |
2267
|
42 |
|
private function cascadeMerge($entity, $managedCopy, array &$visited) |
2268
|
|
|
{ |
2269
|
42 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2270
|
|
|
|
2271
|
42 |
|
$associationMappings = array_filter( |
2272
|
42 |
|
$class->associationMappings, |
|
|
|
|
2273
|
|
|
function ($assoc) { return $assoc['isCascadeMerge']; } |
2274
|
|
|
); |
2275
|
|
|
|
2276
|
42 |
|
foreach ($associationMappings as $assoc) { |
2277
|
17 |
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
|
|
|
|
2278
|
|
|
|
2279
|
17 |
|
if ($relatedEntities instanceof Collection) { |
2280
|
11 |
|
if ($relatedEntities === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) { |
2281
|
2 |
|
continue; |
2282
|
|
|
} |
2283
|
|
|
|
2284
|
10 |
|
if ($relatedEntities instanceof PersistentCollection) { |
2285
|
|
|
// Unwrap so that foreach() does not initialize |
2286
|
6 |
|
$relatedEntities = $relatedEntities->unwrap(); |
2287
|
|
|
} |
2288
|
|
|
|
2289
|
10 |
|
foreach ($relatedEntities as $relatedEntity) { |
2290
|
10 |
|
$this->doMerge($relatedEntity, $visited, $managedCopy, $assoc); |
2291
|
|
|
} |
2292
|
7 |
|
} else if ($relatedEntities !== null) { |
2293
|
16 |
|
$this->doMerge($relatedEntities, $visited, $managedCopy, $assoc); |
2294
|
|
|
} |
2295
|
|
|
} |
2296
|
42 |
|
} |
2297
|
|
|
|
2298
|
|
|
/** |
2299
|
|
|
* Cascades the save operation to associated entities. |
2300
|
|
|
* |
2301
|
|
|
* @param object $entity |
2302
|
|
|
* @param array $visited |
2303
|
|
|
* |
2304
|
|
|
* @return void |
2305
|
|
|
*/ |
2306
|
1104 |
|
private function cascadePersist($entity, array &$visited) |
2307
|
|
|
{ |
2308
|
1104 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2309
|
|
|
|
2310
|
1104 |
|
$associationMappings = array_filter( |
2311
|
1104 |
|
$class->associationMappings, |
|
|
|
|
2312
|
|
|
function ($assoc) { return $assoc['isCascadePersist']; } |
2313
|
|
|
); |
2314
|
|
|
|
2315
|
1104 |
|
foreach ($associationMappings as $assoc) { |
2316
|
686 |
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
|
|
|
|
2317
|
|
|
|
2318
|
|
|
switch (true) { |
2319
|
686 |
|
case ($relatedEntities instanceof PersistentCollection): |
2320
|
|
|
// Unwrap so that foreach() does not initialize |
2321
|
21 |
|
$relatedEntities = $relatedEntities->unwrap(); |
2322
|
|
|
// break; is commented intentionally! |
2323
|
|
|
|
2324
|
686 |
|
case ($relatedEntities instanceof Collection): |
2325
|
622 |
|
case (is_array($relatedEntities)): |
2326
|
577 |
|
if (($assoc['type'] & ClassMetadata::TO_MANY) <= 0) { |
2327
|
3 |
|
throw ORMInvalidArgumentException::invalidAssociation( |
2328
|
3 |
|
$this->em->getClassMetadata($assoc['targetEntity']), |
2329
|
3 |
|
$assoc, |
2330
|
3 |
|
$relatedEntities |
2331
|
|
|
); |
2332
|
|
|
} |
2333
|
|
|
|
2334
|
574 |
|
foreach ($relatedEntities as $relatedEntity) { |
2335
|
293 |
|
$this->doPersist($relatedEntity, $visited); |
2336
|
|
|
} |
2337
|
|
|
|
2338
|
574 |
|
break; |
2339
|
|
|
|
2340
|
611 |
|
case ($relatedEntities !== null): |
2341
|
254 |
|
if (! $relatedEntities instanceof $assoc['targetEntity']) { |
2342
|
4 |
|
throw ORMInvalidArgumentException::invalidAssociation( |
2343
|
4 |
|
$this->em->getClassMetadata($assoc['targetEntity']), |
2344
|
4 |
|
$assoc, |
2345
|
4 |
|
$relatedEntities |
2346
|
|
|
); |
2347
|
|
|
} |
2348
|
|
|
|
2349
|
250 |
|
$this->doPersist($relatedEntities, $visited); |
2350
|
250 |
|
break; |
2351
|
|
|
|
2352
|
680 |
|
default: |
2353
|
|
|
// Do nothing |
2354
|
|
|
} |
2355
|
|
|
} |
2356
|
1097 |
|
} |
2357
|
|
|
|
2358
|
|
|
/** |
2359
|
|
|
* Cascades the delete operation to associated entities. |
2360
|
|
|
* |
2361
|
|
|
* @param object $entity |
2362
|
|
|
* @param array $visited |
2363
|
|
|
* |
2364
|
|
|
* @return void |
2365
|
|
|
*/ |
2366
|
66 |
|
private function cascadeRemove($entity, array &$visited) |
2367
|
|
|
{ |
2368
|
66 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2369
|
|
|
|
2370
|
66 |
|
$associationMappings = array_filter( |
2371
|
66 |
|
$class->associationMappings, |
|
|
|
|
2372
|
|
|
function ($assoc) { return $assoc['isCascadeRemove']; } |
2373
|
|
|
); |
2374
|
|
|
|
2375
|
66 |
|
$entitiesToCascade = []; |
2376
|
|
|
|
2377
|
66 |
|
foreach ($associationMappings as $assoc) { |
2378
|
26 |
|
if ($entity instanceof Proxy && !$entity->__isInitialized__) { |
|
|
|
|
2379
|
6 |
|
$entity->__load(); |
2380
|
|
|
} |
2381
|
|
|
|
2382
|
26 |
|
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
|
|
|
|
2383
|
|
|
|
2384
|
|
|
switch (true) { |
2385
|
26 |
|
case ($relatedEntities instanceof Collection): |
2386
|
19 |
|
case (is_array($relatedEntities)): |
2387
|
|
|
// If its a PersistentCollection initialization is intended! No unwrap! |
2388
|
20 |
|
foreach ($relatedEntities as $relatedEntity) { |
2389
|
10 |
|
$entitiesToCascade[] = $relatedEntity; |
2390
|
|
|
} |
2391
|
20 |
|
break; |
2392
|
|
|
|
2393
|
19 |
|
case ($relatedEntities !== null): |
2394
|
7 |
|
$entitiesToCascade[] = $relatedEntities; |
2395
|
7 |
|
break; |
2396
|
|
|
|
2397
|
26 |
|
default: |
2398
|
|
|
// Do nothing |
2399
|
|
|
} |
2400
|
|
|
} |
2401
|
|
|
|
2402
|
66 |
|
foreach ($entitiesToCascade as $relatedEntity) { |
2403
|
16 |
|
$this->doRemove($relatedEntity, $visited); |
2404
|
|
|
} |
2405
|
66 |
|
} |
2406
|
|
|
|
2407
|
|
|
/** |
2408
|
|
|
* Acquire a lock on the given entity. |
2409
|
|
|
* |
2410
|
|
|
* @param object $entity |
2411
|
|
|
* @param int $lockMode |
2412
|
|
|
* @param int $lockVersion |
2413
|
|
|
* |
2414
|
|
|
* @return void |
2415
|
|
|
* |
2416
|
|
|
* @throws ORMInvalidArgumentException |
2417
|
|
|
* @throws TransactionRequiredException |
2418
|
|
|
* @throws OptimisticLockException |
2419
|
|
|
*/ |
2420
|
10 |
|
public function lock($entity, $lockMode, $lockVersion = null) |
2421
|
|
|
{ |
2422
|
10 |
|
if ($entity === null) { |
2423
|
1 |
|
throw new \InvalidArgumentException("No entity passed to UnitOfWork#lock()."); |
2424
|
|
|
} |
2425
|
|
|
|
2426
|
9 |
|
if ($this->getEntityState($entity, self::STATE_DETACHED) != self::STATE_MANAGED) { |
2427
|
1 |
|
throw ORMInvalidArgumentException::entityNotManaged($entity); |
2428
|
|
|
} |
2429
|
|
|
|
2430
|
8 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
2431
|
|
|
|
2432
|
|
|
switch (true) { |
2433
|
8 |
|
case LockMode::OPTIMISTIC === $lockMode: |
2434
|
6 |
|
if ( ! $class->isVersioned) { |
|
|
|
|
2435
|
1 |
|
throw OptimisticLockException::notVersioned($class->name); |
|
|
|
|
2436
|
|
|
} |
2437
|
|
|
|
2438
|
5 |
|
if ($lockVersion === null) { |
2439
|
1 |
|
return; |
2440
|
|
|
} |
2441
|
|
|
|
2442
|
4 |
|
if ($entity instanceof Proxy && !$entity->__isInitialized__) { |
|
|
|
|
2443
|
1 |
|
$entity->__load(); |
2444
|
|
|
} |
2445
|
|
|
|
2446
|
4 |
|
$entityVersion = $class->reflFields[$class->versionField]->getValue($entity); |
|
|
|
|
2447
|
|
|
|
2448
|
4 |
|
if ($entityVersion != $lockVersion) { |
2449
|
2 |
|
throw OptimisticLockException::lockFailedVersionMismatch($entity, $lockVersion, $entityVersion); |
2450
|
|
|
} |
2451
|
|
|
|
2452
|
2 |
|
break; |
2453
|
|
|
|
2454
|
2 |
|
case LockMode::NONE === $lockMode: |
2455
|
2 |
|
case LockMode::PESSIMISTIC_READ === $lockMode: |
2456
|
1 |
|
case LockMode::PESSIMISTIC_WRITE === $lockMode: |
2457
|
2 |
|
if (!$this->em->getConnection()->isTransactionActive()) { |
2458
|
2 |
|
throw TransactionRequiredException::transactionRequired(); |
2459
|
|
|
} |
2460
|
|
|
|
2461
|
|
|
$oid = spl_object_hash($entity); |
2462
|
|
|
|
2463
|
|
|
$this->getEntityPersister($class->name)->lock( |
2464
|
|
|
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), |
2465
|
|
|
$lockMode |
2466
|
|
|
); |
2467
|
|
|
break; |
2468
|
|
|
|
2469
|
|
|
default: |
2470
|
|
|
// Do nothing |
2471
|
|
|
} |
2472
|
2 |
|
} |
2473
|
|
|
|
2474
|
|
|
/** |
2475
|
|
|
* Gets the CommitOrderCalculator used by the UnitOfWork to order commits. |
2476
|
|
|
* |
2477
|
|
|
* @return \Doctrine\ORM\Internal\CommitOrderCalculator |
2478
|
|
|
*/ |
2479
|
1079 |
|
public function getCommitOrderCalculator() |
2480
|
|
|
{ |
2481
|
1079 |
|
return new Internal\CommitOrderCalculator(); |
2482
|
|
|
} |
2483
|
|
|
|
2484
|
|
|
/** |
2485
|
|
|
* Clears the UnitOfWork. |
2486
|
|
|
* |
2487
|
|
|
* @param string|null $entityName if given, only entities of this type will get detached. |
2488
|
|
|
* |
2489
|
|
|
* @return void |
2490
|
|
|
* |
2491
|
|
|
* @throws ORMInvalidArgumentException if an invalid entity name is given |
2492
|
|
|
*/ |
2493
|
1306 |
|
public function clear($entityName = null) |
2494
|
|
|
{ |
2495
|
1306 |
|
if ($entityName === null) { |
2496
|
1304 |
|
$this->identityMap = |
2497
|
1304 |
|
$this->oidMap = |
2498
|
1304 |
|
$this->entityIdentifiers = |
2499
|
1304 |
|
$this->originalEntityData = |
2500
|
1304 |
|
$this->entityChangeSets = |
2501
|
1304 |
|
$this->entityStates = |
2502
|
1304 |
|
$this->scheduledForSynchronization = |
2503
|
1304 |
|
$this->entityInsertions = |
2504
|
1304 |
|
$this->entityUpdates = |
2505
|
1304 |
|
$this->entityDeletions = |
2506
|
1304 |
|
$this->nonCascadedNewDetectedEntities = |
2507
|
1304 |
|
$this->collectionDeletions = |
2508
|
1304 |
|
$this->collectionUpdates = |
2509
|
1304 |
|
$this->extraUpdates = |
2510
|
1304 |
|
$this->readOnlyObjects = |
2511
|
1304 |
|
$this->visitedCollections = |
2512
|
1304 |
|
$this->orphanRemovals = []; |
2513
|
|
|
} else { |
2514
|
4 |
|
$this->clearIdentityMapForEntityName($entityName); |
2515
|
4 |
|
$this->clearEntityInsertionsForEntityName($entityName); |
2516
|
|
|
} |
2517
|
|
|
|
2518
|
1306 |
|
if ($this->evm->hasListeners(Events::onClear)) { |
2519
|
9 |
|
$this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em, $entityName)); |
2520
|
|
|
} |
2521
|
1306 |
|
} |
2522
|
|
|
|
2523
|
|
|
/** |
2524
|
|
|
* INTERNAL: |
2525
|
|
|
* Schedules an orphaned entity for removal. The remove() operation will be |
2526
|
|
|
* invoked on that entity at the beginning of the next commit of this |
2527
|
|
|
* UnitOfWork. |
2528
|
|
|
* |
2529
|
|
|
* @ignore |
2530
|
|
|
* |
2531
|
|
|
* @param object $entity |
2532
|
|
|
* |
2533
|
|
|
* @return void |
2534
|
|
|
*/ |
2535
|
17 |
|
public function scheduleOrphanRemoval($entity) |
2536
|
|
|
{ |
2537
|
17 |
|
$this->orphanRemovals[spl_object_hash($entity)] = $entity; |
2538
|
17 |
|
} |
2539
|
|
|
|
2540
|
|
|
/** |
2541
|
|
|
* INTERNAL: |
2542
|
|
|
* Cancels a previously scheduled orphan removal. |
2543
|
|
|
* |
2544
|
|
|
* @ignore |
2545
|
|
|
* |
2546
|
|
|
* @param object $entity |
2547
|
|
|
* |
2548
|
|
|
* @return void |
2549
|
|
|
*/ |
2550
|
117 |
|
public function cancelOrphanRemoval($entity) |
2551
|
|
|
{ |
2552
|
117 |
|
unset($this->orphanRemovals[spl_object_hash($entity)]); |
2553
|
117 |
|
} |
2554
|
|
|
|
2555
|
|
|
/** |
2556
|
|
|
* INTERNAL: |
2557
|
|
|
* Schedules a complete collection for removal when this UnitOfWork commits. |
2558
|
|
|
* |
2559
|
|
|
* @param PersistentCollection $coll |
2560
|
|
|
* |
2561
|
|
|
* @return void |
2562
|
|
|
*/ |
2563
|
14 |
|
public function scheduleCollectionDeletion(PersistentCollection $coll) |
2564
|
|
|
{ |
2565
|
14 |
|
$coid = spl_object_hash($coll); |
2566
|
|
|
|
2567
|
|
|
// TODO: if $coll is already scheduled for recreation ... what to do? |
2568
|
|
|
// Just remove $coll from the scheduled recreations? |
2569
|
14 |
|
unset($this->collectionUpdates[$coid]); |
2570
|
|
|
|
2571
|
14 |
|
$this->collectionDeletions[$coid] = $coll; |
2572
|
14 |
|
} |
2573
|
|
|
|
2574
|
|
|
/** |
2575
|
|
|
* @param PersistentCollection $coll |
2576
|
|
|
* |
2577
|
|
|
* @return bool |
2578
|
|
|
*/ |
2579
|
|
|
public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
2580
|
|
|
{ |
2581
|
|
|
return isset($this->collectionDeletions[spl_object_hash($coll)]); |
2582
|
|
|
} |
2583
|
|
|
|
2584
|
|
|
/** |
2585
|
|
|
* @param ClassMetadata $class |
2586
|
|
|
* |
2587
|
|
|
* @return \Doctrine\Common\Persistence\ObjectManagerAware|object |
2588
|
|
|
*/ |
2589
|
715 |
|
private function newInstance($class) |
2590
|
|
|
{ |
2591
|
715 |
|
$entity = $class->newInstance(); |
2592
|
|
|
|
2593
|
715 |
|
if ($entity instanceof \Doctrine\Common\Persistence\ObjectManagerAware) { |
2594
|
4 |
|
$entity->injectObjectManager($this->em, $class); |
2595
|
|
|
} |
2596
|
|
|
|
2597
|
715 |
|
return $entity; |
2598
|
|
|
} |
2599
|
|
|
|
2600
|
|
|
/** |
2601
|
|
|
* INTERNAL: |
2602
|
|
|
* Creates an entity. Used for reconstitution of persistent entities. |
2603
|
|
|
* |
2604
|
|
|
* Internal note: Highly performance-sensitive method. |
2605
|
|
|
* |
2606
|
|
|
* @ignore |
2607
|
|
|
* |
2608
|
|
|
* @param string $className The name of the entity class. |
2609
|
|
|
* @param array $data The data for the entity. |
2610
|
|
|
* @param array $hints Any hints to account for during reconstitution/lookup of the entity. |
2611
|
|
|
* |
2612
|
|
|
* @return object The managed entity instance. |
2613
|
|
|
* |
2614
|
|
|
* @todo Rename: getOrCreateEntity |
2615
|
|
|
*/ |
2616
|
857 |
|
public function createEntity($className, array $data, &$hints = []) |
2617
|
|
|
{ |
2618
|
857 |
|
$class = $this->em->getClassMetadata($className); |
2619
|
|
|
|
2620
|
857 |
|
$id = $this->identifierFlattener->flattenIdentifier($class, $data); |
2621
|
857 |
|
$idHash = implode(' ', $id); |
2622
|
|
|
|
2623
|
857 |
|
if (isset($this->identityMap[$class->rootEntityName][$idHash])) { |
|
|
|
|
2624
|
325 |
|
$entity = $this->identityMap[$class->rootEntityName][$idHash]; |
2625
|
325 |
|
$oid = spl_object_hash($entity); |
2626
|
|
|
|
2627
|
|
|
if ( |
2628
|
325 |
|
isset($hints[Query::HINT_REFRESH]) |
2629
|
325 |
|
&& isset($hints[Query::HINT_REFRESH_ENTITY]) |
2630
|
325 |
|
&& ($unmanagedProxy = $hints[Query::HINT_REFRESH_ENTITY]) !== $entity |
2631
|
325 |
|
&& $unmanagedProxy instanceof Proxy |
2632
|
325 |
|
&& $this->isIdentifierEquals($unmanagedProxy, $entity) |
2633
|
|
|
) { |
2634
|
|
|
// DDC-1238 - we have a managed instance, but it isn't the provided one. |
2635
|
|
|
// Therefore we clear its identifier. Also, we must re-fetch metadata since the |
2636
|
|
|
// refreshed object may be anything |
2637
|
|
|
|
2638
|
2 |
|
foreach ($class->identifier as $fieldName) { |
|
|
|
|
2639
|
2 |
|
$class->reflFields[$fieldName]->setValue($unmanagedProxy, null); |
|
|
|
|
2640
|
|
|
} |
2641
|
|
|
|
2642
|
2 |
|
return $unmanagedProxy; |
2643
|
|
|
} |
2644
|
|
|
|
2645
|
323 |
|
if ($entity instanceof Proxy && ! $entity->__isInitialized()) { |
2646
|
23 |
|
$entity->__setInitialized(true); |
2647
|
|
|
|
2648
|
23 |
|
if ($entity instanceof NotifyPropertyChanged) { |
2649
|
23 |
|
$entity->addPropertyChangedListener($this); |
2650
|
|
|
} |
2651
|
|
|
} else { |
2652
|
302 |
|
if ( ! isset($hints[Query::HINT_REFRESH]) |
2653
|
302 |
|
|| (isset($hints[Query::HINT_REFRESH_ENTITY]) && $hints[Query::HINT_REFRESH_ENTITY] !== $entity)) { |
2654
|
231 |
|
return $entity; |
2655
|
|
|
} |
2656
|
|
|
} |
2657
|
|
|
|
2658
|
|
|
// inject ObjectManager upon refresh. |
2659
|
115 |
|
if ($entity instanceof ObjectManagerAware) { |
2660
|
3 |
|
$entity->injectObjectManager($this->em, $class); |
2661
|
|
|
} |
2662
|
|
|
|
2663
|
115 |
|
$this->originalEntityData[$oid] = $data; |
2664
|
|
|
} else { |
2665
|
710 |
|
$entity = $this->newInstance($class); |
2666
|
710 |
|
$oid = spl_object_hash($entity); |
2667
|
|
|
|
2668
|
710 |
|
$this->entityIdentifiers[$oid] = $id; |
2669
|
710 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
2670
|
710 |
|
$this->originalEntityData[$oid] = $data; |
2671
|
|
|
|
2672
|
710 |
|
$this->oidMap[$oid] = $entity; |
2673
|
710 |
|
$this->identityMap[$class->rootEntityName][$idHash] = $entity; |
2674
|
|
|
|
2675
|
710 |
|
if ($entity instanceof NotifyPropertyChanged) { |
2676
|
2 |
|
$entity->addPropertyChangedListener($this); |
2677
|
|
|
} |
2678
|
|
|
} |
2679
|
|
|
|
2680
|
748 |
|
foreach ($data as $field => $value) { |
2681
|
748 |
|
if (isset($class->fieldMappings[$field])) { |
|
|
|
|
2682
|
748 |
|
$class->reflFields[$field]->setValue($entity, $value); |
2683
|
|
|
} |
2684
|
|
|
} |
2685
|
|
|
|
2686
|
|
|
// Loading the entity right here, if its in the eager loading map get rid of it there. |
2687
|
748 |
|
unset($this->eagerLoadingEntities[$class->rootEntityName][$idHash]); |
2688
|
|
|
|
2689
|
748 |
|
if (isset($this->eagerLoadingEntities[$class->rootEntityName]) && ! $this->eagerLoadingEntities[$class->rootEntityName]) { |
2690
|
|
|
unset($this->eagerLoadingEntities[$class->rootEntityName]); |
2691
|
|
|
} |
2692
|
|
|
|
2693
|
|
|
// Properly initialize any unfetched associations, if partial objects are not allowed. |
2694
|
748 |
|
if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD])) { |
2695
|
34 |
|
return $entity; |
2696
|
|
|
} |
2697
|
|
|
|
2698
|
714 |
|
foreach ($class->associationMappings as $field => $assoc) { |
2699
|
|
|
// Check if the association is not among the fetch-joined associations already. |
2700
|
612 |
|
if (isset($hints['fetchAlias']) && isset($hints['fetched'][$hints['fetchAlias']][$field])) { |
2701
|
261 |
|
continue; |
2702
|
|
|
} |
2703
|
|
|
|
2704
|
588 |
|
$targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
2705
|
|
|
|
2706
|
|
|
switch (true) { |
|
|
|
|
2707
|
588 |
|
case ($assoc['type'] & ClassMetadata::TO_ONE): |
2708
|
508 |
|
if ( ! $assoc['isOwningSide']) { |
2709
|
|
|
|
2710
|
|
|
// use the given entity association |
2711
|
67 |
|
if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_hash($data[$field])])) { |
2712
|
|
|
|
2713
|
3 |
|
$this->originalEntityData[$oid][$field] = $data[$field]; |
2714
|
|
|
|
2715
|
3 |
|
$class->reflFields[$field]->setValue($entity, $data[$field]); |
2716
|
3 |
|
$targetClass->reflFields[$assoc['mappedBy']]->setValue($data[$field], $entity); |
2717
|
|
|
|
2718
|
3 |
|
continue 2; |
2719
|
|
|
} |
2720
|
|
|
|
2721
|
|
|
// Inverse side of x-to-one can never be lazy |
2722
|
64 |
|
$class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity)); |
2723
|
|
|
|
2724
|
64 |
|
continue 2; |
2725
|
|
|
} |
2726
|
|
|
|
2727
|
|
|
// use the entity association |
2728
|
508 |
|
if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_hash($data[$field])])) { |
2729
|
38 |
|
$class->reflFields[$field]->setValue($entity, $data[$field]); |
2730
|
38 |
|
$this->originalEntityData[$oid][$field] = $data[$field]; |
2731
|
|
|
|
2732
|
38 |
|
break; |
2733
|
|
|
} |
2734
|
|
|
|
2735
|
501 |
|
$associatedId = []; |
2736
|
|
|
|
2737
|
|
|
// TODO: Is this even computed right in all cases of composite keys? |
2738
|
501 |
|
foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { |
2739
|
501 |
|
$joinColumnValue = $data[$srcColumn] ?? null; |
2740
|
|
|
|
2741
|
501 |
|
if ($joinColumnValue !== null) { |
2742
|
301 |
|
if ($targetClass->containsForeignIdentifier) { |
|
|
|
|
2743
|
12 |
|
$associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue; |
2744
|
|
|
} else { |
2745
|
301 |
|
$associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; |
|
|
|
|
2746
|
|
|
} |
2747
|
295 |
|
} elseif ($targetClass->containsForeignIdentifier |
2748
|
295 |
|
&& in_array($targetClass->getFieldForColumn($targetColumn), $targetClass->identifier, true) |
2749
|
|
|
) { |
2750
|
|
|
// the missing key is part of target's entity primary key |
2751
|
7 |
|
$associatedId = []; |
2752
|
501 |
|
break; |
2753
|
|
|
} |
2754
|
|
|
} |
2755
|
|
|
|
2756
|
501 |
|
if ( ! $associatedId) { |
2757
|
|
|
// Foreign key is NULL |
2758
|
295 |
|
$class->reflFields[$field]->setValue($entity, null); |
2759
|
295 |
|
$this->originalEntityData[$oid][$field] = null; |
2760
|
|
|
|
2761
|
295 |
|
break; |
2762
|
|
|
} |
2763
|
|
|
|
2764
|
301 |
|
if ( ! isset($hints['fetchMode'][$class->name][$field])) { |
|
|
|
|
2765
|
298 |
|
$hints['fetchMode'][$class->name][$field] = $assoc['fetch']; |
2766
|
|
|
} |
2767
|
|
|
|
2768
|
|
|
// Foreign key is set |
2769
|
|
|
// Check identity map first |
2770
|
|
|
// FIXME: Can break easily with composite keys if join column values are in |
2771
|
|
|
// wrong order. The correct order is the one in ClassMetadata#identifier. |
2772
|
301 |
|
$relatedIdHash = implode(' ', $associatedId); |
2773
|
|
|
|
2774
|
|
|
switch (true) { |
2775
|
301 |
|
case (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])): |
2776
|
175 |
|
$newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash]; |
2777
|
|
|
|
2778
|
|
|
// If this is an uninitialized proxy, we are deferring eager loads, |
2779
|
|
|
// this association is marked as eager fetch, and its an uninitialized proxy (wtf!) |
2780
|
|
|
// then we can append this entity for eager loading! |
2781
|
175 |
|
if ($hints['fetchMode'][$class->name][$field] == ClassMetadata::FETCH_EAGER && |
2782
|
175 |
|
isset($hints[self::HINT_DEFEREAGERLOAD]) && |
2783
|
175 |
|
!$targetClass->isIdentifierComposite && |
|
|
|
|
2784
|
175 |
|
$newValue instanceof Proxy && |
2785
|
175 |
|
$newValue->__isInitialized__ === false) { |
|
|
|
|
2786
|
|
|
|
2787
|
|
|
$this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId); |
2788
|
|
|
} |
2789
|
|
|
|
2790
|
175 |
|
break; |
2791
|
|
|
|
2792
|
204 |
|
case ($targetClass->subClasses): |
|
|
|
|
2793
|
|
|
// If it might be a subtype, it can not be lazy. There isn't even |
2794
|
|
|
// a way to solve this with deferred eager loading, which means putting |
2795
|
|
|
// an entity with subclasses at a *-to-one location is really bad! (performance-wise) |
2796
|
32 |
|
$newValue = $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity, $associatedId); |
2797
|
32 |
|
break; |
2798
|
|
|
|
2799
|
|
|
default: |
2800
|
|
|
switch (true) { |
2801
|
|
|
// We are negating the condition here. Other cases will assume it is valid! |
2802
|
174 |
|
case ($hints['fetchMode'][$class->name][$field] !== ClassMetadata::FETCH_EAGER): |
2803
|
167 |
|
$newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId); |
2804
|
167 |
|
break; |
2805
|
|
|
|
2806
|
|
|
// Deferred eager load only works for single identifier classes |
2807
|
7 |
|
case (isset($hints[self::HINT_DEFEREAGERLOAD]) && ! $targetClass->isIdentifierComposite): |
2808
|
|
|
// TODO: Is there a faster approach? |
2809
|
7 |
|
$this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId); |
2810
|
|
|
|
2811
|
7 |
|
$newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId); |
2812
|
7 |
|
break; |
2813
|
|
|
|
2814
|
|
|
default: |
2815
|
|
|
// TODO: This is very imperformant, ignore it? |
2816
|
|
|
$newValue = $this->em->find($assoc['targetEntity'], $associatedId); |
2817
|
|
|
break; |
2818
|
|
|
} |
2819
|
|
|
|
2820
|
|
|
// PERF: Inlined & optimized code from UnitOfWork#registerManaged() |
2821
|
174 |
|
$newValueOid = spl_object_hash($newValue); |
2822
|
174 |
|
$this->entityIdentifiers[$newValueOid] = $associatedId; |
2823
|
174 |
|
$this->oidMap[$newValueOid] = $newValue; |
2824
|
174 |
|
$this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue; |
2825
|
|
|
|
2826
|
|
|
if ( |
2827
|
174 |
|
$newValue instanceof NotifyPropertyChanged && |
2828
|
174 |
|
( ! $newValue instanceof Proxy || $newValue->__isInitialized()) |
2829
|
|
|
) { |
2830
|
|
|
$newValue->addPropertyChangedListener($this); |
2831
|
|
|
} |
2832
|
174 |
|
$this->entityStates[$newValueOid] = self::STATE_MANAGED; |
2833
|
|
|
// make sure that when an proxy is then finally loaded, $this->originalEntityData is set also! |
2834
|
174 |
|
break; |
2835
|
|
|
} |
2836
|
|
|
|
2837
|
301 |
|
$this->originalEntityData[$oid][$field] = $newValue; |
2838
|
301 |
|
$class->reflFields[$field]->setValue($entity, $newValue); |
2839
|
|
|
|
2840
|
301 |
|
if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) { |
2841
|
59 |
|
$inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']]; |
|
|
|
|
2842
|
59 |
|
$targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($newValue, $entity); |
2843
|
|
|
} |
2844
|
|
|
|
2845
|
301 |
|
break; |
2846
|
|
|
|
2847
|
|
|
default: |
2848
|
|
|
// Ignore if its a cached collection |
2849
|
498 |
|
if (isset($hints[Query::HINT_CACHE_ENABLED]) && $class->getFieldValue($entity, $field) instanceof PersistentCollection) { |
2850
|
|
|
break; |
2851
|
|
|
} |
2852
|
|
|
|
2853
|
|
|
// use the given collection |
2854
|
498 |
|
if (isset($data[$field]) && $data[$field] instanceof PersistentCollection) { |
2855
|
|
|
|
2856
|
3 |
|
$data[$field]->setOwner($entity, $assoc); |
2857
|
|
|
|
2858
|
3 |
|
$class->reflFields[$field]->setValue($entity, $data[$field]); |
2859
|
3 |
|
$this->originalEntityData[$oid][$field] = $data[$field]; |
2860
|
|
|
|
2861
|
3 |
|
break; |
2862
|
|
|
} |
2863
|
|
|
|
2864
|
|
|
// Inject collection |
2865
|
498 |
|
$pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection); |
2866
|
498 |
|
$pColl->setOwner($entity, $assoc); |
2867
|
498 |
|
$pColl->setInitialized(false); |
2868
|
|
|
|
2869
|
498 |
|
$reflField = $class->reflFields[$field]; |
2870
|
498 |
|
$reflField->setValue($entity, $pColl); |
2871
|
|
|
|
2872
|
498 |
|
if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) { |
2873
|
4 |
|
$this->loadCollection($pColl); |
2874
|
4 |
|
$pColl->takeSnapshot(); |
2875
|
|
|
} |
2876
|
|
|
|
2877
|
498 |
|
$this->originalEntityData[$oid][$field] = $pColl; |
2878
|
588 |
|
break; |
2879
|
|
|
} |
2880
|
|
|
} |
2881
|
|
|
|
2882
|
|
|
// defer invoking of postLoad event to hydration complete step |
2883
|
714 |
|
$this->hydrationCompleteHandler->deferPostLoadInvoking($class, $entity); |
2884
|
|
|
|
2885
|
714 |
|
return $entity; |
2886
|
|
|
} |
2887
|
|
|
|
2888
|
|
|
/** |
2889
|
|
|
* @return void |
2890
|
|
|
*/ |
2891
|
925 |
|
public function triggerEagerLoads() |
2892
|
|
|
{ |
2893
|
925 |
|
if ( ! $this->eagerLoadingEntities) { |
|
|
|
|
2894
|
925 |
|
return; |
2895
|
|
|
} |
2896
|
|
|
|
2897
|
|
|
// avoid infinite recursion |
2898
|
7 |
|
$eagerLoadingEntities = $this->eagerLoadingEntities; |
2899
|
7 |
|
$this->eagerLoadingEntities = []; |
2900
|
|
|
|
2901
|
7 |
|
foreach ($eagerLoadingEntities as $entityName => $ids) { |
2902
|
7 |
|
if ( ! $ids) { |
2903
|
|
|
continue; |
2904
|
|
|
} |
2905
|
|
|
|
2906
|
7 |
|
$class = $this->em->getClassMetadata($entityName); |
2907
|
|
|
|
2908
|
7 |
|
$this->getEntityPersister($entityName)->loadAll( |
2909
|
7 |
|
array_combine($class->identifier, [array_values($ids)]) |
|
|
|
|
2910
|
|
|
); |
2911
|
|
|
} |
2912
|
7 |
|
} |
2913
|
|
|
|
2914
|
|
|
/** |
2915
|
|
|
* Initializes (loads) an uninitialized persistent collection of an entity. |
2916
|
|
|
* |
2917
|
|
|
* @param \Doctrine\ORM\PersistentCollection $collection The collection to initialize. |
2918
|
|
|
* |
2919
|
|
|
* @return void |
2920
|
|
|
* |
2921
|
|
|
* @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733. |
2922
|
|
|
*/ |
2923
|
149 |
|
public function loadCollection(PersistentCollection $collection) |
2924
|
|
|
{ |
2925
|
149 |
|
$assoc = $collection->getMapping(); |
2926
|
149 |
|
$persister = $this->getEntityPersister($assoc['targetEntity']); |
2927
|
|
|
|
2928
|
149 |
|
switch ($assoc['type']) { |
2929
|
149 |
|
case ClassMetadata::ONE_TO_MANY: |
2930
|
79 |
|
$persister->loadOneToManyCollection($assoc, $collection->getOwner(), $collection); |
2931
|
79 |
|
break; |
2932
|
|
|
|
2933
|
85 |
|
case ClassMetadata::MANY_TO_MANY: |
2934
|
85 |
|
$persister->loadManyToManyCollection($assoc, $collection->getOwner(), $collection); |
2935
|
85 |
|
break; |
2936
|
|
|
} |
2937
|
|
|
|
2938
|
149 |
|
$collection->setInitialized(true); |
2939
|
149 |
|
} |
2940
|
|
|
|
2941
|
|
|
/** |
2942
|
|
|
* Gets the identity map of the UnitOfWork. |
2943
|
|
|
* |
2944
|
|
|
* @return array |
2945
|
|
|
*/ |
2946
|
2 |
|
public function getIdentityMap() |
2947
|
|
|
{ |
2948
|
2 |
|
return $this->identityMap; |
2949
|
|
|
} |
2950
|
|
|
|
2951
|
|
|
/** |
2952
|
|
|
* Gets the original data of an entity. The original data is the data that was |
2953
|
|
|
* present at the time the entity was reconstituted from the database. |
2954
|
|
|
* |
2955
|
|
|
* @param object $entity |
2956
|
|
|
* |
2957
|
|
|
* @return array |
2958
|
|
|
*/ |
2959
|
122 |
|
public function getOriginalEntityData($entity) |
2960
|
|
|
{ |
2961
|
122 |
|
$oid = spl_object_hash($entity); |
2962
|
|
|
|
2963
|
122 |
|
return isset($this->originalEntityData[$oid]) |
2964
|
118 |
|
? $this->originalEntityData[$oid] |
2965
|
122 |
|
: []; |
2966
|
|
|
} |
2967
|
|
|
|
2968
|
|
|
/** |
2969
|
|
|
* @ignore |
2970
|
|
|
* |
2971
|
|
|
* @param object $entity |
2972
|
|
|
* @param array $data |
2973
|
|
|
* |
2974
|
|
|
* @return void |
2975
|
|
|
*/ |
2976
|
|
|
public function setOriginalEntityData($entity, array $data) |
2977
|
|
|
{ |
2978
|
|
|
$this->originalEntityData[spl_object_hash($entity)] = $data; |
2979
|
|
|
} |
2980
|
|
|
|
2981
|
|
|
/** |
2982
|
|
|
* INTERNAL: |
2983
|
|
|
* Sets a property value of the original data array of an entity. |
2984
|
|
|
* |
2985
|
|
|
* @ignore |
2986
|
|
|
* |
2987
|
|
|
* @param string $oid |
2988
|
|
|
* @param string $property |
2989
|
|
|
* @param mixed $value |
2990
|
|
|
* |
2991
|
|
|
* @return void |
2992
|
|
|
*/ |
2993
|
315 |
|
public function setOriginalEntityProperty($oid, $property, $value) |
2994
|
|
|
{ |
2995
|
315 |
|
$this->originalEntityData[$oid][$property] = $value; |
2996
|
315 |
|
} |
2997
|
|
|
|
2998
|
|
|
/** |
2999
|
|
|
* Gets the identifier of an entity. |
3000
|
|
|
* The returned value is always an array of identifier values. If the entity |
3001
|
|
|
* has a composite identifier then the identifier values are in the same |
3002
|
|
|
* order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames(). |
3003
|
|
|
* |
3004
|
|
|
* @param object $entity |
3005
|
|
|
* |
3006
|
|
|
* @return array The identifier values. |
3007
|
|
|
*/ |
3008
|
877 |
|
public function getEntityIdentifier($entity) |
3009
|
|
|
{ |
3010
|
877 |
|
return $this->entityIdentifiers[spl_object_hash($entity)]; |
3011
|
|
|
} |
3012
|
|
|
|
3013
|
|
|
/** |
3014
|
|
|
* Processes an entity instance to extract their identifier values. |
3015
|
|
|
* |
3016
|
|
|
* @param object $entity The entity instance. |
3017
|
|
|
* |
3018
|
|
|
* @return mixed A scalar value. |
3019
|
|
|
* |
3020
|
|
|
* @throws \Doctrine\ORM\ORMInvalidArgumentException |
3021
|
|
|
*/ |
3022
|
139 |
|
public function getSingleIdentifierValue($entity) |
3023
|
|
|
{ |
3024
|
139 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
3025
|
|
|
|
3026
|
134 |
|
if ($class->isIdentifierComposite) { |
|
|
|
|
3027
|
|
|
throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
3028
|
|
|
} |
3029
|
|
|
|
3030
|
134 |
|
$values = $this->isInIdentityMap($entity) |
3031
|
120 |
|
? $this->getEntityIdentifier($entity) |
3032
|
134 |
|
: $class->getIdentifierValues($entity); |
3033
|
|
|
|
3034
|
134 |
|
return isset($values[$class->identifier[0]]) ? $values[$class->identifier[0]] : null; |
|
|
|
|
3035
|
|
|
} |
3036
|
|
|
|
3037
|
|
|
/** |
3038
|
|
|
* Tries to find an entity with the given identifier in the identity map of |
3039
|
|
|
* this UnitOfWork. |
3040
|
|
|
* |
3041
|
|
|
* @param mixed $id The entity identifier to look for. |
3042
|
|
|
* @param string $rootClassName The name of the root class of the mapped entity hierarchy. |
3043
|
|
|
* |
3044
|
|
|
* @return object|bool Returns the entity with the specified identifier if it exists in |
3045
|
|
|
* this UnitOfWork, FALSE otherwise. |
3046
|
|
|
*/ |
3047
|
559 |
|
public function tryGetById($id, $rootClassName) |
3048
|
|
|
{ |
3049
|
559 |
|
$idHash = implode(' ', (array) $id); |
3050
|
|
|
|
3051
|
559 |
|
return isset($this->identityMap[$rootClassName][$idHash]) |
3052
|
89 |
|
? $this->identityMap[$rootClassName][$idHash] |
3053
|
559 |
|
: false; |
3054
|
|
|
} |
3055
|
|
|
|
3056
|
|
|
/** |
3057
|
|
|
* Schedules an entity for dirty-checking at commit-time. |
3058
|
|
|
* |
3059
|
|
|
* @param object $entity The entity to schedule for dirty-checking. |
3060
|
|
|
* |
3061
|
|
|
* @return void |
3062
|
|
|
* |
3063
|
|
|
* @todo Rename: scheduleForSynchronization |
3064
|
|
|
*/ |
3065
|
6 |
|
public function scheduleForDirtyCheck($entity) |
3066
|
|
|
{ |
3067
|
6 |
|
$rootClassName = $this->em->getClassMetadata(get_class($entity))->rootEntityName; |
|
|
|
|
3068
|
|
|
|
3069
|
6 |
|
$this->scheduledForSynchronization[$rootClassName][spl_object_hash($entity)] = $entity; |
3070
|
6 |
|
} |
3071
|
|
|
|
3072
|
|
|
/** |
3073
|
|
|
* Checks whether the UnitOfWork has any pending insertions. |
3074
|
|
|
* |
3075
|
|
|
* @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
3076
|
|
|
*/ |
3077
|
|
|
public function hasPendingInsertions() |
3078
|
|
|
{ |
3079
|
|
|
return ! empty($this->entityInsertions); |
3080
|
|
|
} |
3081
|
|
|
|
3082
|
|
|
/** |
3083
|
|
|
* Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
3084
|
|
|
* number of entities in the identity map. |
3085
|
|
|
* |
3086
|
|
|
* @return integer |
3087
|
|
|
*/ |
3088
|
1 |
|
public function size() |
3089
|
|
|
{ |
3090
|
1 |
|
$countArray = array_map('count', $this->identityMap); |
3091
|
|
|
|
3092
|
1 |
|
return array_sum($countArray); |
3093
|
|
|
} |
3094
|
|
|
|
3095
|
|
|
/** |
3096
|
|
|
* Gets the EntityPersister for an Entity. |
3097
|
|
|
* |
3098
|
|
|
* @param string $entityName The name of the Entity. |
3099
|
|
|
* |
3100
|
|
|
* @return \Doctrine\ORM\Persisters\Entity\EntityPersister |
3101
|
|
|
*/ |
3102
|
1140 |
|
public function getEntityPersister($entityName) |
3103
|
|
|
{ |
3104
|
1140 |
|
if (isset($this->persisters[$entityName])) { |
3105
|
899 |
|
return $this->persisters[$entityName]; |
3106
|
|
|
} |
3107
|
|
|
|
3108
|
1140 |
|
$class = $this->em->getClassMetadata($entityName); |
3109
|
|
|
|
3110
|
|
|
switch (true) { |
3111
|
1140 |
|
case ($class->isInheritanceTypeNone()): |
3112
|
1091 |
|
$persister = new BasicEntityPersister($this->em, $class); |
3113
|
1091 |
|
break; |
3114
|
|
|
|
3115
|
394 |
|
case ($class->isInheritanceTypeSingleTable()): |
3116
|
226 |
|
$persister = new SingleTablePersister($this->em, $class); |
3117
|
226 |
|
break; |
3118
|
|
|
|
3119
|
361 |
|
case ($class->isInheritanceTypeJoined()): |
3120
|
361 |
|
$persister = new JoinedSubclassPersister($this->em, $class); |
3121
|
361 |
|
break; |
3122
|
|
|
|
3123
|
|
|
default: |
3124
|
|
|
throw new \RuntimeException('No persister found for entity.'); |
3125
|
|
|
} |
3126
|
|
|
|
3127
|
1140 |
|
if ($this->hasCache && $class->cache !== null) { |
|
|
|
|
3128
|
126 |
|
$persister = $this->em->getConfiguration() |
3129
|
126 |
|
->getSecondLevelCacheConfiguration() |
3130
|
126 |
|
->getCacheFactory() |
3131
|
126 |
|
->buildCachedEntityPersister($this->em, $persister, $class); |
3132
|
|
|
} |
3133
|
|
|
|
3134
|
1140 |
|
$this->persisters[$entityName] = $persister; |
3135
|
|
|
|
3136
|
1140 |
|
return $this->persisters[$entityName]; |
3137
|
|
|
} |
3138
|
|
|
|
3139
|
|
|
/** |
3140
|
|
|
* Gets a collection persister for a collection-valued association. |
3141
|
|
|
* |
3142
|
|
|
* @param array $association |
3143
|
|
|
* |
3144
|
|
|
* @return \Doctrine\ORM\Persisters\Collection\CollectionPersister |
3145
|
|
|
*/ |
3146
|
582 |
|
public function getCollectionPersister(array $association) |
3147
|
|
|
{ |
3148
|
582 |
|
$role = isset($association['cache']) |
3149
|
78 |
|
? $association['sourceEntity'] . '::' . $association['fieldName'] |
3150
|
582 |
|
: $association['type']; |
3151
|
|
|
|
3152
|
582 |
|
if (isset($this->collectionPersisters[$role])) { |
3153
|
457 |
|
return $this->collectionPersisters[$role]; |
3154
|
|
|
} |
3155
|
|
|
|
3156
|
582 |
|
$persister = ClassMetadata::ONE_TO_MANY === $association['type'] |
3157
|
411 |
|
? new OneToManyPersister($this->em) |
3158
|
582 |
|
: new ManyToManyPersister($this->em); |
3159
|
|
|
|
3160
|
582 |
|
if ($this->hasCache && isset($association['cache'])) { |
3161
|
77 |
|
$persister = $this->em->getConfiguration() |
3162
|
77 |
|
->getSecondLevelCacheConfiguration() |
3163
|
77 |
|
->getCacheFactory() |
3164
|
77 |
|
->buildCachedCollectionPersister($this->em, $persister, $association); |
3165
|
|
|
} |
3166
|
|
|
|
3167
|
582 |
|
$this->collectionPersisters[$role] = $persister; |
3168
|
|
|
|
3169
|
582 |
|
return $this->collectionPersisters[$role]; |
3170
|
|
|
} |
3171
|
|
|
|
3172
|
|
|
/** |
3173
|
|
|
* INTERNAL: |
3174
|
|
|
* Registers an entity as managed. |
3175
|
|
|
* |
3176
|
|
|
* @param object $entity The entity. |
3177
|
|
|
* @param array $id The identifier values. |
3178
|
|
|
* @param array $data The original entity data. |
3179
|
|
|
* |
3180
|
|
|
* @return void |
3181
|
|
|
*/ |
3182
|
211 |
|
public function registerManaged($entity, array $id, array $data) |
3183
|
|
|
{ |
3184
|
211 |
|
$oid = spl_object_hash($entity); |
3185
|
|
|
|
3186
|
211 |
|
$this->entityIdentifiers[$oid] = $id; |
3187
|
211 |
|
$this->entityStates[$oid] = self::STATE_MANAGED; |
3188
|
211 |
|
$this->originalEntityData[$oid] = $data; |
3189
|
|
|
|
3190
|
211 |
|
$this->addToIdentityMap($entity); |
3191
|
|
|
|
3192
|
205 |
|
if ($entity instanceof NotifyPropertyChanged && ( ! $entity instanceof Proxy || $entity->__isInitialized())) { |
3193
|
2 |
|
$entity->addPropertyChangedListener($this); |
3194
|
|
|
} |
3195
|
205 |
|
} |
3196
|
|
|
|
3197
|
|
|
/** |
3198
|
|
|
* INTERNAL: |
3199
|
|
|
* Clears the property changeset of the entity with the given OID. |
3200
|
|
|
* |
3201
|
|
|
* @param string $oid The entity's OID. |
3202
|
|
|
* |
3203
|
|
|
* @return void |
3204
|
|
|
*/ |
3205
|
16 |
|
public function clearEntityChangeSet($oid) |
3206
|
|
|
{ |
3207
|
16 |
|
unset($this->entityChangeSets[$oid]); |
3208
|
16 |
|
} |
3209
|
|
|
|
3210
|
|
|
/* PropertyChangedListener implementation */ |
3211
|
|
|
|
3212
|
|
|
/** |
3213
|
|
|
* Notifies this UnitOfWork of a property change in an entity. |
3214
|
|
|
* |
3215
|
|
|
* @param object $entity The entity that owns the property. |
3216
|
|
|
* @param string $propertyName The name of the property that changed. |
3217
|
|
|
* @param mixed $oldValue The old value of the property. |
3218
|
|
|
* @param mixed $newValue The new value of the property. |
3219
|
|
|
* |
3220
|
|
|
* @return void |
3221
|
|
|
*/ |
3222
|
4 |
|
public function propertyChanged($entity, $propertyName, $oldValue, $newValue) |
3223
|
|
|
{ |
3224
|
4 |
|
$oid = spl_object_hash($entity); |
3225
|
4 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
3226
|
|
|
|
3227
|
4 |
|
$isAssocField = isset($class->associationMappings[$propertyName]); |
|
|
|
|
3228
|
|
|
|
3229
|
4 |
|
if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) { |
|
|
|
|
3230
|
1 |
|
return; // ignore non-persistent fields |
3231
|
|
|
} |
3232
|
|
|
|
3233
|
|
|
// Update changeset and mark entity for synchronization |
3234
|
4 |
|
$this->entityChangeSets[$oid][$propertyName] = [$oldValue, $newValue]; |
3235
|
|
|
|
3236
|
4 |
|
if ( ! isset($this->scheduledForSynchronization[$class->rootEntityName][$oid])) { |
|
|
|
|
3237
|
4 |
|
$this->scheduleForDirtyCheck($entity); |
3238
|
|
|
} |
3239
|
4 |
|
} |
3240
|
|
|
|
3241
|
|
|
/** |
3242
|
|
|
* Gets the currently scheduled entity insertions in this UnitOfWork. |
3243
|
|
|
* |
3244
|
|
|
* @return array |
3245
|
|
|
*/ |
3246
|
2 |
|
public function getScheduledEntityInsertions() |
3247
|
|
|
{ |
3248
|
2 |
|
return $this->entityInsertions; |
3249
|
|
|
} |
3250
|
|
|
|
3251
|
|
|
/** |
3252
|
|
|
* Gets the currently scheduled entity updates in this UnitOfWork. |
3253
|
|
|
* |
3254
|
|
|
* @return array |
3255
|
|
|
*/ |
3256
|
3 |
|
public function getScheduledEntityUpdates() |
3257
|
|
|
{ |
3258
|
3 |
|
return $this->entityUpdates; |
3259
|
|
|
} |
3260
|
|
|
|
3261
|
|
|
/** |
3262
|
|
|
* Gets the currently scheduled entity deletions in this UnitOfWork. |
3263
|
|
|
* |
3264
|
|
|
* @return array |
3265
|
|
|
*/ |
3266
|
1 |
|
public function getScheduledEntityDeletions() |
3267
|
|
|
{ |
3268
|
1 |
|
return $this->entityDeletions; |
3269
|
|
|
} |
3270
|
|
|
|
3271
|
|
|
/** |
3272
|
|
|
* Gets the currently scheduled complete collection deletions |
3273
|
|
|
* |
3274
|
|
|
* @return array |
3275
|
|
|
*/ |
3276
|
1 |
|
public function getScheduledCollectionDeletions() |
3277
|
|
|
{ |
3278
|
1 |
|
return $this->collectionDeletions; |
3279
|
|
|
} |
3280
|
|
|
|
3281
|
|
|
/** |
3282
|
|
|
* Gets the currently scheduled collection inserts, updates and deletes. |
3283
|
|
|
* |
3284
|
|
|
* @return array |
3285
|
|
|
*/ |
3286
|
|
|
public function getScheduledCollectionUpdates() |
3287
|
|
|
{ |
3288
|
|
|
return $this->collectionUpdates; |
3289
|
|
|
} |
3290
|
|
|
|
3291
|
|
|
/** |
3292
|
|
|
* Helper method to initialize a lazy loading proxy or persistent collection. |
3293
|
|
|
* |
3294
|
|
|
* @param object $obj |
3295
|
|
|
* |
3296
|
|
|
* @return void |
3297
|
|
|
*/ |
3298
|
2 |
|
public function initializeObject($obj) |
3299
|
|
|
{ |
3300
|
2 |
|
if ($obj instanceof Proxy) { |
3301
|
1 |
|
$obj->__load(); |
3302
|
|
|
|
3303
|
1 |
|
return; |
3304
|
|
|
} |
3305
|
|
|
|
3306
|
1 |
|
if ($obj instanceof PersistentCollection) { |
3307
|
1 |
|
$obj->initialize(); |
3308
|
|
|
} |
3309
|
1 |
|
} |
3310
|
|
|
|
3311
|
|
|
/** |
3312
|
|
|
* Helper method to show an object as string. |
3313
|
|
|
* |
3314
|
|
|
* @param object $obj |
3315
|
|
|
* |
3316
|
|
|
* @return string |
3317
|
|
|
*/ |
3318
|
1 |
|
private static function objToStr($obj) |
3319
|
|
|
{ |
3320
|
1 |
|
return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj); |
3321
|
|
|
} |
3322
|
|
|
|
3323
|
|
|
/** |
3324
|
|
|
* Marks an entity as read-only so that it will not be considered for updates during UnitOfWork#commit(). |
3325
|
|
|
* |
3326
|
|
|
* This operation cannot be undone as some parts of the UnitOfWork now keep gathering information |
3327
|
|
|
* on this object that might be necessary to perform a correct update. |
3328
|
|
|
* |
3329
|
|
|
* @param object $object |
3330
|
|
|
* |
3331
|
|
|
* @return void |
3332
|
|
|
* |
3333
|
|
|
* @throws ORMInvalidArgumentException |
3334
|
|
|
*/ |
3335
|
6 |
|
public function markReadOnly($object) |
3336
|
|
|
{ |
3337
|
6 |
|
if ( ! is_object($object) || ! $this->isInIdentityMap($object)) { |
3338
|
1 |
|
throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object); |
3339
|
|
|
} |
3340
|
|
|
|
3341
|
5 |
|
$this->readOnlyObjects[spl_object_hash($object)] = true; |
3342
|
5 |
|
} |
3343
|
|
|
|
3344
|
|
|
/** |
3345
|
|
|
* Is this entity read only? |
3346
|
|
|
* |
3347
|
|
|
* @param object $object |
3348
|
|
|
* |
3349
|
|
|
* @return bool |
3350
|
|
|
* |
3351
|
|
|
* @throws ORMInvalidArgumentException |
3352
|
|
|
*/ |
3353
|
3 |
|
public function isReadOnly($object) |
3354
|
|
|
{ |
3355
|
3 |
|
if ( ! is_object($object)) { |
3356
|
|
|
throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object); |
3357
|
|
|
} |
3358
|
|
|
|
3359
|
3 |
|
return isset($this->readOnlyObjects[spl_object_hash($object)]); |
3360
|
|
|
} |
3361
|
|
|
|
3362
|
|
|
/** |
3363
|
|
|
* Perform whatever processing is encapsulated here after completion of the transaction. |
3364
|
|
|
*/ |
3365
|
|
|
private function afterTransactionComplete() |
3366
|
|
|
{ |
3367
|
1074 |
|
$this->performCallbackOnCachedPersister(function (CachedPersister $persister) { |
3368
|
95 |
|
$persister->afterTransactionComplete(); |
3369
|
1074 |
|
}); |
3370
|
1074 |
|
} |
3371
|
|
|
|
3372
|
|
|
/** |
3373
|
|
|
* Perform whatever processing is encapsulated here after completion of the rolled-back. |
3374
|
|
|
*/ |
3375
|
|
|
private function afterTransactionRolledBack() |
3376
|
|
|
{ |
3377
|
11 |
|
$this->performCallbackOnCachedPersister(function (CachedPersister $persister) { |
3378
|
3 |
|
$persister->afterTransactionRolledBack(); |
3379
|
11 |
|
}); |
3380
|
11 |
|
} |
3381
|
|
|
|
3382
|
|
|
/** |
3383
|
|
|
* Performs an action after the transaction. |
3384
|
|
|
* |
3385
|
|
|
* @param callable $callback |
3386
|
|
|
*/ |
3387
|
1079 |
|
private function performCallbackOnCachedPersister(callable $callback) |
3388
|
|
|
{ |
3389
|
1079 |
|
if ( ! $this->hasCache) { |
3390
|
984 |
|
return; |
3391
|
|
|
} |
3392
|
|
|
|
3393
|
95 |
|
foreach (array_merge($this->persisters, $this->collectionPersisters) as $persister) { |
3394
|
95 |
|
if ($persister instanceof CachedPersister) { |
3395
|
95 |
|
$callback($persister); |
3396
|
|
|
} |
3397
|
|
|
} |
3398
|
95 |
|
} |
3399
|
|
|
|
3400
|
1083 |
|
private function dispatchOnFlushEvent() |
3401
|
|
|
{ |
3402
|
1083 |
|
if ($this->evm->hasListeners(Events::onFlush)) { |
3403
|
4 |
|
$this->evm->dispatchEvent(Events::onFlush, new OnFlushEventArgs($this->em)); |
3404
|
|
|
} |
3405
|
1083 |
|
} |
3406
|
|
|
|
3407
|
1078 |
|
private function dispatchPostFlushEvent() |
3408
|
|
|
{ |
3409
|
1078 |
|
if ($this->evm->hasListeners(Events::postFlush)) { |
3410
|
5 |
|
$this->evm->dispatchEvent(Events::postFlush, new PostFlushEventArgs($this->em)); |
3411
|
|
|
} |
3412
|
1077 |
|
} |
3413
|
|
|
|
3414
|
|
|
/** |
3415
|
|
|
* Verifies if two given entities actually are the same based on identifier comparison |
3416
|
|
|
* |
3417
|
|
|
* @param object $entity1 |
3418
|
|
|
* @param object $entity2 |
3419
|
|
|
* |
3420
|
|
|
* @return bool |
3421
|
|
|
*/ |
3422
|
14 |
|
private function isIdentifierEquals($entity1, $entity2) |
3423
|
|
|
{ |
3424
|
14 |
|
if ($entity1 === $entity2) { |
3425
|
|
|
return true; |
3426
|
|
|
} |
3427
|
|
|
|
3428
|
14 |
|
$class = $this->em->getClassMetadata(get_class($entity1)); |
3429
|
|
|
|
3430
|
14 |
|
if ($class !== $this->em->getClassMetadata(get_class($entity2))) { |
3431
|
11 |
|
return false; |
3432
|
|
|
} |
3433
|
|
|
|
3434
|
3 |
|
$oid1 = spl_object_hash($entity1); |
3435
|
3 |
|
$oid2 = spl_object_hash($entity2); |
3436
|
|
|
|
3437
|
3 |
|
$id1 = isset($this->entityIdentifiers[$oid1]) |
3438
|
3 |
|
? $this->entityIdentifiers[$oid1] |
3439
|
3 |
|
: $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($entity1)); |
3440
|
3 |
|
$id2 = isset($this->entityIdentifiers[$oid2]) |
3441
|
3 |
|
? $this->entityIdentifiers[$oid2] |
3442
|
3 |
|
: $this->identifierFlattener->flattenIdentifier($class, $class->getIdentifierValues($entity2)); |
3443
|
|
|
|
3444
|
3 |
|
return $id1 === $id2 || implode(' ', $id1) === implode(' ', $id2); |
3445
|
|
|
} |
3446
|
|
|
|
3447
|
|
|
/** |
3448
|
|
|
* @throws ORMInvalidArgumentException |
3449
|
|
|
*/ |
3450
|
1081 |
|
private function assertThatThereAreNoUnintentionallyNonPersistedAssociations() : void |
3451
|
|
|
{ |
3452
|
1081 |
|
$entitiesNeedingCascadePersist = \array_diff_key($this->nonCascadedNewDetectedEntities, $this->entityInsertions); |
3453
|
|
|
|
3454
|
1081 |
|
$this->nonCascadedNewDetectedEntities = []; |
3455
|
|
|
|
3456
|
1081 |
|
if ($entitiesNeedingCascadePersist) { |
|
|
|
|
3457
|
5 |
|
throw ORMInvalidArgumentException::newEntitiesFoundThroughRelationships( |
3458
|
5 |
|
\array_values($entitiesNeedingCascadePersist) |
3459
|
|
|
); |
3460
|
|
|
} |
3461
|
1079 |
|
} |
3462
|
|
|
|
3463
|
|
|
/** |
3464
|
|
|
* @param object $entity |
3465
|
|
|
* @param object $managedCopy |
3466
|
|
|
* |
3467
|
|
|
* @throws ORMException |
3468
|
|
|
* @throws OptimisticLockException |
3469
|
|
|
* @throws TransactionRequiredException |
3470
|
|
|
*/ |
3471
|
41 |
|
private function mergeEntityStateIntoManagedCopy($entity, $managedCopy) |
3472
|
|
|
{ |
3473
|
41 |
|
if (! $this->isLoaded($entity)) { |
3474
|
7 |
|
return; |
3475
|
|
|
} |
3476
|
|
|
|
3477
|
34 |
|
if (! $this->isLoaded($managedCopy)) { |
3478
|
4 |
|
$managedCopy->__load(); |
3479
|
|
|
} |
3480
|
|
|
|
3481
|
34 |
|
$class = $this->em->getClassMetadata(get_class($entity)); |
3482
|
|
|
|
3483
|
34 |
|
foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) { |
3484
|
34 |
|
$name = $prop->name; |
3485
|
|
|
|
3486
|
34 |
|
$prop->setAccessible(true); |
3487
|
|
|
|
3488
|
34 |
|
if ( ! isset($class->associationMappings[$name])) { |
|
|
|
|
3489
|
34 |
|
if ( ! $class->isIdentifier($name)) { |
3490
|
34 |
|
$prop->setValue($managedCopy, $prop->getValue($entity)); |
3491
|
|
|
} |
3492
|
|
|
} else { |
3493
|
30 |
|
$assoc2 = $class->associationMappings[$name]; |
3494
|
|
|
|
3495
|
30 |
|
if ($assoc2['type'] & ClassMetadata::TO_ONE) { |
3496
|
26 |
|
$other = $prop->getValue($entity); |
3497
|
26 |
|
if ($other === null) { |
3498
|
13 |
|
$prop->setValue($managedCopy, null); |
3499
|
|
|
} else { |
3500
|
17 |
|
if ($other instanceof Proxy && !$other->__isInitialized()) { |
3501
|
|
|
// do not merge fields marked lazy that have not been fetched. |
3502
|
4 |
|
continue; |
3503
|
|
|
} |
3504
|
|
|
|
3505
|
13 |
|
if ( ! $assoc2['isCascadeMerge']) { |
3506
|
7 |
|
if ($this->getEntityState($other) === self::STATE_DETACHED) { |
3507
|
4 |
|
$targetClass = $this->em->getClassMetadata($assoc2['targetEntity']); |
3508
|
4 |
|
$relatedId = $targetClass->getIdentifierValues($other); |
3509
|
|
|
|
3510
|
4 |
|
if ($targetClass->subClasses) { |
|
|
|
|
3511
|
2 |
|
$other = $this->em->find($targetClass->name, $relatedId); |
|
|
|
|
3512
|
|
|
} else { |
3513
|
2 |
|
$other = $this->em->getProxyFactory()->getProxy( |
3514
|
2 |
|
$assoc2['targetEntity'], |
3515
|
2 |
|
$relatedId |
3516
|
|
|
); |
3517
|
2 |
|
$this->registerManaged($other, $relatedId, []); |
3518
|
|
|
} |
3519
|
|
|
} |
3520
|
|
|
|
3521
|
22 |
|
$prop->setValue($managedCopy, $other); |
3522
|
|
|
} |
3523
|
|
|
} |
3524
|
|
|
} else { |
3525
|
18 |
|
$mergeCol = $prop->getValue($entity); |
3526
|
|
|
|
3527
|
18 |
|
if ($mergeCol instanceof PersistentCollection && ! $mergeCol->isInitialized()) { |
3528
|
|
|
// do not merge fields marked lazy that have not been fetched. |
3529
|
|
|
// keep the lazy persistent collection of the managed copy. |
3530
|
5 |
|
continue; |
3531
|
|
|
} |
3532
|
|
|
|
3533
|
15 |
|
$managedCol = $prop->getValue($managedCopy); |
3534
|
|
|
|
3535
|
15 |
|
if ( ! $managedCol) { |
3536
|
4 |
|
$managedCol = new PersistentCollection( |
3537
|
4 |
|
$this->em, |
3538
|
4 |
|
$this->em->getClassMetadata($assoc2['targetEntity']), |
3539
|
4 |
|
new ArrayCollection |
3540
|
|
|
); |
3541
|
4 |
|
$managedCol->setOwner($managedCopy, $assoc2); |
3542
|
4 |
|
$prop->setValue($managedCopy, $managedCol); |
3543
|
|
|
} |
3544
|
|
|
|
3545
|
15 |
|
if ($assoc2['isCascadeMerge']) { |
3546
|
10 |
|
$managedCol->initialize(); |
3547
|
|
|
|
3548
|
|
|
// clear and set dirty a managed collection if its not also the same collection to merge from. |
3549
|
10 |
|
if ( ! $managedCol->isEmpty() && $managedCol !== $mergeCol) { |
3550
|
1 |
|
$managedCol->unwrap()->clear(); |
3551
|
1 |
|
$managedCol->setDirty(true); |
3552
|
|
|
|
3553
|
1 |
|
if ($assoc2['isOwningSide'] |
3554
|
1 |
|
&& $assoc2['type'] == ClassMetadata::MANY_TO_MANY |
3555
|
1 |
|
&& $class->isChangeTrackingNotify() |
3556
|
|
|
) { |
3557
|
|
|
$this->scheduleForDirtyCheck($managedCopy); |
3558
|
|
|
} |
3559
|
|
|
} |
3560
|
|
|
} |
3561
|
|
|
} |
3562
|
|
|
} |
3563
|
|
|
|
3564
|
34 |
|
if ($class->isChangeTrackingNotify()) { |
3565
|
|
|
// Just treat all properties as changed, there is no other choice. |
3566
|
34 |
|
$this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); |
3567
|
|
|
} |
3568
|
|
|
} |
3569
|
34 |
|
} |
3570
|
|
|
|
3571
|
|
|
/** |
3572
|
|
|
* This method called by hydrators, and indicates that hydrator totally completed current hydration cycle. |
3573
|
|
|
* Unit of work able to fire deferred events, related to loading events here. |
3574
|
|
|
* |
3575
|
|
|
* @internal should be called internally from object hydrators |
3576
|
|
|
*/ |
3577
|
940 |
|
public function hydrationComplete() |
3578
|
|
|
{ |
3579
|
940 |
|
$this->hydrationCompleteHandler->hydrationComplete(); |
3580
|
940 |
|
} |
3581
|
|
|
|
3582
|
|
|
/** |
3583
|
|
|
* @param string $entityName |
3584
|
|
|
*/ |
3585
|
4 |
|
private function clearIdentityMapForEntityName($entityName) |
3586
|
|
|
{ |
3587
|
4 |
|
if (! isset($this->identityMap[$entityName])) { |
3588
|
|
|
return; |
3589
|
|
|
} |
3590
|
|
|
|
3591
|
4 |
|
$visited = []; |
3592
|
|
|
|
3593
|
4 |
|
foreach ($this->identityMap[$entityName] as $entity) { |
3594
|
4 |
|
$this->doDetach($entity, $visited, false); |
3595
|
|
|
} |
3596
|
4 |
|
} |
3597
|
|
|
|
3598
|
|
|
/** |
3599
|
|
|
* @param string $entityName |
3600
|
|
|
*/ |
3601
|
4 |
|
private function clearEntityInsertionsForEntityName($entityName) |
3602
|
|
|
{ |
3603
|
4 |
|
foreach ($this->entityInsertions as $hash => $entity) { |
3604
|
|
|
// note: performance optimization - `instanceof` is much faster than a function call |
3605
|
1 |
|
if ($entity instanceof $entityName && get_class($entity) === $entityName) { |
3606
|
1 |
|
unset($this->entityInsertions[$hash]); |
3607
|
|
|
} |
3608
|
|
|
} |
3609
|
4 |
|
} |
3610
|
|
|
|
3611
|
|
|
/** |
3612
|
|
|
* @param ClassMetadata $class |
3613
|
|
|
* @param mixed $identifierValue |
3614
|
|
|
* |
3615
|
|
|
* @return mixed the identifier after type conversion |
3616
|
|
|
* |
3617
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException if the entity has more than a single identifier |
3618
|
|
|
*/ |
3619
|
977 |
|
private function convertSingleFieldIdentifierToPHPValue(ClassMetadata $class, $identifierValue) |
3620
|
|
|
{ |
3621
|
977 |
|
return $this->em->getConnection()->convertToPHPValue( |
3622
|
977 |
|
$identifierValue, |
3623
|
977 |
|
$class->getTypeOfField($class->getSingleIdentifierFieldName()) |
3624
|
|
|
); |
3625
|
|
|
} |
3626
|
|
|
} |
3627
|
|
|
|