Complex classes like UnitOfWork often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class UnitOfWork implements PropertyChangedListener |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 49 | */ |
||
| 50 | public const STATE_MANAGED = 1; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 54 | * and is not (yet) managed by a DocumentManager. |
||
| 55 | */ |
||
| 56 | public const STATE_NEW = 2; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * A detached document is an instance with a persistent identity that is not |
||
| 60 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 61 | */ |
||
| 62 | public const STATE_DETACHED = 3; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A removed document instance is an instance with a persistent identity, |
||
| 66 | * associated with a DocumentManager, whose persistent state has been |
||
| 67 | * deleted (or is scheduled for deletion). |
||
| 68 | */ |
||
| 69 | public const STATE_REMOVED = 4; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The identity map holds references to all managed documents. |
||
| 73 | * |
||
| 74 | * Documents are grouped by their class name, and then indexed by the |
||
| 75 | * serialized string of their database identifier field or, if the class |
||
| 76 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 77 | * differentiation of values that may be equal (via type juggling) but not |
||
| 78 | * identical. |
||
| 79 | * |
||
| 80 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 81 | * we always take the root class name of the hierarchy. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $identityMap = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Map of all identifiers of managed documents. |
||
| 89 | * Keys are object ids (spl_object_hash). |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $documentIdentifiers = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Map of the original document data of managed documents. |
||
| 97 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 98 | * at commit time. |
||
| 99 | * |
||
| 100 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 101 | * A value will only really be copied if the value in the document is modified |
||
| 102 | * by the user. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $originalDocumentData = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 110 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private $documentChangeSets = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The (cached) states of any known documents. |
||
| 118 | * Keys are object ids (spl_object_hash). |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $documentStates = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 126 | * |
||
| 127 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 128 | * object hash. This is only used for documents with a change tracking |
||
| 129 | * policy of DEFERRED_EXPLICIT. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | */ |
||
| 133 | private $scheduledForSynchronization = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of all pending document insertions. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $documentInsertions = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A list of all pending document updates. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $documentUpdates = []; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A list of all pending document upserts. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $documentUpserts = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending document deletions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $documentDeletions = []; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * All pending collection deletions. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $collectionDeletions = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * All pending collection updates. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $collectionUpdates = []; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of documents related to collections scheduled for update or deletion |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $hasScheduledCollections = []; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 186 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 187 | * of their data. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $visitedCollections = []; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 195 | * |
||
| 196 | * @var DocumentManager |
||
| 197 | */ |
||
| 198 | private $dm; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The EventManager used for dispatching events. |
||
| 202 | * |
||
| 203 | * @var EventManager |
||
| 204 | */ |
||
| 205 | private $evm; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Additional documents that are scheduled for removal. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | private $orphanRemovals = []; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 216 | * |
||
| 217 | * @var HydratorFactory |
||
| 218 | */ |
||
| 219 | private $hydratorFactory; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The document persister instances used to persist document instances. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $persisters = []; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The collection persister instance used to persist changes to collections. |
||
| 230 | * |
||
| 231 | * @var Persisters\CollectionPersister |
||
| 232 | */ |
||
| 233 | private $collectionPersister; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The persistence builder instance used in DocumentPersisters. |
||
| 237 | * |
||
| 238 | * @var PersistenceBuilder|null |
||
| 239 | */ |
||
| 240 | private $persistenceBuilder; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Array of parent associations between embedded documents. |
||
| 244 | * |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | private $parentAssociations = []; |
||
| 248 | |||
| 249 | /** @var LifecycleEventManager */ |
||
| 250 | private $lifecycleEventManager; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 254 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 255 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 256 | * |
||
| 257 | * @var array |
||
| 258 | */ |
||
| 259 | private $embeddedDocumentsRegistry = []; |
||
| 260 | |||
| 261 | /** @var int */ |
||
| 262 | private $commitsInProgress = 0; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 266 | */ |
||
| 267 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
||
| 268 | 1651 | { |
|
| 269 | $this->dm = $dm; |
||
| 270 | 1651 | $this->evm = $evm; |
|
| 271 | 1651 | $this->hydratorFactory = $hydratorFactory; |
|
| 272 | 1651 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 273 | 1651 | } |
|
| 274 | 1651 | ||
| 275 | /** |
||
| 276 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 277 | * queries for insert persistence. |
||
| 278 | */ |
||
| 279 | public function getPersistenceBuilder() : PersistenceBuilder |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the parent association for a given embedded document. |
||
| 289 | */ |
||
| 290 | public function setParentAssociation(object $document, array $mapping, ?object $parent, string $propertyPath) : void |
||
| 291 | 209 | { |
|
| 292 | $oid = spl_object_hash($document); |
||
| 293 | 209 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 294 | 209 | $this->parentAssociations[$oid] = [$mapping, $parent, $propertyPath]; |
|
| 295 | 209 | } |
|
| 296 | 209 | ||
| 297 | /** |
||
| 298 | * Gets the parent association for a given embedded document. |
||
| 299 | * |
||
| 300 | * <code> |
||
| 301 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 302 | * </code> |
||
| 303 | */ |
||
| 304 | public function getParentAssociation(object $document) : ?array |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the document persister instance for the given document name |
||
| 313 | */ |
||
| 314 | public function getDocumentPersister(string $documentName) : Persisters\DocumentPersister |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the collection persister instance. |
||
| 326 | */ |
||
| 327 | public function getCollectionPersister() : CollectionPersister |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Set the document persister instance to use for the given document name |
||
| 338 | */ |
||
| 339 | public function setDocumentPersister(string $documentName, Persisters\DocumentPersister $persister) : void |
||
| 340 | 13 | { |
|
| 341 | $this->persisters[$documentName] = $persister; |
||
| 342 | 13 | } |
|
| 343 | 13 | ||
| 344 | /** |
||
| 345 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 346 | * up to this point. The state of all managed documents will be synchronized with |
||
| 347 | * the database. |
||
| 348 | * |
||
| 349 | * The operations are executed in the following order: |
||
| 350 | * |
||
| 351 | * 1) All document insertions |
||
| 352 | * 2) All document updates |
||
| 353 | * 3) All document deletions |
||
| 354 | * |
||
| 355 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 356 | */ |
||
| 357 | public function commit(array $options = []) : void |
||
| 358 | 611 | { |
|
| 359 | // Raise preFlush |
||
| 360 | if ($this->evm->hasListeners(Events::preFlush)) { |
||
| 361 | 611 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
|
| 362 | } |
||
| 363 | |||
| 364 | // Compute changes done since last commit. |
||
| 365 | $this->computeChangeSets(); |
||
| 366 | 611 | ||
| 367 | if (! ($this->documentInsertions || |
||
|
|
|||
| 368 | 610 | $this->documentUpserts || |
|
| 369 | 257 | $this->documentDeletions || |
|
| 370 | 215 | $this->documentUpdates || |
|
| 371 | 198 | $this->collectionUpdates || |
|
| 372 | 22 | $this->collectionDeletions || |
|
| 373 | 22 | $this->orphanRemovals) |
|
| 374 | 610 | ) { |
|
| 375 | return; // Nothing to do. |
||
| 376 | 22 | } |
|
| 377 | |||
| 378 | $this->commitsInProgress++; |
||
| 379 | 607 | if ($this->commitsInProgress > 1) { |
|
| 380 | 607 | throw MongoDBException::commitInProgress(); |
|
| 381 | } |
||
| 382 | try { |
||
| 383 | if ($this->orphanRemovals) { |
||
| 384 | 607 | foreach ($this->orphanRemovals as $removal) { |
|
| 385 | 55 | $this->remove($removal); |
|
| 386 | 55 | } |
|
| 387 | } |
||
| 388 | |||
| 389 | // Raise onFlush |
||
| 390 | if ($this->evm->hasListeners(Events::onFlush)) { |
||
| 391 | 607 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 392 | 5 | } |
|
| 393 | |||
| 394 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
||
| 395 | 606 | [$class, $documents] = $classAndDocuments; |
|
| 396 | 86 | $this->executeUpserts($class, $documents, $options); |
|
| 397 | 86 | } |
|
| 398 | |||
| 399 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
||
| 400 | 606 | [$class, $documents] = $classAndDocuments; |
|
| 401 | 530 | $this->executeInserts($class, $documents, $options); |
|
| 402 | 530 | } |
|
| 403 | |||
| 404 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
||
| 405 | 593 | [$class, $documents] = $classAndDocuments; |
|
| 406 | 236 | $this->executeUpdates($class, $documents, $options); |
|
| 407 | 236 | } |
|
| 408 | |||
| 409 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
||
| 410 | 593 | [$class, $documents] = $classAndDocuments; |
|
| 411 | 79 | $this->executeDeletions($class, $documents, $options); |
|
| 412 | 79 | } |
|
| 413 | |||
| 414 | // Raise postFlush |
||
| 415 | if ($this->evm->hasListeners(Events::postFlush)) { |
||
| 416 | 593 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
|
| 417 | } |
||
| 418 | |||
| 419 | // Clear up |
||
| 420 | $this->documentInsertions = |
||
| 421 | 593 | $this->documentUpserts = |
|
| 422 | 593 | $this->documentUpdates = |
|
| 423 | 593 | $this->documentDeletions = |
|
| 424 | 593 | $this->documentChangeSets = |
|
| 425 | 593 | $this->collectionUpdates = |
|
| 426 | 593 | $this->collectionDeletions = |
|
| 427 | 593 | $this->visitedCollections = |
|
| 428 | 593 | $this->scheduledForSynchronization = |
|
| 429 | 593 | $this->orphanRemovals = |
|
| 430 | 593 | $this->hasScheduledCollections = []; |
|
| 431 | 593 | } finally { |
|
| 432 | 593 | $this->commitsInProgress--; |
|
| 433 | 607 | } |
|
| 434 | } |
||
| 435 | 593 | ||
| 436 | /** |
||
| 437 | * Groups a list of scheduled documents by their class. |
||
| 438 | */ |
||
| 439 | private function getClassesForCommitAction(array $documents, bool $includeEmbedded = false) : array |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Compute changesets of all documents scheduled for insertion. |
||
| 471 | * |
||
| 472 | * Embedded documents will not be processed. |
||
| 473 | */ |
||
| 474 | private function computeScheduleInsertsChangeSets() : void |
||
| 475 | 616 | { |
|
| 476 | foreach ($this->documentInsertions as $document) { |
||
| 477 | 616 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 478 | 543 | if ($class->isEmbeddedDocument) { |
|
| 479 | 543 | continue; |
|
| 480 | 165 | } |
|
| 481 | |||
| 482 | $this->computeChangeSet($class, $document); |
||
| 483 | 537 | } |
|
| 484 | } |
||
| 485 | 615 | ||
| 486 | /** |
||
| 487 | * Compute changesets of all documents scheduled for upsert. |
||
| 488 | * |
||
| 489 | * Embedded documents will not be processed. |
||
| 490 | */ |
||
| 491 | private function computeScheduleUpsertsChangeSets() : void |
||
| 492 | 615 | { |
|
| 493 | foreach ($this->documentUpserts as $document) { |
||
| 494 | 615 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 495 | 85 | if ($class->isEmbeddedDocument) { |
|
| 496 | 85 | continue; |
|
| 497 | } |
||
| 498 | |||
| 499 | $this->computeChangeSet($class, $document); |
||
| 500 | 85 | } |
|
| 501 | } |
||
| 502 | 615 | ||
| 503 | /** |
||
| 504 | * Gets the changeset for a document. |
||
| 505 | * |
||
| 506 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 507 | */ |
||
| 508 | public function getDocumentChangeSet(object $document) : array |
||
| 514 | |||
| 515 | /** |
||
| 516 | * INTERNAL: |
||
| 517 | * Sets the changeset for a document. |
||
| 518 | */ |
||
| 519 | public function setDocumentChangeSet(object $document, array $changeset) : void |
||
| 520 | 1 | { |
|
| 521 | $this->documentChangeSets[spl_object_hash($document)] = $changeset; |
||
| 522 | 1 | } |
|
| 523 | 1 | ||
| 524 | /** |
||
| 525 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function getDocumentActualData(object $document) : array |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Computes the changes that happened to a single document. |
||
| 562 | * |
||
| 563 | * Modifies/populates the following properties: |
||
| 564 | * |
||
| 565 | * {@link originalDocumentData} |
||
| 566 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 567 | * then it was not fetched from the database and therefore we have no original |
||
| 568 | * document data yet. All of the current document data is stored as the original document data. |
||
| 569 | * |
||
| 570 | * {@link documentChangeSets} |
||
| 571 | * The changes detected on all properties of the document are stored there. |
||
| 572 | * A change is a tuple array where the first entry is the old value and the second |
||
| 573 | * entry is the new value of the property. Changesets are used by persisters |
||
| 574 | * to INSERT/UPDATE the persistent document state. |
||
| 575 | * |
||
| 576 | * {@link documentUpdates} |
||
| 577 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 578 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 579 | * there to mark it for an update. |
||
| 580 | */ |
||
| 581 | public function computeChangeSet(ClassMetadata $class, object $document) : void |
||
| 582 | 612 | { |
|
| 583 | if (! $class->isInheritanceTypeNone()) { |
||
| 584 | 612 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 585 | 183 | } |
|
| 586 | |||
| 587 | // Fire PreFlush lifecycle callbacks |
||
| 588 | if (! empty($class->lifecycleCallbacks[Events::preFlush])) { |
||
| 589 | 612 | $class->invokeLifecycleCallbacks(Events::preFlush, $document, [new Event\PreFlushEventArgs($this->dm)]); |
|
| 590 | 11 | } |
|
| 591 | |||
| 592 | $this->computeOrRecomputeChangeSet($class, $document); |
||
| 593 | 612 | } |
|
| 594 | 611 | ||
| 595 | /** |
||
| 596 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 597 | */ |
||
| 598 | private function computeOrRecomputeChangeSet(ClassMetadata $class, object $document, bool $recompute = false) : void |
||
| 599 | 612 | { |
|
| 600 | $oid = spl_object_hash($document); |
||
| 601 | 612 | $actualData = $this->getDocumentActualData($document); |
|
| 602 | 612 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 603 | 612 | if ($isNewDocument) { |
|
| 604 | 612 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
|
| 605 | // These result in an INSERT. |
||
| 606 | $this->originalDocumentData[$oid] = $actualData; |
||
| 607 | 611 | $changeSet = []; |
|
| 608 | 611 | foreach ($actualData as $propName => $actualValue) { |
|
| 609 | 611 | /* At this PersistentCollection shouldn't be here, probably it |
|
| 610 | * was cloned and its ownership must be fixed |
||
| 611 | */ |
||
| 612 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
||
| 613 | 611 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 614 | $actualValue = $actualData[$propName]; |
||
| 615 | } |
||
| 616 | // ignore inverse side of reference relationship |
||
| 617 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
||
| 618 | 611 | continue; |
|
| 619 | 187 | } |
|
| 620 | $changeSet[$propName] = [null, $actualValue]; |
||
| 621 | 611 | } |
|
| 622 | $this->documentChangeSets[$oid] = $changeSet; |
||
| 623 | 611 | } else { |
|
| 624 | if ($class->isReadOnly) { |
||
| 625 | 297 | return; |
|
| 626 | 2 | } |
|
| 627 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 628 | // and we have a copy of the original data |
||
| 629 | $originalData = $this->originalDocumentData[$oid]; |
||
| 630 | 295 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 631 | 295 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 632 | 295 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 633 | 2 | } else { |
|
| 634 | $changeSet = []; |
||
| 635 | 295 | } |
|
| 636 | |||
| 637 | $gridFSMetadataProperty = null; |
||
| 638 | 295 | ||
| 639 | if ($class->isFile) { |
||
| 640 | 295 | try { |
|
| 641 | $gridFSMetadata = $class->getFieldMappingByDbFieldName('metadata'); |
||
| 642 | 4 | $gridFSMetadataProperty = $gridFSMetadata['fieldName']; |
|
| 643 | 3 | } catch (MappingException $e) { |
|
| 644 | 1 | } |
|
| 645 | } |
||
| 646 | |||
| 647 | foreach ($actualData as $propName => $actualValue) { |
||
| 648 | 295 | // skip not saved fields |
|
| 649 | if ((isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) || |
||
| 650 | 295 | ($class->isFile && $propName !== $gridFSMetadataProperty)) { |
|
| 651 | 295 | continue; |
|
| 652 | 4 | } |
|
| 653 | |||
| 654 | $orgValue = $originalData[$propName] ?? null; |
||
| 655 | 294 | ||
| 656 | // skip if value has not changed |
||
| 657 | if ($orgValue === $actualValue) { |
||
| 658 | 294 | if (! $actualValue instanceof PersistentCollectionInterface) { |
|
| 659 | 292 | continue; |
|
| 660 | 292 | } |
|
| 661 | |||
| 662 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
||
| 663 | 206 | // consider dirty collections as changed as well |
|
| 664 | continue; |
||
| 665 | 182 | } |
|
| 666 | } |
||
| 667 | |||
| 668 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 669 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
||
| 670 | 254 | if ($orgValue !== null) { |
|
| 671 | 14 | $this->scheduleOrphanRemoval($orgValue); |
|
| 672 | 8 | } |
|
| 673 | $changeSet[$propName] = [$orgValue, $actualValue]; |
||
| 674 | 14 | continue; |
|
| 675 | 14 | } |
|
| 676 | |||
| 677 | // if owning side of reference-one relationship |
||
| 678 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
||
| 679 | 247 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 680 | 13 | $this->scheduleOrphanRemoval($orgValue); |
|
| 681 | 1 | } |
|
| 682 | |||
| 683 | $changeSet[$propName] = [$orgValue, $actualValue]; |
||
| 684 | 13 | continue; |
|
| 685 | 13 | } |
|
| 686 | |||
| 687 | if ($isChangeTrackingNotify) { |
||
| 688 | 240 | continue; |
|
| 689 | 3 | } |
|
| 690 | |||
| 691 | // ignore inverse side of reference relationship |
||
| 692 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
||
| 693 | 238 | continue; |
|
| 694 | 6 | } |
|
| 695 | |||
| 696 | // Persistent collection was exchanged with the "originally" |
||
| 697 | // created one. This can only mean it was cloned and replaced |
||
| 698 | // on another document. |
||
| 699 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
||
| 700 | 236 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 701 | 6 | } |
|
| 702 | |||
| 703 | // if embed-many or reference-many relationship |
||
| 704 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
||
| 705 | 236 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 706 | 125 | /* If original collection was exchanged with a non-empty value |
|
| 707 | * and $set will be issued, there is no need to $unset it first |
||
| 708 | */ |
||
| 709 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
||
| 710 | 125 | continue; |
|
| 711 | 31 | } |
|
| 712 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
||
| 713 | 104 | $this->scheduleCollectionDeletion($orgValue); |
|
| 714 | 18 | } |
|
| 715 | continue; |
||
| 716 | 104 | } |
|
| 717 | |||
| 718 | // skip equivalent date values |
||
| 719 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
||
| 720 | 148 | /** @var DateType $dateType */ |
|
| 721 | $dateType = Type::getType('date'); |
||
| 722 | 37 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 723 | 37 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 724 | 37 | ||
| 725 | $orgTimestamp = $dbOrgValue instanceof UTCDateTime ? $dbOrgValue->toDateTime()->getTimestamp() : null; |
||
| 726 | 37 | $actualTimestamp = $dbActualValue instanceof UTCDateTime ? $dbActualValue->toDateTime()->getTimestamp() : null; |
|
| 727 | 37 | ||
| 728 | if ($orgTimestamp === $actualTimestamp) { |
||
| 729 | 37 | continue; |
|
| 730 | 30 | } |
|
| 731 | } |
||
| 732 | |||
| 733 | // regular field |
||
| 734 | $changeSet[$propName] = [$orgValue, $actualValue]; |
||
| 735 | 131 | } |
|
| 736 | if ($changeSet) { |
||
| 737 | 295 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 738 | 243 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 739 | 19 | : $changeSet; |
|
| 740 | 240 | ||
| 741 | $this->originalDocumentData[$oid] = $actualData; |
||
| 742 | 243 | $this->scheduleForUpdate($document); |
|
| 743 | 243 | } |
|
| 744 | } |
||
| 745 | |||
| 746 | // Look for changes in associations of the document |
||
| 747 | $associationMappings = array_filter( |
||
| 748 | 612 | $class->associationMappings, |
|
| 749 | 612 | static function ($assoc) { |
|
| 750 | return empty($assoc['notSaved']); |
||
| 751 | 471 | } |
|
| 752 | 612 | ); |
|
| 753 | |||
| 754 | foreach ($associationMappings as $mapping) { |
||
| 755 | 612 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 756 | 471 | ||
| 757 | if ($value === null) { |
||
| 758 | 471 | continue; |
|
| 759 | 323 | } |
|
| 760 | |||
| 761 | $this->computeAssociationChanges($document, $mapping, $value); |
||
| 762 | 452 | ||
| 763 | if (isset($mapping['reference'])) { |
||
| 764 | 451 | continue; |
|
| 765 | 337 | } |
|
| 766 | |||
| 767 | $values = $mapping['type'] === ClassMetadata::ONE ? [$value] : $value->unwrap(); |
||
| 768 | 354 | ||
| 769 | foreach ($values as $obj) { |
||
| 770 | 354 | $oid2 = spl_object_hash($obj); |
|
| 771 | 188 | ||
| 772 | if (isset($this->documentChangeSets[$oid2])) { |
||
| 773 | 188 | if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) { |
|
| 774 | 186 | // instance of $value is the same as it was previously otherwise there would be |
|
| 775 | // change set already in place |
||
| 776 | $this->documentChangeSets[$oid][$mapping['fieldName']] = [$value, $value]; |
||
| 777 | 42 | } |
|
| 778 | |||
| 779 | if (! $isNewDocument) { |
||
| 780 | 186 | $this->scheduleForUpdate($document); |
|
| 781 | 87 | } |
|
| 782 | |||
| 783 | break; |
||
| 784 | 186 | } |
|
| 785 | } |
||
| 786 | } |
||
| 787 | } |
||
| 788 | 611 | ||
| 789 | /** |
||
| 790 | * Computes all the changes that have been done to documents and collections |
||
| 791 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 792 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 793 | */ |
||
| 794 | public function computeChangeSets() : void |
||
| 795 | 616 | { |
|
| 796 | $this->computeScheduleInsertsChangeSets(); |
||
| 797 | 616 | $this->computeScheduleUpsertsChangeSets(); |
|
| 798 | 615 | ||
| 799 | // Compute changes for other MANAGED documents. Change tracking policies take effect here. |
||
| 800 | foreach ($this->identityMap as $className => $documents) { |
||
| 801 | 615 | $class = $this->dm->getClassMetadata($className); |
|
| 802 | 615 | if ($class->isEmbeddedDocument) { |
|
| 803 | 615 | /* we do not want to compute changes to embedded documents up front |
|
| 804 | * in case embedded document was replaced and its changeset |
||
| 805 | * would corrupt data. Embedded documents' change set will |
||
| 806 | * be calculated by reachability from owning document. |
||
| 807 | */ |
||
| 808 | continue; |
||
| 809 | 178 | } |
|
| 810 | |||
| 811 | // If change tracking is explicit or happens through notification, then only compute |
||
| 812 | // changes on document of that type that are explicitly marked for synchronization. |
||
| 813 | switch (true) { |
||
| 814 | case $class->isChangeTrackingDeferredImplicit(): |
||
| 815 | 615 | $documentsToProcess = $documents; |
|
| 816 | 614 | break; |
|
| 817 | 614 | ||
| 818 | case isset($this->scheduledForSynchronization[$className]): |
||
| 819 | 4 | $documentsToProcess = $this->scheduledForSynchronization[$className]; |
|
| 820 | 3 | break; |
|
| 821 | 3 | ||
| 822 | default: |
||
| 823 | $documentsToProcess = []; |
||
| 824 | 4 | } |
|
| 825 | |||
| 826 | foreach ($documentsToProcess as $document) { |
||
| 827 | 615 | // Ignore uninitialized proxy objects |
|
| 828 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
||
| 829 | 610 | continue; |
|
| 830 | 9 | } |
|
| 831 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 832 | $oid = spl_object_hash($document); |
||
| 833 | 610 | if (isset($this->documentInsertions[$oid]) |
|
| 834 | 610 | || isset($this->documentUpserts[$oid]) |
|
| 835 | 338 | || isset($this->documentDeletions[$oid]) |
|
| 836 | 292 | || ! isset($this->documentStates[$oid]) |
|
| 837 | 610 | ) { |
|
| 838 | continue; |
||
| 839 | 608 | } |
|
| 840 | |||
| 841 | $this->computeChangeSet($class, $document); |
||
| 842 | 292 | } |
|
| 843 | } |
||
| 844 | } |
||
| 845 | 615 | ||
| 846 | /** |
||
| 847 | * Computes the changes of an association. |
||
| 848 | * |
||
| 849 | * @param mixed $value The value of the association. |
||
| 850 | * |
||
| 851 | * @throws InvalidArgumentException |
||
| 852 | */ |
||
| 853 | private function computeAssociationChanges(object $parentDocument, array $assoc, $value) : void |
||
| 854 | 452 | { |
|
| 855 | $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]); |
||
| 856 | 452 | $class = $this->dm->getClassMetadata(get_class($parentDocument)); |
|
| 857 | 452 | $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); |
|
| 858 | 452 | ||
| 859 | if ($value instanceof GhostObjectInterface && ! $value->isProxyInitialized()) { |
||
| 860 | 452 | return; |
|
| 861 | 7 | } |
|
| 862 | |||
| 863 | if ($value instanceof PersistentCollectionInterface && $value->isDirty() && $value->getOwner() !== null && ($assoc['isOwningSide'] || isset($assoc['embedded']))) { |
||
| 864 | 451 | if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) { |
|
| 865 | 258 | $this->scheduleCollectionUpdate($value); |
|
| 866 | 254 | } |
|
| 867 | |||
| 868 | $topmostOwner = $this->getOwningDocument($value->getOwner()); |
||
| 869 | 258 | $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value; |
|
| 870 | 258 | if (! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) { |
|
| 871 | 258 | $value->initialize(); |
|
| 872 | 151 | foreach ($value->getDeletedDocuments() as $orphan) { |
|
| 873 | 151 | $this->scheduleOrphanRemoval($orphan); |
|
| 874 | 25 | } |
|
| 875 | } |
||
| 876 | } |
||
| 877 | |||
| 878 | // Look through the documents, and in any of their associations, |
||
| 879 | // for transient (new) documents, recursively. ("Persistence by reachability") |
||
| 880 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 881 | $unwrappedValue = $assoc['type'] === ClassMetadata::ONE ? [$value] : $value->unwrap(); |
||
| 882 | 451 | ||
| 883 | $count = 0; |
||
| 884 | 451 | foreach ($unwrappedValue as $key => $entry) { |
|
| 885 | 451 | if (! is_object($entry)) { |
|
| 886 | 367 | throw new InvalidArgumentException( |
|
| 887 | 1 | sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name']) |
|
| 888 | 1 | ); |
|
| 889 | } |
||
| 890 | |||
| 891 | $targetClass = $this->dm->getClassMetadata(get_class($entry)); |
||
| 892 | 366 | ||
| 893 | $state = $this->getDocumentState($entry, self::STATE_NEW); |
||
| 894 | 366 | ||
| 895 | // Handle "set" strategy for multi-level hierarchy |
||
| 896 | $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key; |
||
| 897 | 366 | $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name']; |
|
| 898 | 366 | ||
| 899 | $count++; |
||
| 900 | 366 | ||
| 901 | switch ($state) { |
||
| 902 | case self::STATE_NEW: |
||
| 903 | 366 | if (! $assoc['isCascadePersist']) { |
|
| 904 | 70 | throw new InvalidArgumentException('A new document was found through a relationship that was not' |
|
| 905 | . ' configured to cascade persist operations: ' . $this->objToStr($entry) . '.' |
||
| 906 | . ' Explicitly persist the new document or configure cascading persist operations' |
||
| 907 | . ' on the relationship.'); |
||
| 908 | } |
||
| 909 | |||
| 910 | $this->persistNew($targetClass, $entry); |
||
| 911 | 70 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 912 | 70 | $this->computeChangeSet($targetClass, $entry); |
|
| 913 | 70 | break; |
|
| 914 | 70 | ||
| 915 | case self::STATE_MANAGED: |
||
| 916 | 362 | if ($targetClass->isEmbeddedDocument) { |
|
| 917 | 362 | [, $knownParent ] = $this->getParentAssociation($entry); |
|
| 918 | 179 | if ($knownParent && $knownParent !== $parentDocument) { |
|
| 919 | 179 | $entry = clone $entry; |
|
| 920 | 6 | if ($assoc['type'] === ClassMetadata::ONE) { |
|
| 921 | 6 | $class->setFieldValue($parentDocument, $assoc['fieldName'], $entry); |
|
| 922 | 3 | $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['fieldName'], $entry); |
|
| 923 | 3 | $poid = spl_object_hash($parentDocument); |
|
| 924 | 3 | if (isset($this->documentChangeSets[$poid][$assoc['fieldName']])) { |
|
| 925 | 3 | $this->documentChangeSets[$poid][$assoc['fieldName']][1] = $entry; |
|
| 926 | 3 | } |
|
| 927 | } else { |
||
| 928 | // must use unwrapped value to not trigger orphan removal |
||
| 929 | $unwrappedValue[$key] = $entry; |
||
| 930 | 4 | } |
|
| 931 | $this->persistNew($targetClass, $entry); |
||
| 932 | 6 | } |
|
| 933 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
||
| 934 | 179 | $this->computeChangeSet($targetClass, $entry); |
|
| 935 | 179 | } |
|
| 936 | break; |
||
| 937 | 362 | ||
| 938 | case self::STATE_REMOVED: |
||
| 939 | 1 | // Consume the $value as array (it's either an array or an ArrayAccess) |
|
| 940 | // and remove the element from Collection. |
||
| 941 | if ($assoc['type'] === ClassMetadata::MANY) { |
||
| 942 | 1 | unset($value[$key]); |
|
| 943 | } |
||
| 944 | break; |
||
| 945 | 1 | ||
| 946 | case self::STATE_DETACHED: |
||
| 947 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 948 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 949 | throw new InvalidArgumentException('A detached document was found through a ' |
||
| 950 | . 'relationship during cascading a persist operation.'); |
||
| 951 | |||
| 952 | default: |
||
| 953 | // MANAGED associated documents are already taken into account |
||
| 954 | // during changeset calculation anyway, since they are in the identity map. |
||
| 955 | } |
||
| 956 | } |
||
| 957 | } |
||
| 958 | 450 | ||
| 959 | /** |
||
| 960 | * INTERNAL: |
||
| 961 | * Computes the changeset of an individual document, independently of the |
||
| 962 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 963 | * |
||
| 964 | * The passed document must be a managed document. If the document already has a change set |
||
| 965 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 966 | * whereby changes detected in this method prevail. |
||
| 967 | * |
||
| 968 | * @throws InvalidArgumentException If the passed document is not MANAGED. |
||
| 969 | * |
||
| 970 | * @ignore |
||
| 971 | */ |
||
| 972 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, object $document) : void |
||
| 973 | 19 | { |
|
| 974 | // Ignore uninitialized proxy objects |
||
| 975 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
||
| 976 | 19 | return; |
|
| 977 | 1 | } |
|
| 978 | |||
| 979 | $oid = spl_object_hash($document); |
||
| 980 | 18 | ||
| 981 | if (! isset($this->documentStates[$oid]) || $this->documentStates[$oid] !== self::STATE_MANAGED) { |
||
| 982 | 18 | throw new InvalidArgumentException('Document must be managed.'); |
|
| 983 | } |
||
| 984 | |||
| 985 | if (! $class->isInheritanceTypeNone()) { |
||
| 986 | 18 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 987 | 2 | } |
|
| 988 | |||
| 989 | $this->computeOrRecomputeChangeSet($class, $document, true); |
||
| 990 | 18 | } |
|
| 991 | 18 | ||
| 992 | /** |
||
| 993 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 994 | */ |
||
| 995 | private function persistNew(ClassMetadata $class, object $document) : void |
||
| 996 | 641 | { |
|
| 997 | $this->lifecycleEventManager->prePersist($class, $document); |
||
| 998 | 641 | $oid = spl_object_hash($document); |
|
| 999 | 641 | $upsert = false; |
|
| 1000 | 641 | if ($class->identifier) { |
|
| 1001 | 641 | $idValue = $class->getIdentifierValue($document); |
|
| 1002 | 641 | $upsert = ! $class->isEmbeddedDocument && $idValue !== null; |
|
| 1003 | 641 | ||
| 1004 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
||
| 1005 | 641 | throw new InvalidArgumentException(sprintf( |
|
| 1006 | 3 | '%s uses NONE identifier generation strategy but no identifier was provided when persisting.', |
|
| 1007 | 3 | get_class($document) |
|
| 1008 | 3 | )); |
|
| 1009 | } |
||
| 1010 | |||
| 1011 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && ! preg_match('#^[0-9a-f]{24}$#', (string) $idValue)) { |
||
| 1012 | 640 | throw new InvalidArgumentException(sprintf( |
|
| 1013 | 1 | '%s uses AUTO identifier generation strategy but provided identifier is not a valid ObjectId.', |
|
| 1014 | 1 | get_class($document) |
|
| 1015 | 1 | )); |
|
| 1016 | } |
||
| 1017 | |||
| 1018 | if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null && $class->idGenerator !== null) { |
||
| 1019 | 639 | $idValue = $class->idGenerator->generate($this->dm, $document); |
|
| 1020 | 560 | $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue)); |
|
| 1021 | 560 | $class->setIdentifierValue($document, $idValue); |
|
| 1022 | 560 | } |
|
| 1023 | |||
| 1024 | $this->documentIdentifiers[$oid] = $idValue; |
||
| 1025 | 639 | } else { |
|
| 1026 | // this is for embedded documents without identifiers |
||
| 1027 | $this->documentIdentifiers[$oid] = $oid; |
||
| 1028 | 152 | } |
|
| 1029 | |||
| 1030 | $this->documentStates[$oid] = self::STATE_MANAGED; |
||
| 1031 | 639 | ||
| 1032 | if ($upsert) { |
||
| 1033 | 639 | $this->scheduleForUpsert($class, $document); |
|
| 1034 | 89 | } else { |
|
| 1035 | $this->scheduleForInsert($class, $document); |
||
| 1036 | 569 | } |
|
| 1037 | } |
||
| 1038 | 639 | ||
| 1039 | /** |
||
| 1040 | * Executes all document insertions for documents of the specified type. |
||
| 1041 | */ |
||
| 1042 | private function executeInserts(ClassMetadata $class, array $documents, array $options = []) : void |
||
| 1043 | 530 | { |
|
| 1044 | $persister = $this->getDocumentPersister($class->name); |
||
| 1045 | 530 | ||
| 1046 | foreach ($documents as $oid => $document) { |
||
| 1047 | 530 | $persister->addInsert($document); |
|
| 1048 | 530 | unset($this->documentInsertions[$oid]); |
|
| 1049 | 530 | } |
|
| 1050 | |||
| 1051 | $persister->executeInserts($options); |
||
| 1052 | 530 | ||
| 1053 | foreach ($documents as $document) { |
||
| 1054 | 519 | $this->lifecycleEventManager->postPersist($class, $document); |
|
| 1055 | 519 | } |
|
| 1056 | } |
||
| 1057 | 519 | ||
| 1058 | /** |
||
| 1059 | * Executes all document upserts for documents of the specified type. |
||
| 1060 | */ |
||
| 1061 | private function executeUpserts(ClassMetadata $class, array $documents, array $options = []) : void |
||
| 1062 | 86 | { |
|
| 1063 | $persister = $this->getDocumentPersister($class->name); |
||
| 1064 | 86 | ||
| 1065 | foreach ($documents as $oid => $document) { |
||
| 1066 | 86 | $persister->addUpsert($document); |
|
| 1067 | 86 | unset($this->documentUpserts[$oid]); |
|
| 1068 | 86 | } |
|
| 1069 | |||
| 1070 | $persister->executeUpserts($options); |
||
| 1071 | 86 | ||
| 1072 | foreach ($documents as $document) { |
||
| 1073 | 86 | $this->lifecycleEventManager->postPersist($class, $document); |
|
| 1074 | 86 | } |
|
| 1075 | } |
||
| 1076 | 86 | ||
| 1077 | /** |
||
| 1078 | * Executes all document updates for documents of the specified type. |
||
| 1079 | */ |
||
| 1080 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = []) : void |
||
| 1081 | 236 | { |
|
| 1082 | if ($class->isReadOnly) { |
||
| 1083 | 236 | return; |
|
| 1084 | } |
||
| 1085 | |||
| 1086 | $className = $class->name; |
||
| 1087 | 236 | $persister = $this->getDocumentPersister($className); |
|
| 1088 | 236 | ||
| 1089 | foreach ($documents as $oid => $document) { |
||
| 1090 | 236 | $this->lifecycleEventManager->preUpdate($class, $document); |
|
| 1091 | 236 | ||
| 1092 | if (! empty($this->documentChangeSets[$oid]) || $this->hasScheduledCollections($document)) { |
||
| 1093 | 236 | $persister->update($document, $options); |
|
| 1094 | 235 | } |
|
| 1095 | |||
| 1096 | unset($this->documentUpdates[$oid]); |
||
| 1097 | 229 | ||
| 1098 | $this->lifecycleEventManager->postUpdate($class, $document); |
||
| 1099 | 229 | } |
|
| 1100 | } |
||
| 1101 | 228 | ||
| 1102 | /** |
||
| 1103 | * Executes all document deletions for documents of the specified type. |
||
| 1104 | */ |
||
| 1105 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = []) : void |
||
| 1106 | 79 | { |
|
| 1107 | $persister = $this->getDocumentPersister($class->name); |
||
| 1108 | 79 | ||
| 1109 | foreach ($documents as $oid => $document) { |
||
| 1110 | 79 | if (! $class->isEmbeddedDocument) { |
|
| 1111 | 79 | $persister->delete($document, $options); |
|
| 1112 | 36 | } |
|
| 1113 | unset( |
||
| 1114 | $this->documentDeletions[$oid], |
||
| 1115 | 77 | $this->documentIdentifiers[$oid], |
|
| 1116 | 77 | $this->originalDocumentData[$oid] |
|
| 1117 | 77 | ); |
|
| 1118 | |||
| 1119 | // Clear snapshot information for any referenced PersistentCollection |
||
| 1120 | // http://www.doctrine-project.org/jira/browse/MODM-95 |
||
| 1121 | foreach ($class->associationMappings as $fieldMapping) { |
||
| 1122 | 77 | if (! isset($fieldMapping['type']) || $fieldMapping['type'] !== ClassMetadata::MANY) { |
|
| 1123 | 53 | continue; |
|
| 1124 | 38 | } |
|
| 1125 | |||
| 1126 | $value = $class->reflFields[$fieldMapping['fieldName']]->getValue($document); |
||
| 1127 | 33 | if (! ($value instanceof PersistentCollectionInterface)) { |
|
| 1128 | 33 | continue; |
|
| 1129 | 7 | } |
|
| 1130 | |||
| 1131 | $value->clearSnapshot(); |
||
| 1132 | 29 | } |
|
| 1133 | |||
| 1134 | // Document with this $oid after deletion treated as NEW, even if the $oid |
||
| 1135 | // is obtained by a new document because the old one went out of scope. |
||
| 1136 | $this->documentStates[$oid] = self::STATE_NEW; |
||
| 1137 | 77 | ||
| 1138 | $this->lifecycleEventManager->postRemove($class, $document); |
||
| 1139 | 77 | } |
|
| 1140 | } |
||
| 1141 | 77 | ||
| 1142 | /** |
||
| 1143 | * Schedules a document for insertion into the database. |
||
| 1144 | * If the document already has an identifier, it will be added to the |
||
| 1145 | * identity map. |
||
| 1146 | * |
||
| 1147 | * @throws InvalidArgumentException |
||
| 1148 | */ |
||
| 1149 | public function scheduleForInsert(ClassMetadata $class, object $document) : void |
||
| 1150 | 572 | { |
|
| 1151 | $oid = spl_object_hash($document); |
||
| 1152 | 572 | ||
| 1153 | if (isset($this->documentUpdates[$oid])) { |
||
| 1154 | 572 | throw new InvalidArgumentException('Dirty document can not be scheduled for insertion.'); |
|
| 1155 | } |
||
| 1156 | if (isset($this->documentDeletions[$oid])) { |
||
| 1157 | 572 | throw new InvalidArgumentException('Removed document can not be scheduled for insertion.'); |
|
| 1158 | } |
||
| 1159 | if (isset($this->documentInsertions[$oid])) { |
||
| 1160 | 572 | throw new InvalidArgumentException('Document can not be scheduled for insertion twice.'); |
|
| 1161 | } |
||
| 1162 | |||
| 1163 | $this->documentInsertions[$oid] = $document; |
||
| 1164 | 572 | ||
| 1165 | if (! isset($this->documentIdentifiers[$oid])) { |
||
| 1166 | 572 | return; |
|
| 1167 | 3 | } |
|
| 1168 | |||
| 1169 | $this->addToIdentityMap($document); |
||
| 1170 | 569 | } |
|
| 1171 | 569 | ||
| 1172 | /** |
||
| 1173 | * Schedules a document for upsert into the database and adds it to the |
||
| 1174 | * identity map |
||
| 1175 | * |
||
| 1176 | * @throws InvalidArgumentException |
||
| 1177 | */ |
||
| 1178 | public function scheduleForUpsert(ClassMetadata $class, object $document) : void |
||
| 1179 | 92 | { |
|
| 1180 | $oid = spl_object_hash($document); |
||
| 1181 | 92 | ||
| 1182 | if ($class->isEmbeddedDocument) { |
||
| 1183 | 92 | throw new InvalidArgumentException('Embedded document can not be scheduled for upsert.'); |
|
| 1184 | } |
||
| 1185 | if (isset($this->documentUpdates[$oid])) { |
||
| 1186 | 92 | throw new InvalidArgumentException('Dirty document can not be scheduled for upsert.'); |
|
| 1187 | } |
||
| 1188 | if (isset($this->documentDeletions[$oid])) { |
||
| 1189 | 92 | throw new InvalidArgumentException('Removed document can not be scheduled for upsert.'); |
|
| 1190 | } |
||
| 1191 | if (isset($this->documentUpserts[$oid])) { |
||
| 1192 | 92 | throw new InvalidArgumentException('Document can not be scheduled for upsert twice.'); |
|
| 1193 | } |
||
| 1194 | |||
| 1195 | $this->documentUpserts[$oid] = $document; |
||
| 1196 | 92 | $this->documentIdentifiers[$oid] = $class->getIdentifierValue($document); |
|
| 1197 | 92 | $this->addToIdentityMap($document); |
|
| 1198 | 92 | } |
|
| 1199 | 92 | ||
| 1200 | /** |
||
| 1201 | * Checks whether a document is scheduled for insertion. |
||
| 1202 | */ |
||
| 1203 | public function isScheduledForInsert(object $document) : bool |
||
| 1204 | 110 | { |
|
| 1205 | return isset($this->documentInsertions[spl_object_hash($document)]); |
||
| 1206 | 110 | } |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Checks whether a document is scheduled for upsert. |
||
| 1210 | */ |
||
| 1211 | public function isScheduledForUpsert(object $document) : bool |
||
| 1212 | 5 | { |
|
| 1213 | return isset($this->documentUpserts[spl_object_hash($document)]); |
||
| 1214 | 5 | } |
|
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Schedules a document for being updated. |
||
| 1218 | * |
||
| 1219 | * @throws InvalidArgumentException |
||
| 1220 | */ |
||
| 1221 | public function scheduleForUpdate(object $document) : void |
||
| 1222 | 245 | { |
|
| 1223 | $oid = spl_object_hash($document); |
||
| 1224 | 245 | if (! isset($this->documentIdentifiers[$oid])) { |
|
| 1225 | 245 | throw new InvalidArgumentException('Document has no identity.'); |
|
| 1226 | } |
||
| 1227 | |||
| 1228 | if (isset($this->documentDeletions[$oid])) { |
||
| 1229 | 245 | throw new InvalidArgumentException('Document is removed.'); |
|
| 1230 | } |
||
| 1231 | |||
| 1232 | if (isset($this->documentUpdates[$oid]) |
||
| 1233 | 245 | || isset($this->documentInsertions[$oid]) |
|
| 1234 | 245 | || isset($this->documentUpserts[$oid])) { |
|
| 1235 | 245 | return; |
|
| 1236 | 104 | } |
|
| 1237 | |||
| 1238 | $this->documentUpdates[$oid] = $document; |
||
| 1239 | 243 | } |
|
| 1240 | 243 | ||
| 1241 | /** |
||
| 1242 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1243 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1244 | * at commit time. |
||
| 1245 | */ |
||
| 1246 | public function isScheduledForUpdate(object $document) : bool |
||
| 1247 | 21 | { |
|
| 1248 | return isset($this->documentUpdates[spl_object_hash($document)]); |
||
| 1249 | 21 | } |
|
| 1250 | |||
| 1251 | /** |
||
| 1252 | 1 | * Checks whether a document is registered to be checked in the unit of work. |
|
| 1253 | */ |
||
| 1254 | 1 | public function isScheduledForSynchronization(object $document) : bool |
|
| 1255 | 1 | { |
|
| 1256 | $class = $this->dm->getClassMetadata(get_class($document)); |
||
| 1257 | return isset($this->scheduledForSynchronization[$class->name][spl_object_hash($document)]); |
||
| 1258 | } |
||
| 1259 | |||
| 1260 | /** |
||
| 1261 | * INTERNAL: |
||
| 1262 | 84 | * Schedules a document for deletion. |
|
| 1263 | */ |
||
| 1264 | 84 | public function scheduleForDelete(object $document) : void |
|
| 1265 | { |
||
| 1266 | 84 | $oid = spl_object_hash($document); |
|
| 1267 | 2 | ||
| 1268 | 2 | if (isset($this->documentInsertions[$oid])) { |
|
| 1269 | if ($this->isInIdentityMap($document)) { |
||
| 1270 | 2 | $this->removeFromIdentityMap($document); |
|
| 1271 | 2 | } |
|
| 1272 | unset($this->documentInsertions[$oid]); |
||
| 1273 | return; // document has not been persisted yet, so nothing more to do. |
||
| 1274 | 83 | } |
|
| 1275 | 2 | ||
| 1276 | if (! $this->isInIdentityMap($document)) { |
||
| 1277 | return; // ignore |
||
| 1278 | 82 | } |
|
| 1279 | 82 | ||
| 1280 | $this->removeFromIdentityMap($document); |
||
| 1281 | 82 | $this->documentStates[$oid] = self::STATE_REMOVED; |
|
| 1282 | |||
| 1283 | if (isset($this->documentUpdates[$oid])) { |
||
| 1284 | 82 | unset($this->documentUpdates[$oid]); |
|
| 1285 | } |
||
| 1286 | if (isset($this->documentDeletions[$oid])) { |
||
| 1287 | return; |
||
| 1288 | 82 | } |
|
| 1289 | 82 | ||
| 1290 | $this->documentDeletions[$oid] = $document; |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1295 | 5 | * of work. |
|
| 1296 | */ |
||
| 1297 | 5 | public function isScheduledForDelete(object $document) : bool |
|
| 1298 | { |
||
| 1299 | return isset($this->documentDeletions[spl_object_hash($document)]); |
||
| 1300 | } |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | 257 | * Checks whether a document is scheduled for insertion, update or deletion. |
|
| 1304 | */ |
||
| 1305 | 257 | public function isDocumentScheduled(object $document) : bool |
|
| 1306 | 257 | { |
|
| 1307 | 139 | $oid = spl_object_hash($document); |
|
| 1308 | 129 | return isset($this->documentInsertions[$oid]) || |
|
| 1309 | 257 | isset($this->documentUpserts[$oid]) || |
|
| 1310 | isset($this->documentUpdates[$oid]) || |
||
| 1311 | isset($this->documentDeletions[$oid]); |
||
| 1312 | } |
||
| 1313 | |||
| 1314 | /** |
||
| 1315 | * INTERNAL: |
||
| 1316 | * Registers a document in the identity map. |
||
| 1317 | * |
||
| 1318 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1319 | * the root document. Identifiers are serialized before being used as array |
||
| 1320 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1321 | * |
||
| 1322 | 680 | * @ignore |
|
| 1323 | */ |
||
| 1324 | 680 | public function addToIdentityMap(object $document) : bool |
|
| 1325 | 680 | { |
|
| 1326 | $class = $this->dm->getClassMetadata(get_class($document)); |
||
| 1327 | 680 | $id = $this->getIdForIdentityMap($document); |
|
| 1328 | 44 | ||
| 1329 | if (isset($this->identityMap[$class->name][$id])) { |
||
| 1330 | return false; |
||
| 1331 | 680 | } |
|
| 1332 | |||
| 1333 | 680 | $this->identityMap[$class->name][$id] = $document; |
|
| 1334 | 680 | ||
| 1335 | 3 | if ($document instanceof NotifyPropertyChanged && |
|
| 1336 | ( ! $document instanceof GhostObjectInterface || $document->isProxyInitialized())) { |
||
| 1337 | $document->addPropertyChangedListener($this); |
||
| 1338 | 680 | } |
|
| 1339 | |||
| 1340 | return true; |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Gets the state of a document with regard to the current unit of work. |
||
| 1345 | * |
||
| 1346 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1347 | * This parameter can be set to improve performance of document state detection |
||
| 1348 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1349 | 645 | * is either known or does not matter for the caller of the method. |
|
| 1350 | */ |
||
| 1351 | 645 | public function getDocumentState(object $document, ?int $assume = null) : int |
|
| 1352 | { |
||
| 1353 | 645 | $oid = spl_object_hash($document); |
|
| 1354 | 406 | ||
| 1355 | if (isset($this->documentStates[$oid])) { |
||
| 1356 | return $this->documentStates[$oid]; |
||
| 1357 | 644 | } |
|
| 1358 | |||
| 1359 | 644 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1360 | 192 | ||
| 1361 | if ($class->isEmbeddedDocument) { |
||
| 1362 | return self::STATE_NEW; |
||
| 1363 | 641 | } |
|
| 1364 | 639 | ||
| 1365 | if ($assume !== null) { |
||
| 1366 | return $assume; |
||
| 1367 | } |
||
| 1368 | |||
| 1369 | /* State can only be NEW or DETACHED, because MANAGED/REMOVED states are |
||
| 1370 | * known. Note that you cannot remember the NEW or DETACHED state in |
||
| 1371 | * _documentStates since the UoW does not hold references to such |
||
| 1372 | * objects and the object hash can be reused. More generally, because |
||
| 1373 | * the state may "change" between NEW/DETACHED without the UoW being |
||
| 1374 | 3 | * aware of it. |
|
| 1375 | */ |
||
| 1376 | 3 | $id = $class->getIdentifierObject($document); |
|
| 1377 | 2 | ||
| 1378 | if ($id === null) { |
||
| 1379 | return self::STATE_NEW; |
||
| 1380 | } |
||
| 1381 | 2 | ||
| 1382 | // Check for a version field, if available, to avoid a DB lookup. |
||
| 1383 | if ($class->isVersioned && $class->versionField !== null) { |
||
| 1384 | return $class->getFieldValue($document, $class->versionField) |
||
| 1385 | ? self::STATE_DETACHED |
||
| 1386 | : self::STATE_NEW; |
||
| 1387 | } |
||
| 1388 | 2 | ||
| 1389 | 1 | // Last try before DB lookup: check the identity map. |
|
| 1390 | if ($this->tryGetById($id, $class)) { |
||
| 1391 | return self::STATE_DETACHED; |
||
| 1392 | } |
||
| 1393 | 2 | ||
| 1394 | 1 | // DB lookup |
|
| 1395 | if ($this->getDocumentPersister($class->name)->exists($document)) { |
||
| 1396 | return self::STATE_DETACHED; |
||
| 1397 | 1 | } |
|
| 1398 | |||
| 1399 | return self::STATE_NEW; |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | /** |
||
| 1403 | * INTERNAL: |
||
| 1404 | * Removes a document from the identity map. This effectively detaches the |
||
| 1405 | * document from the persistence management of Doctrine. |
||
| 1406 | * |
||
| 1407 | * @throws InvalidArgumentException |
||
| 1408 | * |
||
| 1409 | 96 | * @ignore |
|
| 1410 | */ |
||
| 1411 | 96 | public function removeFromIdentityMap(object $document) : bool |
|
| 1412 | { |
||
| 1413 | $oid = spl_object_hash($document); |
||
| 1414 | 96 | ||
| 1415 | // Check if id is registered first |
||
| 1416 | if (! isset($this->documentIdentifiers[$oid])) { |
||
| 1417 | return false; |
||
| 1418 | 96 | } |
|
| 1419 | 96 | ||
| 1420 | $class = $this->dm->getClassMetadata(get_class($document)); |
||
| 1421 | 96 | $id = $this->getIdForIdentityMap($document); |
|
| 1422 | 96 | ||
| 1423 | 96 | if (isset($this->identityMap[$class->name][$id])) { |
|
| 1424 | 96 | unset($this->identityMap[$class->name][$id]); |
|
| 1425 | $this->documentStates[$oid] = self::STATE_DETACHED; |
||
| 1426 | return true; |
||
| 1427 | } |
||
| 1428 | |||
| 1429 | return false; |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * INTERNAL: |
||
| 1434 | * Gets a document in the identity map by its identifier hash. |
||
| 1435 | * |
||
| 1436 | * @param mixed $id Document identifier |
||
| 1437 | * |
||
| 1438 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1439 | * |
||
| 1440 | 37 | * @ignore |
|
| 1441 | */ |
||
| 1442 | 37 | public function getById($id, ClassMetadata $class) : object |
|
| 1443 | { |
||
| 1444 | if (! $class->identifier) { |
||
| 1445 | throw new InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); |
||
| 1446 | 37 | } |
|
| 1447 | |||
| 1448 | 37 | $serializedId = serialize($class->getDatabaseIdentifierValue($id)); |
|
| 1449 | |||
| 1450 | return $this->identityMap[$class->name][$serializedId]; |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * INTERNAL: |
||
| 1455 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1456 | * for the given hash, FALSE is returned. |
||
| 1457 | * |
||
| 1458 | * @param mixed $id Document identifier |
||
| 1459 | * |
||
| 1460 | * @return mixed The found document or FALSE. |
||
| 1461 | * |
||
| 1462 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1463 | * |
||
| 1464 | 306 | * @ignore |
|
| 1465 | */ |
||
| 1466 | 306 | public function tryGetById($id, ClassMetadata $class) |
|
| 1467 | { |
||
| 1468 | if (! $class->identifier) { |
||
| 1469 | throw new InvalidArgumentException(sprintf('Class "%s" does not have an identifier', $class->name)); |
||
| 1470 | 306 | } |
|
| 1471 | |||
| 1472 | 306 | $serializedId = serialize($class->getDatabaseIdentifierValue($id)); |
|
| 1473 | |||
| 1474 | return $this->identityMap[$class->name][$serializedId] ?? false; |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Schedules a document for dirty-checking at commit-time. |
||
| 1479 | */ |
||
| 1480 | 3 | public function scheduleForSynchronization(object $document) : void |
|
| 1481 | { |
||
| 1482 | 3 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1483 | 3 | $this->scheduledForSynchronization[$class->name][spl_object_hash($document)] = $document; |
|
| 1484 | 3 | } |
|
| 1485 | |||
| 1486 | /** |
||
| 1487 | * Checks whether a document is registered in the identity map. |
||
| 1488 | */ |
||
| 1489 | 92 | public function isInIdentityMap(object $document) : bool |
|
| 1502 | |||
| 1503 | 680 | private function getIdForIdentityMap(object $document) : string |
|
| 1516 | |||
| 1517 | /** |
||
| 1518 | * INTERNAL: |
||
| 1519 | * Checks whether an identifier exists in the identity map. |
||
| 1520 | * |
||
| 1521 | * @ignore |
||
| 1522 | */ |
||
| 1523 | public function containsId($id, string $rootClassName) : bool |
||
| 1527 | |||
| 1528 | /** |
||
| 1529 | * Persists a document as part of the current unit of work. |
||
| 1530 | * |
||
| 1531 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1532 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1533 | */ |
||
| 1534 | 642 | public function persist(object $document) : void |
|
| 1543 | |||
| 1544 | /** |
||
| 1545 | * Saves a document as part of the current unit of work. |
||
| 1546 | * This method is internally called during save() cascades as it tracks |
||
| 1547 | * the already visited documents to prevent infinite recursions. |
||
| 1548 | * |
||
| 1549 | * NOTE: This method always considers documents that are not yet known to |
||
| 1550 | * this UnitOfWork as NEW. |
||
| 1551 | * |
||
| 1552 | * @throws InvalidArgumentException |
||
| 1553 | * @throws MongoDBException |
||
| 1554 | */ |
||
| 1555 | 641 | private function doPersist(object $document, array &$visited) : void |
|
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Deletes a document as part of the current unit of work. |
||
| 1603 | */ |
||
| 1604 | 83 | public function remove(object $document) |
|
| 1609 | |||
| 1610 | /** |
||
| 1611 | * Deletes a document as part of the current unit of work. |
||
| 1612 | * |
||
| 1613 | * This method is internally called during delete() cascades as it tracks |
||
| 1614 | * the already visited documents to prevent infinite recursions. |
||
| 1615 | * |
||
| 1616 | * @throws MongoDBException |
||
| 1617 | */ |
||
| 1618 | 83 | private function doRemove(object $document, array &$visited) : void |
|
| 1650 | |||
| 1651 | /** |
||
| 1652 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1653 | */ |
||
| 1654 | 11 | public function merge(object $document) : object |
|
| 1660 | |||
| 1661 | /** |
||
| 1662 | * Executes a merge operation on a document. |
||
| 1663 | * |
||
| 1664 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1665 | * @throws LockException If the document uses optimistic locking through a |
||
| 1666 | * version attribute and the version check against the |
||
| 1667 | * managed copy fails. |
||
| 1668 | */ |
||
| 1669 | 11 | private function doMerge(object $document, array &$visited, ?object $prevManagedCopy = null, ?array $assoc = null) : object |
|
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Detaches a document from the persistence management. It's persistence will |
||
| 1848 | * no longer be managed by Doctrine. |
||
| 1849 | */ |
||
| 1850 | 11 | public function detach(object $document) : void |
|
| 1855 | |||
| 1856 | /** |
||
| 1857 | * Executes a detach operation on the given document. |
||
| 1858 | * |
||
| 1859 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1860 | */ |
||
| 1861 | 17 | private function doDetach(object $document, array &$visited) : void |
|
| 1893 | |||
| 1894 | /** |
||
| 1895 | * Refreshes the state of the given document from the database, overwriting |
||
| 1896 | * any local, unpersisted changes. |
||
| 1897 | * |
||
| 1898 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1899 | */ |
||
| 1900 | 24 | public function refresh(object $document) : void |
|
| 1905 | |||
| 1906 | /** |
||
| 1907 | * Executes a refresh operation on a document. |
||
| 1908 | * |
||
| 1909 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1910 | */ |
||
| 1911 | 24 | private function doRefresh(object $document, array &$visited) : void |
|
| 1932 | |||
| 1933 | /** |
||
| 1934 | * Cascades a refresh operation to associated documents. |
||
| 1935 | */ |
||
| 1936 | 23 | private function cascadeRefresh(object $document, array &$visited) : void |
|
| 1937 | { |
||
| 1938 | 23 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1939 | |||
| 1940 | 23 | $associationMappings = array_filter( |
|
| 1941 | 23 | $class->associationMappings, |
|
| 1942 | static function ($assoc) { |
||
| 1943 | 18 | return $assoc['isCascadeRefresh']; |
|
| 1944 | 23 | } |
|
| 1945 | ); |
||
| 1946 | |||
| 1947 | 23 | foreach ($associationMappings as $mapping) { |
|
| 1948 | 15 | $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 1949 | 15 | if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) { |
|
| 1950 | 15 | if ($relatedDocuments instanceof PersistentCollectionInterface) { |
|
| 1951 | // Unwrap so that foreach() does not initialize |
||
| 1952 | 15 | $relatedDocuments = $relatedDocuments->unwrap(); |
|
| 1953 | } |
||
| 1954 | 15 | foreach ($relatedDocuments as $relatedDocument) { |
|
| 1955 | $this->doRefresh($relatedDocument, $visited); |
||
| 1956 | } |
||
| 1957 | 10 | } elseif ($relatedDocuments !== null) { |
|
| 1958 | 2 | $this->doRefresh($relatedDocuments, $visited); |
|
| 1959 | } |
||
| 1960 | } |
||
| 1961 | 23 | } |
|
| 1962 | |||
| 1963 | /** |
||
| 1964 | * Cascades a detach operation to associated documents. |
||
| 1965 | */ |
||
| 1966 | 17 | private function cascadeDetach(object $document, array &$visited) : void |
|
| 1987 | /** |
||
| 1988 | * Cascades a merge operation to associated documents. |
||
| 1989 | */ |
||
| 1990 | 11 | private function cascadeMerge(object $document, object $managedCopy, array &$visited) : void |
|
| 2018 | |||
| 2019 | /** |
||
| 2020 | * Cascades the save operation to associated documents. |
||
| 2021 | */ |
||
| 2022 | 638 | private function cascadePersist(object $document, array &$visited) : void |
|
| 2071 | |||
| 2072 | /** |
||
| 2073 | * Cascades the delete operation to associated documents. |
||
| 2074 | */ |
||
| 2075 | 83 | private function cascadeRemove(object $document, array &$visited) : void |
|
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Acquire a lock on the given document. |
||
| 2100 | * |
||
| 2101 | * @throws LockException |
||
| 2102 | * @throws InvalidArgumentException |
||
| 2103 | */ |
||
| 2104 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
| 2128 | |||
| 2129 | /** |
||
| 2130 | * Releases a lock on the given document. |
||
| 2131 | * |
||
| 2132 | * @throws InvalidArgumentException |
||
| 2133 | */ |
||
| 2134 | 1 | public function unlock(object $document) : void |
|
| 2142 | |||
| 2143 | /** |
||
| 2144 | * Clears the UnitOfWork. |
||
| 2145 | */ |
||
| 2146 | 378 | public function clear(?string $documentName = null) : void |
|
| 2184 | |||
| 2185 | /** |
||
| 2186 | * INTERNAL: |
||
| 2187 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2188 | * invoked on that document at the beginning of the next commit of this |
||
| 2189 | * UnitOfWork. |
||
| 2190 | * |
||
| 2191 | * @ignore |
||
| 2192 | */ |
||
| 2193 | 58 | public function scheduleOrphanRemoval(object $document) : void |
|
| 2197 | |||
| 2198 | /** |
||
| 2199 | * INTERNAL: |
||
| 2200 | * Unschedules an embedded or referenced object for removal. |
||
| 2201 | * |
||
| 2202 | * @ignore |
||
| 2203 | */ |
||
| 2204 | 123 | public function unscheduleOrphanRemoval(object $document) : void |
|
| 2209 | |||
| 2210 | /** |
||
| 2211 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2212 | * 1) sets owner if it was cloned |
||
| 2213 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2214 | * 3) NOP if state is OK |
||
| 2215 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2216 | */ |
||
| 2217 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, object $document, ClassMetadata $class, string $propName) : PersistentCollectionInterface |
|
| 2237 | |||
| 2238 | /** |
||
| 2239 | * INTERNAL: |
||
| 2240 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2241 | */ |
||
| 2242 | 47 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2253 | |||
| 2254 | /** |
||
| 2255 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2256 | */ |
||
| 2257 | 220 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) : bool |
|
| 2261 | |||
| 2262 | /** |
||
| 2263 | * INTERNAL: |
||
| 2264 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2265 | */ |
||
| 2266 | 227 | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2281 | |||
| 2282 | /** |
||
| 2283 | * INTERNAL: |
||
| 2284 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2285 | */ |
||
| 2286 | 254 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2303 | |||
| 2304 | /** |
||
| 2305 | * INTERNAL: |
||
| 2306 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2307 | */ |
||
| 2308 | 227 | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2323 | |||
| 2324 | /** |
||
| 2325 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2326 | */ |
||
| 2327 | 140 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) : bool |
|
| 2331 | |||
| 2332 | /** |
||
| 2333 | * INTERNAL: |
||
| 2334 | * Gets PersistentCollections that have been visited during computing change |
||
| 2335 | * set of $document |
||
| 2336 | * |
||
| 2337 | * @return PersistentCollectionInterface[] |
||
| 2338 | */ |
||
| 2339 | 581 | public function getVisitedCollections(object $document) : array |
|
| 2345 | |||
| 2346 | /** |
||
| 2347 | * INTERNAL: |
||
| 2348 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2349 | * |
||
| 2350 | * @return PersistentCollectionInterface[] |
||
| 2351 | */ |
||
| 2352 | 581 | public function getScheduledCollections(object $document) : array |
|
| 2358 | |||
| 2359 | /** |
||
| 2360 | * Checks whether the document is related to a PersistentCollection |
||
| 2361 | * scheduled for update or deletion. |
||
| 2362 | */ |
||
| 2363 | 56 | public function hasScheduledCollections(object $document) : bool |
|
| 2367 | |||
| 2368 | /** |
||
| 2369 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2370 | * a collection scheduled for update or deletion. |
||
| 2371 | * |
||
| 2372 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2373 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2374 | * |
||
| 2375 | * If the collection is nested within atomic collection, it is immediately |
||
| 2376 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2377 | * calculating update data way easier. |
||
| 2378 | */ |
||
| 2379 | 256 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) : void |
|
| 2409 | |||
| 2410 | /** |
||
| 2411 | * Get the top-most owning document of a given document |
||
| 2412 | * |
||
| 2413 | * If a top-level document is provided, that same document will be returned. |
||
| 2414 | * For an embedded document, we will walk through parent associations until |
||
| 2415 | * we find a top-level document. |
||
| 2416 | * |
||
| 2417 | * @throws UnexpectedValueException When a top-level document could not be found. |
||
| 2418 | */ |
||
| 2419 | 258 | public function getOwningDocument(object $document) : object |
|
| 2435 | |||
| 2436 | /** |
||
| 2437 | * Gets the class name for an association (embed or reference) with respect |
||
| 2438 | * to any discriminator value. |
||
| 2439 | * |
||
| 2440 | * @param array|null $data |
||
| 2441 | */ |
||
| 2442 | 227 | public function getClassNameForAssociation(array $mapping, $data) : string |
|
| 2472 | |||
| 2473 | /** |
||
| 2474 | * INTERNAL: |
||
| 2475 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2476 | * |
||
| 2477 | * @internal Highly performance-sensitive method. |
||
| 2478 | * |
||
| 2479 | * @ignore |
||
| 2480 | */ |
||
| 2481 | 402 | public function getOrCreateDocument(string $className, array $data, array &$hints = [], ?object $document = null) : object |
|
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2557 | */ |
||
| 2558 | 178 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Gets the identity map of the UnitOfWork. |
||
| 2570 | */ |
||
| 2571 | public function getIdentityMap() : array |
||
| 2575 | |||
| 2576 | /** |
||
| 2577 | * Gets the original data of a document. The original data is the data that was |
||
| 2578 | * present at the time the document was reconstituted from the database. |
||
| 2579 | * |
||
| 2580 | * @return array |
||
| 2581 | */ |
||
| 2582 | 1 | public function getOriginalDocumentData(object $document) : array |
|
| 2588 | |||
| 2589 | 60 | public function setOriginalDocumentData(object $document, array $data) : void |
|
| 2595 | |||
| 2596 | /** |
||
| 2597 | * INTERNAL: |
||
| 2598 | * Sets a property value of the original data array of a document. |
||
| 2599 | * |
||
| 2600 | * @param mixed $value |
||
| 2601 | * |
||
| 2602 | * @ignore |
||
| 2603 | */ |
||
| 2604 | 3 | public function setOriginalDocumentProperty(string $oid, string $property, $value) : void |
|
| 2608 | |||
| 2609 | /** |
||
| 2610 | * Gets the identifier of a document. |
||
| 2611 | * |
||
| 2612 | * @return mixed The identifier value |
||
| 2613 | */ |
||
| 2614 | 452 | public function getDocumentIdentifier(object $document) |
|
| 2618 | |||
| 2619 | /** |
||
| 2620 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2621 | * |
||
| 2622 | * @return bool TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2623 | */ |
||
| 2624 | public function hasPendingInsertions() : bool |
||
| 2628 | |||
| 2629 | /** |
||
| 2630 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2631 | * number of documents in the identity map. |
||
| 2632 | */ |
||
| 2633 | 2 | public function size() : int |
|
| 2641 | |||
| 2642 | /** |
||
| 2643 | * INTERNAL: |
||
| 2644 | * Registers a document as managed. |
||
| 2645 | * |
||
| 2646 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2647 | * document class. If the class expects its database identifier to be an |
||
| 2648 | * ObjectId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2649 | * document identifiers map will become inconsistent with the identity map. |
||
| 2650 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2651 | * conversion and throw an exception if it's inconsistent. |
||
| 2652 | * |
||
| 2653 | * @param mixed $id The identifier values. |
||
| 2654 | */ |
||
| 2655 | 381 | public function registerManaged(object $document, $id, array $data) : void |
|
| 2670 | |||
| 2671 | /** |
||
| 2672 | * INTERNAL: |
||
| 2673 | * Clears the property changeset of the document with the given OID. |
||
| 2674 | */ |
||
| 2675 | public function clearDocumentChangeSet(string $oid) |
||
| 2679 | |||
| 2680 | /* PropertyChangedListener implementation */ |
||
| 2681 | |||
| 2682 | /** |
||
| 2683 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2684 | * |
||
| 2685 | * @param object $document The document that owns the property. |
||
| 2686 | * @param string $propertyName The name of the property that changed. |
||
| 2687 | * @param mixed $oldValue The old value of the property. |
||
| 2688 | * @param mixed $newValue The new value of the property. |
||
| 2689 | */ |
||
| 2690 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2707 | |||
| 2708 | /** |
||
| 2709 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2710 | */ |
||
| 2711 | 3 | public function getScheduledDocumentInsertions() : array |
|
| 2715 | |||
| 2716 | /** |
||
| 2717 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2718 | */ |
||
| 2719 | 1 | public function getScheduledDocumentUpserts() : array |
|
| 2723 | |||
| 2724 | /** |
||
| 2725 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2726 | */ |
||
| 2727 | 2 | public function getScheduledDocumentUpdates() : array |
|
| 2731 | |||
| 2732 | /** |
||
| 2733 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2734 | */ |
||
| 2735 | public function getScheduledDocumentDeletions() : array |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * Get the currently scheduled complete collection deletions |
||
| 2742 | */ |
||
| 2743 | public function getScheduledCollectionDeletions() : array |
||
| 2747 | |||
| 2748 | /** |
||
| 2749 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2750 | */ |
||
| 2751 | public function getScheduledCollectionUpdates() : array |
||
| 2755 | |||
| 2756 | /** |
||
| 2757 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2758 | */ |
||
| 2759 | public function initializeObject(object $obj) : void |
||
| 2767 | |||
| 2768 | private function objToStr(object $obj) : string |
||
| 2772 | } |
||
| 2773 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.