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 |
||
| 42 | class UnitOfWork implements PropertyChangedListener |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 46 | */ |
||
| 47 | public const STATE_MANAGED = 1; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 51 | * and is not (yet) managed by a DocumentManager. |
||
| 52 | */ |
||
| 53 | public const STATE_NEW = 2; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A detached document is an instance with a persistent identity that is not |
||
| 57 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 58 | */ |
||
| 59 | public const STATE_DETACHED = 3; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * A removed document instance is an instance with a persistent identity, |
||
| 63 | * associated with a DocumentManager, whose persistent state has been |
||
| 64 | * deleted (or is scheduled for deletion). |
||
| 65 | */ |
||
| 66 | public const STATE_REMOVED = 4; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The identity map holds references to all managed documents. |
||
| 70 | * |
||
| 71 | * Documents are grouped by their class name, and then indexed by the |
||
| 72 | * serialized string of their database identifier field or, if the class |
||
| 73 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 74 | * differentiation of values that may be equal (via type juggling) but not |
||
| 75 | * identical. |
||
| 76 | * |
||
| 77 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 78 | * we always take the root class name of the hierarchy. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $identityMap = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Map of all identifiers of managed documents. |
||
| 86 | * Keys are object ids (spl_object_hash). |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $documentIdentifiers = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Map of the original document data of managed documents. |
||
| 94 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 95 | * at commit time. |
||
| 96 | * |
||
| 97 | * @var array |
||
| 98 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 99 | * A value will only really be copied if the value in the document is modified |
||
| 100 | * by the user. |
||
| 101 | */ |
||
| 102 | private $originalDocumentData = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 106 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private $documentChangeSets = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The (cached) states of any known documents. |
||
| 114 | * Keys are object ids (spl_object_hash). |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | private $documentStates = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 122 | * |
||
| 123 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 124 | * object hash. This is only used for documents with a change tracking |
||
| 125 | * policy of DEFERRED_EXPLICIT. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | * @todo rename: scheduledForSynchronization |
||
| 129 | */ |
||
| 130 | private $scheduledForDirtyCheck = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * A list of all pending document insertions. |
||
| 134 | * |
||
| 135 | * @var array |
||
| 136 | */ |
||
| 137 | private $documentInsertions = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * A list of all pending document updates. |
||
| 141 | * |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | private $documentUpdates = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * A list of all pending document upserts. |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | private $documentUpserts = []; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * A list of all pending document deletions. |
||
| 155 | * |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | private $documentDeletions = []; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * All pending collection deletions. |
||
| 162 | * |
||
| 163 | * @var array |
||
| 164 | */ |
||
| 165 | private $collectionDeletions = []; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * All pending collection updates. |
||
| 169 | * |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | private $collectionUpdates = []; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * A list of documents related to collections scheduled for update or deletion |
||
| 176 | * |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | private $hasScheduledCollections = []; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 183 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 184 | * of their data. |
||
| 185 | * |
||
| 186 | * @var array |
||
| 187 | */ |
||
| 188 | private $visitedCollections = []; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 192 | * |
||
| 193 | * @var DocumentManager |
||
| 194 | */ |
||
| 195 | private $dm; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * The EventManager used for dispatching events. |
||
| 199 | * |
||
| 200 | * @var EventManager |
||
| 201 | */ |
||
| 202 | private $evm; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Additional documents that are scheduled for removal. |
||
| 206 | * |
||
| 207 | * @var array |
||
| 208 | */ |
||
| 209 | private $orphanRemovals = []; |
||
| 210 | |||
| 211 | /** |
||
| 212 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 213 | * |
||
| 214 | * @var HydratorFactory |
||
| 215 | */ |
||
| 216 | private $hydratorFactory; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * The document persister instances used to persist document instances. |
||
| 220 | * |
||
| 221 | * @var array |
||
| 222 | */ |
||
| 223 | private $persisters = []; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * The collection persister instance used to persist changes to collections. |
||
| 227 | * |
||
| 228 | * @var Persisters\CollectionPersister |
||
| 229 | */ |
||
| 230 | private $collectionPersister; |
||
| 231 | |||
| 232 | /** |
||
| 233 | * The persistence builder instance used in DocumentPersisters. |
||
| 234 | * |
||
| 235 | * @var PersistenceBuilder |
||
| 236 | */ |
||
| 237 | private $persistenceBuilder; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Array of parent associations between embedded documents. |
||
| 241 | * |
||
| 242 | * @var array |
||
| 243 | */ |
||
| 244 | private $parentAssociations = []; |
||
| 245 | |||
| 246 | /** @var LifecycleEventManager */ |
||
| 247 | private $lifecycleEventManager; |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 251 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 252 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 253 | * |
||
| 254 | * @var array |
||
| 255 | */ |
||
| 256 | private $embeddedDocumentsRegistry = []; |
||
| 257 | |||
| 258 | /** @var int */ |
||
| 259 | private $commitsInProgress = 0; |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 263 | * |
||
| 264 | */ |
||
| 265 | 1580 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 266 | { |
||
| 267 | 1580 | $this->dm = $dm; |
|
| 268 | 1580 | $this->evm = $evm; |
|
| 269 | 1580 | $this->hydratorFactory = $hydratorFactory; |
|
| 270 | 1580 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 271 | 1580 | } |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 275 | * queries for insert persistence. |
||
| 276 | * |
||
| 277 | * @return PersistenceBuilder $pb |
||
| 278 | */ |
||
| 279 | 1076 | public function getPersistenceBuilder() |
|
| 280 | { |
||
| 281 | 1076 | if (! $this->persistenceBuilder) { |
|
| 282 | 1076 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 283 | } |
||
| 284 | 1076 | return $this->persistenceBuilder; |
|
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the parent association for a given embedded document. |
||
| 289 | * |
||
| 290 | * @param object $document |
||
| 291 | * @param array $mapping |
||
| 292 | * @param object $parent |
||
| 293 | * @param string $propertyPath |
||
| 294 | */ |
||
| 295 | 177 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 296 | { |
||
| 297 | 177 | $oid = spl_object_hash($document); |
|
| 298 | 177 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 299 | 177 | $this->parentAssociations[$oid] = [$mapping, $parent, $propertyPath]; |
|
| 300 | 177 | } |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Gets the parent association for a given embedded document. |
||
| 304 | * |
||
| 305 | * <code> |
||
| 306 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 307 | * </code> |
||
| 308 | * |
||
| 309 | * @param object $document |
||
| 310 | * @return array $association |
||
| 311 | */ |
||
| 312 | 204 | public function getParentAssociation($document) |
|
| 313 | { |
||
| 314 | 204 | $oid = spl_object_hash($document); |
|
| 315 | |||
| 316 | 204 | return $this->parentAssociations[$oid] ?? null; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get the document persister instance for the given document name |
||
| 321 | * |
||
| 322 | * @param string $documentName |
||
| 323 | * @return Persisters\DocumentPersister |
||
| 324 | */ |
||
| 325 | 1074 | public function getDocumentPersister($documentName) |
|
| 326 | { |
||
| 327 | 1074 | if (! isset($this->persisters[$documentName])) { |
|
| 328 | 1061 | $class = $this->dm->getClassMetadata($documentName); |
|
| 329 | 1061 | $pb = $this->getPersistenceBuilder(); |
|
| 330 | 1061 | $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this, $this->hydratorFactory, $class); |
|
| 331 | } |
||
| 332 | 1074 | return $this->persisters[$documentName]; |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the collection persister instance. |
||
| 337 | * |
||
| 338 | * @return CollectionPersister |
||
| 339 | */ |
||
| 340 | 1074 | public function getCollectionPersister() |
|
| 341 | { |
||
| 342 | 1074 | if (! isset($this->collectionPersister)) { |
|
| 343 | 1074 | $pb = $this->getPersistenceBuilder(); |
|
| 344 | 1074 | $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this); |
|
| 345 | } |
||
| 346 | 1074 | return $this->collectionPersister; |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set the document persister instance to use for the given document name |
||
| 351 | * |
||
| 352 | * @param string $documentName |
||
| 353 | */ |
||
| 354 | 13 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 355 | { |
||
| 356 | 13 | $this->persisters[$documentName] = $persister; |
|
| 357 | 13 | } |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 361 | * up to this point. The state of all managed documents will be synchronized with |
||
| 362 | * the database. |
||
| 363 | * |
||
| 364 | * The operations are executed in the following order: |
||
| 365 | * |
||
| 366 | * 1) All document insertions |
||
| 367 | * 2) All document updates |
||
| 368 | * 3) All document deletions |
||
| 369 | * |
||
| 370 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 371 | */ |
||
| 372 | 566 | public function commit(array $options = []) |
|
| 373 | { |
||
| 374 | // Raise preFlush |
||
| 375 | 566 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 376 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 377 | } |
||
| 378 | |||
| 379 | // Compute changes done since last commit. |
||
| 380 | 566 | $this->computeChangeSets(); |
|
| 381 | |||
| 382 | 565 | if (! ($this->documentInsertions || |
|
|
|
|||
| 383 | 240 | $this->documentUpserts || |
|
| 384 | 199 | $this->documentDeletions || |
|
| 385 | 186 | $this->documentUpdates || |
|
| 386 | 22 | $this->collectionUpdates || |
|
| 387 | 22 | $this->collectionDeletions || |
|
| 388 | 565 | $this->orphanRemovals) |
|
| 389 | ) { |
||
| 390 | 22 | return; // Nothing to do. |
|
| 391 | } |
||
| 392 | |||
| 393 | 562 | $this->commitsInProgress++; |
|
| 394 | 562 | if ($this->commitsInProgress > 1) { |
|
| 395 | throw MongoDBException::commitInProgress(); |
||
| 396 | } |
||
| 397 | try { |
||
| 398 | 562 | if ($this->orphanRemovals) { |
|
| 399 | 44 | foreach ($this->orphanRemovals as $removal) { |
|
| 400 | 44 | $this->remove($removal); |
|
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | // Raise onFlush |
||
| 405 | 562 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 406 | 4 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 407 | } |
||
| 408 | |||
| 409 | 561 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 410 | 84 | list($class, $documents) = $classAndDocuments; |
|
| 411 | 84 | $this->executeUpserts($class, $documents, $options); |
|
| 412 | } |
||
| 413 | |||
| 414 | 561 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 415 | 488 | list($class, $documents) = $classAndDocuments; |
|
| 416 | 488 | $this->executeInserts($class, $documents, $options); |
|
| 417 | } |
||
| 418 | |||
| 419 | 560 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 420 | 205 | list($class, $documents) = $classAndDocuments; |
|
| 421 | 205 | $this->executeUpdates($class, $documents, $options); |
|
| 422 | } |
||
| 423 | |||
| 424 | 560 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 425 | 64 | list($class, $documents) = $classAndDocuments; |
|
| 426 | 64 | $this->executeDeletions($class, $documents, $options); |
|
| 427 | } |
||
| 428 | |||
| 429 | // Raise postFlush |
||
| 430 | 560 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 431 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 432 | } |
||
| 433 | |||
| 434 | // Clear up |
||
| 435 | 560 | $this->documentInsertions = |
|
| 436 | 560 | $this->documentUpserts = |
|
| 437 | 560 | $this->documentUpdates = |
|
| 438 | 560 | $this->documentDeletions = |
|
| 439 | 560 | $this->documentChangeSets = |
|
| 440 | 560 | $this->collectionUpdates = |
|
| 441 | 560 | $this->collectionDeletions = |
|
| 442 | 560 | $this->visitedCollections = |
|
| 443 | 560 | $this->scheduledForDirtyCheck = |
|
| 444 | 560 | $this->orphanRemovals = |
|
| 445 | 560 | $this->hasScheduledCollections = []; |
|
| 446 | 560 | } finally { |
|
| 447 | 562 | $this->commitsInProgress--; |
|
| 448 | } |
||
| 449 | 560 | } |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Groups a list of scheduled documents by their class. |
||
| 453 | * |
||
| 454 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 455 | * @param bool $includeEmbedded |
||
| 456 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 457 | */ |
||
| 458 | 561 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Compute changesets of all documents scheduled for insertion. |
||
| 490 | * |
||
| 491 | * Embedded documents will not be processed. |
||
| 492 | */ |
||
| 493 | 569 | private function computeScheduleInsertsChangeSets() |
|
| 494 | { |
||
| 495 | 569 | foreach ($this->documentInsertions as $document) { |
|
| 496 | 499 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 497 | 499 | if ($class->isEmbeddedDocument) { |
|
| 498 | 138 | continue; |
|
| 499 | } |
||
| 500 | |||
| 501 | 494 | $this->computeChangeSet($class, $document); |
|
| 502 | } |
||
| 503 | 568 | } |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Compute changesets of all documents scheduled for upsert. |
||
| 507 | * |
||
| 508 | * Embedded documents will not be processed. |
||
| 509 | */ |
||
| 510 | 568 | private function computeScheduleUpsertsChangeSets() |
|
| 511 | { |
||
| 512 | 568 | foreach ($this->documentUpserts as $document) { |
|
| 513 | 83 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 514 | 83 | if ($class->isEmbeddedDocument) { |
|
| 515 | continue; |
||
| 516 | } |
||
| 517 | |||
| 518 | 83 | $this->computeChangeSet($class, $document); |
|
| 519 | } |
||
| 520 | 568 | } |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Gets the changeset for a document. |
||
| 524 | * |
||
| 525 | * @param object $document |
||
| 526 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 527 | */ |
||
| 528 | 563 | public function getDocumentChangeSet($document) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * INTERNAL: |
||
| 537 | * Sets the changeset for a document. |
||
| 538 | * |
||
| 539 | * @param object $document |
||
| 540 | * @param array $changeset |
||
| 541 | */ |
||
| 542 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 549 | * |
||
| 550 | * @param object $document |
||
| 551 | * @return array |
||
| 552 | */ |
||
| 553 | 570 | public function getDocumentActualData($document) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Computes the changes that happened to a single document. |
||
| 586 | * |
||
| 587 | * Modifies/populates the following properties: |
||
| 588 | * |
||
| 589 | * {@link originalDocumentData} |
||
| 590 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 591 | * then it was not fetched from the database and therefore we have no original |
||
| 592 | * document data yet. All of the current document data is stored as the original document data. |
||
| 593 | * |
||
| 594 | * {@link documentChangeSets} |
||
| 595 | * The changes detected on all properties of the document are stored there. |
||
| 596 | * A change is a tuple array where the first entry is the old value and the second |
||
| 597 | * entry is the new value of the property. Changesets are used by persisters |
||
| 598 | * to INSERT/UPDATE the persistent document state. |
||
| 599 | * |
||
| 600 | * {@link documentUpdates} |
||
| 601 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 602 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 603 | * there to mark it for an update. |
||
| 604 | * |
||
| 605 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 606 | * @param object $document The document for which to compute the changes. |
||
| 607 | */ |
||
| 608 | 566 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 624 | * |
||
| 625 | * @param object $document |
||
| 626 | * @param bool $recompute |
||
| 627 | */ |
||
| 628 | 566 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 629 | { |
||
| 630 | 566 | $oid = spl_object_hash($document); |
|
| 631 | 566 | $actualData = $this->getDocumentActualData($document); |
|
| 632 | 566 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 633 | 566 | if ($isNewDocument) { |
|
| 634 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 635 | // These result in an INSERT. |
||
| 636 | 566 | $this->originalDocumentData[$oid] = $actualData; |
|
| 637 | 566 | $changeSet = []; |
|
| 638 | 566 | foreach ($actualData as $propName => $actualValue) { |
|
| 639 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 640 | * was cloned and its ownership must be fixed |
||
| 641 | */ |
||
| 642 | 566 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 643 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 644 | $actualValue = $actualData[$propName]; |
||
| 645 | } |
||
| 646 | // ignore inverse side of reference relationship |
||
| 647 | 566 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
|
| 648 | 179 | continue; |
|
| 649 | } |
||
| 650 | 566 | $changeSet[$propName] = [null, $actualValue]; |
|
| 651 | } |
||
| 652 | 566 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 653 | } else { |
||
| 654 | 259 | if ($class->isReadOnly) { |
|
| 655 | 2 | return; |
|
| 656 | } |
||
| 657 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 658 | // and we have a copy of the original data |
||
| 659 | 257 | $originalData = $this->originalDocumentData[$oid]; |
|
| 660 | 257 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 661 | 257 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 662 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 663 | } else { |
||
| 664 | 257 | $changeSet = []; |
|
| 665 | } |
||
| 666 | |||
| 667 | 257 | foreach ($actualData as $propName => $actualValue) { |
|
| 668 | // skip not saved fields |
||
| 669 | 257 | if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) { |
|
| 670 | continue; |
||
| 671 | } |
||
| 672 | |||
| 673 | 257 | $orgValue = $originalData[$propName] ?? null; |
|
| 674 | |||
| 675 | // skip if value has not changed |
||
| 676 | 257 | if ($orgValue === $actualValue) { |
|
| 677 | 256 | if (! $actualValue instanceof PersistentCollectionInterface) { |
|
| 678 | 256 | continue; |
|
| 679 | } |
||
| 680 | |||
| 681 | 177 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
|
| 682 | // consider dirty collections as changed as well |
||
| 683 | 153 | continue; |
|
| 684 | } |
||
| 685 | } |
||
| 686 | |||
| 687 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 688 | 221 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 689 | 13 | if ($orgValue !== null) { |
|
| 690 | 8 | $this->scheduleOrphanRemoval($orgValue); |
|
| 691 | } |
||
| 692 | 13 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 693 | 13 | continue; |
|
| 694 | } |
||
| 695 | |||
| 696 | // if owning side of reference-one relationship |
||
| 697 | 215 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 698 | 12 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 699 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 700 | } |
||
| 701 | |||
| 702 | 12 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 703 | 12 | continue; |
|
| 704 | } |
||
| 705 | |||
| 706 | 209 | if ($isChangeTrackingNotify) { |
|
| 707 | 3 | continue; |
|
| 708 | } |
||
| 709 | |||
| 710 | // ignore inverse side of reference relationship |
||
| 711 | 207 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
|
| 712 | 6 | continue; |
|
| 713 | } |
||
| 714 | |||
| 715 | // Persistent collection was exchanged with the "originally" |
||
| 716 | // created one. This can only mean it was cloned and replaced |
||
| 717 | // on another document. |
||
| 718 | 205 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 719 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 720 | } |
||
| 721 | |||
| 722 | // if embed-many or reference-many relationship |
||
| 723 | 205 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 724 | 101 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 725 | /* If original collection was exchanged with a non-empty value |
||
| 726 | * and $set will be issued, there is no need to $unset it first |
||
| 727 | */ |
||
| 728 | 101 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 729 | 19 | continue; |
|
| 730 | } |
||
| 731 | 88 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
|
| 732 | 15 | $this->scheduleCollectionDeletion($orgValue); |
|
| 733 | } |
||
| 734 | 88 | continue; |
|
| 735 | } |
||
| 736 | |||
| 737 | // skip equivalent date values |
||
| 738 | 133 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 739 | /** @var DateType $dateType */ |
||
| 740 | 37 | $dateType = Type::getType('date'); |
|
| 741 | 37 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 742 | 37 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 743 | |||
| 744 | 37 | $orgTimestamp = $dbOrgValue instanceof UTCDateTime ? $dbOrgValue->toDateTime()->getTimestamp() : null; |
|
| 745 | 37 | $actualTimestamp = $dbActualValue instanceof UTCDateTime ? $dbActualValue->toDateTime()->getTimestamp() : null; |
|
| 746 | |||
| 747 | 37 | if ($orgTimestamp === $actualTimestamp) { |
|
| 748 | 30 | continue; |
|
| 749 | } |
||
| 750 | } |
||
| 751 | |||
| 752 | // regular field |
||
| 753 | 116 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 754 | } |
||
| 755 | 257 | if ($changeSet) { |
|
| 756 | 210 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 757 | 16 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 758 | 208 | : $changeSet; |
|
| 759 | |||
| 760 | 210 | $this->originalDocumentData[$oid] = $actualData; |
|
| 761 | 210 | $this->scheduleForUpdate($document); |
|
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | // Look for changes in associations of the document |
||
| 766 | 566 | $associationMappings = array_filter( |
|
| 767 | 566 | $class->associationMappings, |
|
| 768 | function ($assoc) { |
||
| 769 | 429 | return empty($assoc['notSaved']); |
|
| 770 | 566 | } |
|
| 771 | ); |
||
| 772 | |||
| 773 | 566 | foreach ($associationMappings as $mapping) { |
|
| 774 | 429 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 775 | |||
| 776 | 429 | if ($value === null) { |
|
| 777 | 292 | continue; |
|
| 778 | } |
||
| 779 | |||
| 780 | 418 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 781 | |||
| 782 | 417 | if (isset($mapping['reference'])) { |
|
| 783 | 314 | continue; |
|
| 784 | } |
||
| 785 | |||
| 786 | 321 | $values = $mapping['type'] === ClassMetadata::ONE ? [$value] : $value->unwrap(); |
|
| 787 | |||
| 788 | 321 | foreach ($values as $obj) { |
|
| 789 | 158 | $oid2 = spl_object_hash($obj); |
|
| 790 | |||
| 791 | 158 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 792 | 156 | if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) { |
|
| 793 | // instance of $value is the same as it was previously otherwise there would be |
||
| 794 | // change set already in place |
||
| 795 | 34 | $this->documentChangeSets[$oid][$mapping['fieldName']] = [$value, $value]; |
|
| 796 | } |
||
| 797 | |||
| 798 | 156 | if (! $isNewDocument) { |
|
| 799 | 65 | $this->scheduleForUpdate($document); |
|
| 800 | } |
||
| 801 | |||
| 802 | 321 | break; |
|
| 803 | } |
||
| 804 | } |
||
| 805 | } |
||
| 806 | 565 | } |
|
| 807 | |||
| 808 | /** |
||
| 809 | * Computes all the changes that have been done to documents and collections |
||
| 810 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 811 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 812 | */ |
||
| 813 | 569 | public function computeChangeSets() |
|
| 864 | |||
| 865 | /** |
||
| 866 | * Computes the changes of an association. |
||
| 867 | * |
||
| 868 | * @param object $parentDocument |
||
| 869 | * @param array $assoc |
||
| 870 | * @param mixed $value The value of the association. |
||
| 871 | * @throws \InvalidArgumentException |
||
| 872 | */ |
||
| 873 | 418 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 977 | |||
| 978 | /** |
||
| 979 | * INTERNAL: |
||
| 980 | * Computes the changeset of an individual document, independently of the |
||
| 981 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 982 | * |
||
| 983 | * The passed document must be a managed document. If the document 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 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 989 | * @param object $document The document for which to (re)calculate the change set. |
||
| 990 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 991 | */ |
||
| 992 | 17 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * @param object $document |
||
| 1014 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1015 | */ |
||
| 1016 | 596 | private function persistNew(ClassMetadata $class, $document) |
|
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Executes all document insertions for documents of the specified type. |
||
| 1062 | * |
||
| 1063 | * @param array $documents Array of documents to insert |
||
| 1064 | * @param array $options Array of options to be used with batchInsert() |
||
| 1065 | */ |
||
| 1066 | 488 | private function executeInserts(ClassMetadata $class, array $documents, array $options = []) |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Executes all document upserts for documents of the specified type. |
||
| 1084 | * |
||
| 1085 | * @param array $documents Array of documents to upsert |
||
| 1086 | * @param array $options Array of options to be used with batchInsert() |
||
| 1087 | */ |
||
| 1088 | 84 | private function executeUpserts(ClassMetadata $class, array $documents, array $options = []) |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Executes all document updates for documents of the specified type. |
||
| 1106 | * |
||
| 1107 | * @param array $documents Array of documents to update |
||
| 1108 | * @param array $options Array of options to be used with update() |
||
| 1109 | */ |
||
| 1110 | 205 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = []) |
|
| 1131 | |||
| 1132 | /** |
||
| 1133 | * Executes all document deletions for documents of the specified type. |
||
| 1134 | * |
||
| 1135 | * @param array $documents Array of documents to delete |
||
| 1136 | * @param array $options Array of options to be used with remove() |
||
| 1137 | */ |
||
| 1138 | 64 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = []) |
|
| 1174 | |||
| 1175 | /** |
||
| 1176 | * Schedules a document for insertion into the database. |
||
| 1177 | * If the document already has an identifier, it will be added to the |
||
| 1178 | * identity map. |
||
| 1179 | * |
||
| 1180 | * @param object $document The document to schedule for insertion. |
||
| 1181 | * @throws \InvalidArgumentException |
||
| 1182 | */ |
||
| 1183 | 528 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Schedules a document for upsert into the database and adds it to the |
||
| 1208 | * identity map |
||
| 1209 | * |
||
| 1210 | * @param object $document The document to schedule for upsert. |
||
| 1211 | * @throws \InvalidArgumentException |
||
| 1212 | */ |
||
| 1213 | 90 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Checks whether a document is scheduled for insertion. |
||
| 1237 | * |
||
| 1238 | * @param object $document |
||
| 1239 | * @return bool |
||
| 1240 | */ |
||
| 1241 | 89 | public function isScheduledForInsert($document) |
|
| 1245 | |||
| 1246 | /** |
||
| 1247 | * Checks whether a document is scheduled for upsert. |
||
| 1248 | * |
||
| 1249 | * @param object $document |
||
| 1250 | * @return bool |
||
| 1251 | */ |
||
| 1252 | 5 | public function isScheduledForUpsert($document) |
|
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Schedules a document for being updated. |
||
| 1259 | * |
||
| 1260 | * @param object $document The document to schedule for being updated. |
||
| 1261 | * @throws \InvalidArgumentException |
||
| 1262 | */ |
||
| 1263 | 211 | public function scheduleForUpdate($document) |
|
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1285 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1286 | * at commit time. |
||
| 1287 | * |
||
| 1288 | * @param object $document |
||
| 1289 | * @return bool |
||
| 1290 | */ |
||
| 1291 | 15 | public function isScheduledForUpdate($document) |
|
| 1295 | |||
| 1296 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1301 | |||
| 1302 | /** |
||
| 1303 | * INTERNAL: |
||
| 1304 | * Schedules a document for deletion. |
||
| 1305 | * |
||
| 1306 | * @param object $document |
||
| 1307 | */ |
||
| 1308 | 69 | public function scheduleForDelete($document) |
|
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1339 | * of work. |
||
| 1340 | * |
||
| 1341 | * @param object $document |
||
| 1342 | * @return bool |
||
| 1343 | */ |
||
| 1344 | 5 | public function isScheduledForDelete($document) |
|
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1351 | * |
||
| 1352 | * @param object $document |
||
| 1353 | * @return bool |
||
| 1354 | */ |
||
| 1355 | 227 | public function isDocumentScheduled($document) |
|
| 1363 | |||
| 1364 | /** |
||
| 1365 | * INTERNAL: |
||
| 1366 | * Registers a document in the identity map. |
||
| 1367 | * |
||
| 1368 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1369 | * the root document. Identifiers are serialized before being used as array |
||
| 1370 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1371 | * |
||
| 1372 | * @ignore |
||
| 1373 | * @param object $document The document to register. |
||
| 1374 | * @return bool TRUE if the registration was successful, FALSE if the identity of |
||
| 1375 | * the document in question is already managed. |
||
| 1376 | */ |
||
| 1377 | 626 | public function addToIdentityMap($document) |
|
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Gets the state of a document with regard to the current unit of work. |
||
| 1398 | * |
||
| 1399 | * @param object $document |
||
| 1400 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1401 | * This parameter can be set to improve performance of document state detection |
||
| 1402 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1403 | * is either known or does not matter for the caller of the method. |
||
| 1404 | * @return int The document state. |
||
| 1405 | */ |
||
| 1406 | 598 | public function getDocumentState($document, $assume = null) |
|
| 1456 | |||
| 1457 | /** |
||
| 1458 | * INTERNAL: |
||
| 1459 | * Removes a document from the identity map. This effectively detaches the |
||
| 1460 | * document from the persistence management of Doctrine. |
||
| 1461 | * |
||
| 1462 | * @ignore |
||
| 1463 | * @param object $document |
||
| 1464 | * @throws \InvalidArgumentException |
||
| 1465 | * @return bool |
||
| 1466 | */ |
||
| 1467 | 81 | public function removeFromIdentityMap($document) |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * INTERNAL: |
||
| 1490 | * Gets a document in the identity map by its identifier hash. |
||
| 1491 | * |
||
| 1492 | * @ignore |
||
| 1493 | * @param mixed $id Document identifier |
||
| 1494 | * @param ClassMetadata $class Document class |
||
| 1495 | * @return object |
||
| 1496 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1497 | */ |
||
| 1498 | 39 | public function getById($id, ClassMetadata $class) |
|
| 1508 | |||
| 1509 | /** |
||
| 1510 | * INTERNAL: |
||
| 1511 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1512 | * for the given hash, FALSE is returned. |
||
| 1513 | * |
||
| 1514 | * @ignore |
||
| 1515 | * @param mixed $id Document identifier |
||
| 1516 | * @param ClassMetadata $class Document class |
||
| 1517 | * @return mixed The found document or FALSE. |
||
| 1518 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1519 | */ |
||
| 1520 | 294 | public function tryGetById($id, ClassMetadata $class) |
|
| 1530 | |||
| 1531 | /** |
||
| 1532 | * Schedules a document for dirty-checking at commit-time. |
||
| 1533 | * |
||
| 1534 | * @param object $document The document to schedule for dirty-checking. |
||
| 1535 | * @todo Rename: scheduleForSynchronization |
||
| 1536 | */ |
||
| 1537 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1542 | |||
| 1543 | /** |
||
| 1544 | * Checks whether a document is registered in the identity map. |
||
| 1545 | * |
||
| 1546 | * @param object $document |
||
| 1547 | * @return bool |
||
| 1548 | */ |
||
| 1549 | 77 | public function isInIdentityMap($document) |
|
| 1562 | |||
| 1563 | /** |
||
| 1564 | * @param object $document |
||
| 1565 | * @return string |
||
| 1566 | */ |
||
| 1567 | 626 | private function getIdForIdentityMap($document) |
|
| 1580 | |||
| 1581 | /** |
||
| 1582 | * INTERNAL: |
||
| 1583 | * Checks whether an identifier exists in the identity map. |
||
| 1584 | * |
||
| 1585 | * @ignore |
||
| 1586 | * @param string $id |
||
| 1587 | * @param string $rootClassName |
||
| 1588 | * @return bool |
||
| 1589 | */ |
||
| 1590 | public function containsId($id, $rootClassName) |
||
| 1594 | |||
| 1595 | /** |
||
| 1596 | * Persists a document as part of the current unit of work. |
||
| 1597 | * |
||
| 1598 | * @param object $document The document to persist. |
||
| 1599 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1600 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1601 | */ |
||
| 1602 | 596 | public function persist($document) |
|
| 1611 | |||
| 1612 | /** |
||
| 1613 | * Saves a document as part of the current unit of work. |
||
| 1614 | * This method is internally called during save() cascades as it tracks |
||
| 1615 | * the already visited documents to prevent infinite recursions. |
||
| 1616 | * |
||
| 1617 | * NOTE: This method always considers documents that are not yet known to |
||
| 1618 | * this UnitOfWork as NEW. |
||
| 1619 | * |
||
| 1620 | * @param object $document The document to persist. |
||
| 1621 | * @param array $visited The already visited documents. |
||
| 1622 | * @throws \InvalidArgumentException |
||
| 1623 | * @throws MongoDBException |
||
| 1624 | */ |
||
| 1625 | 595 | private function doPersist($document, array &$visited) |
|
| 1666 | |||
| 1667 | /** |
||
| 1668 | * Deletes a document as part of the current unit of work. |
||
| 1669 | * |
||
| 1670 | * @param object $document The document to remove. |
||
| 1671 | */ |
||
| 1672 | 68 | public function remove($document) |
|
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Deletes a document as part of the current unit of work. |
||
| 1680 | * |
||
| 1681 | * This method is internally called during delete() cascades as it tracks |
||
| 1682 | * the already visited documents to prevent infinite recursions. |
||
| 1683 | * |
||
| 1684 | * @param object $document The document to delete. |
||
| 1685 | * @param array $visited The map of the already visited documents. |
||
| 1686 | * @throws MongoDBException |
||
| 1687 | */ |
||
| 1688 | 68 | private function doRemove($document, array &$visited) |
|
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1723 | * |
||
| 1724 | * @param object $document |
||
| 1725 | * @return object The managed copy of the document. |
||
| 1726 | */ |
||
| 1727 | 12 | public function merge($document) |
|
| 1733 | |||
| 1734 | /** |
||
| 1735 | * Executes a merge operation on a document. |
||
| 1736 | * |
||
| 1737 | * @param object $document |
||
| 1738 | * @param array $visited |
||
| 1739 | * @param object|null $prevManagedCopy |
||
| 1740 | * @param array|null $assoc |
||
| 1741 | * |
||
| 1742 | * @return object The managed copy of the document. |
||
| 1743 | * |
||
| 1744 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1745 | * @throws LockException If the document uses optimistic locking through a |
||
| 1746 | * version attribute and the version check against the |
||
| 1747 | * managed copy fails. |
||
| 1748 | */ |
||
| 1749 | 12 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1925 | |||
| 1926 | /** |
||
| 1927 | * Detaches a document from the persistence management. It's persistence will |
||
| 1928 | * no longer be managed by Doctrine. |
||
| 1929 | * |
||
| 1930 | * @param object $document The document to detach. |
||
| 1931 | */ |
||
| 1932 | 11 | public function detach($document) |
|
| 1937 | |||
| 1938 | /** |
||
| 1939 | * Executes a detach operation on the given document. |
||
| 1940 | * |
||
| 1941 | * @param object $document |
||
| 1942 | * @param array $visited |
||
| 1943 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1944 | */ |
||
| 1945 | 16 | private function doDetach($document, array &$visited) |
|
| 1977 | |||
| 1978 | /** |
||
| 1979 | * Refreshes the state of the given document from the database, overwriting |
||
| 1980 | * any local, unpersisted changes. |
||
| 1981 | * |
||
| 1982 | * @param object $document The document to refresh. |
||
| 1983 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 1984 | */ |
||
| 1985 | 21 | public function refresh($document) |
|
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Executes a refresh operation on a document. |
||
| 1993 | * |
||
| 1994 | * @param object $document The document to refresh. |
||
| 1995 | * @param array $visited The already visited documents during cascades. |
||
| 1996 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 1997 | */ |
||
| 1998 | 21 | private function doRefresh($document, array &$visited) |
|
| 2019 | |||
| 2020 | /** |
||
| 2021 | * Cascades a refresh operation to associated documents. |
||
| 2022 | * |
||
| 2023 | * @param object $document |
||
| 2024 | * @param array $visited |
||
| 2025 | */ |
||
| 2026 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2027 | { |
||
| 2028 | 20 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2029 | |||
| 2030 | 20 | $associationMappings = array_filter( |
|
| 2031 | 20 | $class->associationMappings, |
|
| 2032 | function ($assoc) { |
||
| 2033 | 17 | return $assoc['isCascadeRefresh']; |
|
| 2034 | 20 | } |
|
| 2035 | ); |
||
| 2036 | |||
| 2037 | 20 | foreach ($associationMappings as $mapping) { |
|
| 2038 | 15 | $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 2039 | 15 | if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) { |
|
| 2040 | 15 | if ($relatedDocuments instanceof PersistentCollectionInterface) { |
|
| 2041 | // Unwrap so that foreach() does not initialize |
||
| 2042 | 15 | $relatedDocuments = $relatedDocuments->unwrap(); |
|
| 2043 | } |
||
| 2044 | 15 | foreach ($relatedDocuments as $relatedDocument) { |
|
| 2045 | 15 | $this->doRefresh($relatedDocument, $visited); |
|
| 2046 | } |
||
| 2047 | 10 | } elseif ($relatedDocuments !== null) { |
|
| 2048 | 15 | $this->doRefresh($relatedDocuments, $visited); |
|
| 2049 | } |
||
| 2050 | } |
||
| 2051 | 20 | } |
|
| 2052 | |||
| 2053 | /** |
||
| 2054 | * Cascades a detach operation to associated documents. |
||
| 2055 | * |
||
| 2056 | * @param object $document |
||
| 2057 | * @param array $visited |
||
| 2058 | */ |
||
| 2059 | 16 | private function cascadeDetach($document, array &$visited) |
|
| 2080 | /** |
||
| 2081 | * Cascades a merge operation to associated documents. |
||
| 2082 | * |
||
| 2083 | * @param object $document |
||
| 2084 | * @param object $managedCopy |
||
| 2085 | * @param array $visited |
||
| 2086 | */ |
||
| 2087 | 12 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2088 | { |
||
| 2089 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2090 | |||
| 2091 | 12 | $associationMappings = array_filter( |
|
| 2092 | 12 | $class->associationMappings, |
|
| 2093 | function ($assoc) { |
||
| 2094 | 12 | return $assoc['isCascadeMerge']; |
|
| 2095 | 12 | } |
|
| 2096 | ); |
||
| 2097 | |||
| 2098 | 12 | foreach ($associationMappings as $assoc) { |
|
| 2099 | 11 | $relatedDocuments = $class->reflFields[$assoc['fieldName']]->getValue($document); |
|
| 2100 | |||
| 2101 | 11 | if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) { |
|
| 2102 | 8 | if ($relatedDocuments === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) { |
|
| 2103 | // Collections are the same, so there is nothing to do |
||
| 2104 | 1 | continue; |
|
| 2105 | } |
||
| 2106 | |||
| 2107 | 8 | foreach ($relatedDocuments as $relatedDocument) { |
|
| 2108 | 8 | $this->doMerge($relatedDocument, $visited, $managedCopy, $assoc); |
|
| 2109 | } |
||
| 2110 | 6 | } elseif ($relatedDocuments !== null) { |
|
| 2111 | 11 | $this->doMerge($relatedDocuments, $visited, $managedCopy, $assoc); |
|
| 2112 | } |
||
| 2113 | } |
||
| 2114 | 12 | } |
|
| 2115 | |||
| 2116 | /** |
||
| 2117 | * Cascades the save operation to associated documents. |
||
| 2118 | * |
||
| 2119 | * @param object $document |
||
| 2120 | * @param array $visited |
||
| 2121 | */ |
||
| 2122 | 593 | private function cascadePersist($document, array &$visited) |
|
| 2123 | { |
||
| 2124 | 593 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2125 | |||
| 2126 | 593 | $associationMappings = array_filter( |
|
| 2127 | 593 | $class->associationMappings, |
|
| 2128 | function ($assoc) { |
||
| 2129 | 451 | return $assoc['isCascadePersist']; |
|
| 2130 | 593 | } |
|
| 2131 | ); |
||
| 2132 | |||
| 2133 | 593 | foreach ($associationMappings as $fieldName => $mapping) { |
|
| 2134 | 409 | $relatedDocuments = $class->reflFields[$fieldName]->getValue($document); |
|
| 2135 | |||
| 2136 | 409 | if ($relatedDocuments instanceof Collection || is_array($relatedDocuments)) { |
|
| 2137 | 338 | if ($relatedDocuments instanceof PersistentCollectionInterface) { |
|
| 2138 | 12 | if ($relatedDocuments->getOwner() !== $document) { |
|
| 2139 | 2 | $relatedDocuments = $this->fixPersistentCollectionOwnership($relatedDocuments, $document, $class, $mapping['fieldName']); |
|
| 2140 | } |
||
| 2141 | // Unwrap so that foreach() does not initialize |
||
| 2142 | 12 | $relatedDocuments = $relatedDocuments->unwrap(); |
|
| 2143 | } |
||
| 2144 | |||
| 2145 | 338 | $count = 0; |
|
| 2146 | 338 | foreach ($relatedDocuments as $relatedKey => $relatedDocument) { |
|
| 2147 | 174 | if (! empty($mapping['embedded'])) { |
|
| 2148 | 103 | list(, $knownParent, ) = $this->getParentAssociation($relatedDocument); |
|
| 2149 | 103 | if ($knownParent && $knownParent !== $document) { |
|
| 2150 | 1 | $relatedDocument = clone $relatedDocument; |
|
| 2151 | 1 | $relatedDocuments[$relatedKey] = $relatedDocument; |
|
| 2152 | } |
||
| 2153 | 103 | $pathKey = CollectionHelper::isList($mapping['strategy']) ? $count++ : $relatedKey; |
|
| 2154 | 103 | $this->setParentAssociation($relatedDocument, $mapping, $document, $mapping['fieldName'] . '.' . $pathKey); |
|
| 2155 | } |
||
| 2156 | 338 | $this->doPersist($relatedDocument, $visited); |
|
| 2157 | } |
||
| 2158 | 324 | } elseif ($relatedDocuments !== null) { |
|
| 2159 | 128 | if (! empty($mapping['embedded'])) { |
|
| 2160 | 67 | list(, $knownParent, ) = $this->getParentAssociation($relatedDocuments); |
|
| 2161 | 67 | if ($knownParent && $knownParent !== $document) { |
|
| 2162 | 3 | $relatedDocuments = clone $relatedDocuments; |
|
| 2163 | 3 | $class->setFieldValue($document, $mapping['fieldName'], $relatedDocuments); |
|
| 2164 | } |
||
| 2165 | 67 | $this->setParentAssociation($relatedDocuments, $mapping, $document, $mapping['fieldName']); |
|
| 2166 | } |
||
| 2167 | 409 | $this->doPersist($relatedDocuments, $visited); |
|
| 2168 | } |
||
| 2169 | } |
||
| 2170 | 591 | } |
|
| 2171 | |||
| 2172 | /** |
||
| 2173 | * Cascades the delete operation to associated documents. |
||
| 2174 | * |
||
| 2175 | * @param object $document |
||
| 2176 | * @param array $visited |
||
| 2177 | */ |
||
| 2178 | 68 | private function cascadeRemove($document, array &$visited) |
|
| 2200 | |||
| 2201 | /** |
||
| 2202 | * Acquire a lock on the given document. |
||
| 2203 | * |
||
| 2204 | * @param object $document |
||
| 2205 | * @param int $lockMode |
||
| 2206 | * @param int $lockVersion |
||
| 2207 | * @throws LockException |
||
| 2208 | * @throws \InvalidArgumentException |
||
| 2209 | */ |
||
| 2210 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2234 | |||
| 2235 | /** |
||
| 2236 | * Releases a lock on the given document. |
||
| 2237 | * |
||
| 2238 | * @param object $document |
||
| 2239 | * @throws \InvalidArgumentException |
||
| 2240 | */ |
||
| 2241 | 1 | public function unlock($document) |
|
| 2249 | |||
| 2250 | /** |
||
| 2251 | * Clears the UnitOfWork. |
||
| 2252 | * |
||
| 2253 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2254 | */ |
||
| 2255 | 372 | public function clear($documentName = null) |
|
| 2293 | |||
| 2294 | /** |
||
| 2295 | * INTERNAL: |
||
| 2296 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2297 | * invoked on that document at the beginning of the next commit of this |
||
| 2298 | * UnitOfWork. |
||
| 2299 | * |
||
| 2300 | * @ignore |
||
| 2301 | * @param object $document |
||
| 2302 | */ |
||
| 2303 | 47 | public function scheduleOrphanRemoval($document) |
|
| 2307 | |||
| 2308 | /** |
||
| 2309 | * INTERNAL: |
||
| 2310 | * Unschedules an embedded or referenced object for removal. |
||
| 2311 | * |
||
| 2312 | * @ignore |
||
| 2313 | * @param object $document |
||
| 2314 | */ |
||
| 2315 | 100 | public function unscheduleOrphanRemoval($document) |
|
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2323 | * 1) sets owner if it was cloned |
||
| 2324 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2325 | * 3) NOP if state is OK |
||
| 2326 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2327 | * |
||
| 2328 | * @param object $document |
||
| 2329 | * @param string $propName |
||
| 2330 | * @return PersistentCollectionInterface |
||
| 2331 | */ |
||
| 2332 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2352 | |||
| 2353 | /** |
||
| 2354 | * INTERNAL: |
||
| 2355 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2356 | * |
||
| 2357 | */ |
||
| 2358 | 35 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2369 | |||
| 2370 | /** |
||
| 2371 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2372 | * |
||
| 2373 | * @return bool |
||
| 2374 | */ |
||
| 2375 | 191 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2379 | |||
| 2380 | /** |
||
| 2381 | * INTERNAL: |
||
| 2382 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2383 | * |
||
| 2384 | */ |
||
| 2385 | 203 | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2396 | |||
| 2397 | /** |
||
| 2398 | * INTERNAL: |
||
| 2399 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2400 | * |
||
| 2401 | */ |
||
| 2402 | 224 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2419 | |||
| 2420 | /** |
||
| 2421 | * INTERNAL: |
||
| 2422 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2423 | * |
||
| 2424 | */ |
||
| 2425 | 203 | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2436 | |||
| 2437 | /** |
||
| 2438 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2439 | * |
||
| 2440 | * @return bool |
||
| 2441 | */ |
||
| 2442 | 114 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2446 | |||
| 2447 | /** |
||
| 2448 | * INTERNAL: |
||
| 2449 | * Gets PersistentCollections that have been visited during computing change |
||
| 2450 | * set of $document |
||
| 2451 | * |
||
| 2452 | * @param object $document |
||
| 2453 | * @return PersistentCollectionInterface[] |
||
| 2454 | */ |
||
| 2455 | 547 | public function getVisitedCollections($document) |
|
| 2461 | |||
| 2462 | /** |
||
| 2463 | * INTERNAL: |
||
| 2464 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2465 | * |
||
| 2466 | * @param object $document |
||
| 2467 | * @return array |
||
| 2468 | */ |
||
| 2469 | 547 | public function getScheduledCollections($document) |
|
| 2475 | |||
| 2476 | /** |
||
| 2477 | * Checks whether the document is related to a PersistentCollection |
||
| 2478 | * scheduled for update or deletion. |
||
| 2479 | * |
||
| 2480 | * @param object $document |
||
| 2481 | * @return bool |
||
| 2482 | */ |
||
| 2483 | 44 | public function hasScheduledCollections($document) |
|
| 2487 | |||
| 2488 | /** |
||
| 2489 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2490 | * a collection scheduled for update or deletion. |
||
| 2491 | * |
||
| 2492 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2493 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2494 | * |
||
| 2495 | * If the collection is nested within atomic collection, it is immediately |
||
| 2496 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2497 | * calculating update data way easier. |
||
| 2498 | * |
||
| 2499 | */ |
||
| 2500 | 226 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2526 | |||
| 2527 | /** |
||
| 2528 | * Get the top-most owning document of a given document |
||
| 2529 | * |
||
| 2530 | * If a top-level document is provided, that same document will be returned. |
||
| 2531 | * For an embedded document, we will walk through parent associations until |
||
| 2532 | * we find a top-level document. |
||
| 2533 | * |
||
| 2534 | * @param object $document |
||
| 2535 | * @throws \UnexpectedValueException When a top-level document could not be found. |
||
| 2536 | * @return object |
||
| 2537 | */ |
||
| 2538 | 228 | public function getOwningDocument($document) |
|
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Gets the class name for an association (embed or reference) with respect |
||
| 2557 | * to any discriminator value. |
||
| 2558 | * |
||
| 2559 | * @param array $mapping Field mapping for the association |
||
| 2560 | * @param array|null $data Data for the embedded document or reference |
||
| 2561 | * @return string Class name. |
||
| 2562 | */ |
||
| 2563 | 218 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2593 | |||
| 2594 | /** |
||
| 2595 | * INTERNAL: |
||
| 2596 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2597 | * |
||
| 2598 | * @ignore |
||
| 2599 | * @param string $className The name of the document class. |
||
| 2600 | * @param array $data The data for the document. |
||
| 2601 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2602 | * @param object $document The document to be hydrated into in case of creation |
||
| 2603 | * @return object The document instance. |
||
| 2604 | * @internal Highly performance-sensitive method. |
||
| 2605 | */ |
||
| 2606 | 380 | public function getOrCreateDocument($className, $data, &$hints = [], $document = null) |
|
| 2679 | |||
| 2680 | /** |
||
| 2681 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2682 | * |
||
| 2683 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2684 | */ |
||
| 2685 | 164 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2690 | |||
| 2691 | /** |
||
| 2692 | * Gets the identity map of the UnitOfWork. |
||
| 2693 | * |
||
| 2694 | * @return array |
||
| 2695 | */ |
||
| 2696 | public function getIdentityMap() |
||
| 2700 | |||
| 2701 | /** |
||
| 2702 | * Gets the original data of a document. The original data is the data that was |
||
| 2703 | * present at the time the document was reconstituted from the database. |
||
| 2704 | * |
||
| 2705 | * @param object $document |
||
| 2706 | * @return array |
||
| 2707 | */ |
||
| 2708 | 1 | public function getOriginalDocumentData($document) |
|
| 2714 | |||
| 2715 | 59 | public function setOriginalDocumentData($document, array $data) |
|
| 2721 | |||
| 2722 | /** |
||
| 2723 | * INTERNAL: |
||
| 2724 | * Sets a property value of the original data array of a document. |
||
| 2725 | * |
||
| 2726 | * @ignore |
||
| 2727 | * @param string $oid |
||
| 2728 | * @param string $property |
||
| 2729 | * @param mixed $value |
||
| 2730 | */ |
||
| 2731 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2735 | |||
| 2736 | /** |
||
| 2737 | * Gets the identifier of a document. |
||
| 2738 | * |
||
| 2739 | * @param object $document |
||
| 2740 | * @return mixed The identifier value |
||
| 2741 | */ |
||
| 2742 | 411 | public function getDocumentIdentifier($document) |
|
| 2746 | |||
| 2747 | /** |
||
| 2748 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2749 | * |
||
| 2750 | * @return bool TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2751 | */ |
||
| 2752 | public function hasPendingInsertions() |
||
| 2756 | |||
| 2757 | /** |
||
| 2758 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2759 | * number of documents in the identity map. |
||
| 2760 | * |
||
| 2761 | * @return int |
||
| 2762 | */ |
||
| 2763 | 2 | public function size() |
|
| 2771 | |||
| 2772 | /** |
||
| 2773 | * INTERNAL: |
||
| 2774 | * Registers a document as managed. |
||
| 2775 | * |
||
| 2776 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2777 | * document class. If the class expects its database identifier to be an |
||
| 2778 | * ObjectId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2779 | * document identifiers map will become inconsistent with the identity map. |
||
| 2780 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2781 | * conversion and throw an exception if it's inconsistent. |
||
| 2782 | * |
||
| 2783 | * @param object $document The document. |
||
| 2784 | * @param array $id The identifier values. |
||
| 2785 | * @param array $data The original document data. |
||
| 2786 | */ |
||
| 2787 | 363 | public function registerManaged($document, $id, $data) |
|
| 2802 | |||
| 2803 | /** |
||
| 2804 | * INTERNAL: |
||
| 2805 | * Clears the property changeset of the document with the given OID. |
||
| 2806 | * |
||
| 2807 | * @param string $oid The document's OID. |
||
| 2808 | */ |
||
| 2809 | public function clearDocumentChangeSet($oid) |
||
| 2813 | |||
| 2814 | /* PropertyChangedListener implementation */ |
||
| 2815 | |||
| 2816 | /** |
||
| 2817 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2818 | * |
||
| 2819 | * @param object $document The document that owns the property. |
||
| 2820 | * @param string $propertyName The name of the property that changed. |
||
| 2821 | * @param mixed $oldValue The old value of the property. |
||
| 2822 | * @param mixed $newValue The new value of the property. |
||
| 2823 | */ |
||
| 2824 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2841 | |||
| 2842 | /** |
||
| 2843 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2844 | * |
||
| 2845 | * @return array |
||
| 2846 | */ |
||
| 2847 | 2 | public function getScheduledDocumentInsertions() |
|
| 2851 | |||
| 2852 | /** |
||
| 2853 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2854 | * |
||
| 2855 | * @return array |
||
| 2856 | */ |
||
| 2857 | 1 | public function getScheduledDocumentUpserts() |
|
| 2861 | |||
| 2862 | /** |
||
| 2863 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2864 | * |
||
| 2865 | * @return array |
||
| 2866 | */ |
||
| 2867 | 1 | public function getScheduledDocumentUpdates() |
|
| 2871 | |||
| 2872 | /** |
||
| 2873 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2874 | * |
||
| 2875 | * @return array |
||
| 2876 | */ |
||
| 2877 | public function getScheduledDocumentDeletions() |
||
| 2881 | |||
| 2882 | /** |
||
| 2883 | * Get the currently scheduled complete collection deletions |
||
| 2884 | * |
||
| 2885 | * @return array |
||
| 2886 | */ |
||
| 2887 | public function getScheduledCollectionDeletions() |
||
| 2891 | |||
| 2892 | /** |
||
| 2893 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2894 | * |
||
| 2895 | * @return array |
||
| 2896 | */ |
||
| 2897 | public function getScheduledCollectionUpdates() |
||
| 2901 | |||
| 2902 | /** |
||
| 2903 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2904 | * |
||
| 2905 | * @param object $obj |
||
| 2906 | */ |
||
| 2907 | public function initializeObject($obj) |
||
| 2915 | |||
| 2916 | private function objToStr($obj) |
||
| 2920 | } |
||
| 2921 |
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.