Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 27 | class UnitOfWork implements PropertyChangedListener |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 31 | */ |
||
| 32 | const STATE_MANAGED = 1; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 36 | * and is not (yet) managed by a DocumentManager. |
||
| 37 | */ |
||
| 38 | const STATE_NEW = 2; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * A detached document is an instance with a persistent identity that is not |
||
| 42 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 43 | */ |
||
| 44 | const STATE_DETACHED = 3; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A removed document instance is an instance with a persistent identity, |
||
| 48 | * associated with a DocumentManager, whose persistent state has been |
||
| 49 | * deleted (or is scheduled for deletion). |
||
| 50 | */ |
||
| 51 | const STATE_REMOVED = 4; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The identity map holds references to all managed documents. |
||
| 55 | * |
||
| 56 | * Documents are grouped by their class name, and then indexed by the |
||
| 57 | * serialized string of their database identifier field or, if the class |
||
| 58 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 59 | * differentiation of values that may be equal (via type juggling) but not |
||
| 60 | * identical. |
||
| 61 | * |
||
| 62 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 63 | * we always take the root class name of the hierarchy. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $identityMap = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Map of all identifiers of managed documents. |
||
| 71 | * Keys are object ids (spl_object_hash). |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $documentIdentifiers = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Map of the original document data of managed documents. |
||
| 79 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 80 | * at commit time. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 84 | * A value will only really be copied if the value in the document is modified |
||
| 85 | * by the user. |
||
| 86 | */ |
||
| 87 | private $originalDocumentData = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 91 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $documentChangeSets = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The (cached) states of any known documents. |
||
| 99 | * Keys are object ids (spl_object_hash). |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | private $documentStates = array(); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 107 | * |
||
| 108 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 109 | * object hash. This is only used for documents with a change tracking |
||
| 110 | * policy of DEFERRED_EXPLICIT. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | * @todo rename: scheduledForSynchronization |
||
| 114 | */ |
||
| 115 | private $scheduledForDirtyCheck = array(); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * A list of all pending document insertions. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $documentInsertions = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * A list of all pending document updates. |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private $documentUpdates = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * A list of all pending document upserts. |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private $documentUpserts = array(); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * A list of all pending document deletions. |
||
| 140 | * |
||
| 141 | * @var array |
||
| 142 | */ |
||
| 143 | private $documentDeletions = array(); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * All pending collection deletions. |
||
| 147 | * |
||
| 148 | * @var array |
||
| 149 | */ |
||
| 150 | private $collectionDeletions = array(); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * All pending collection updates. |
||
| 154 | * |
||
| 155 | * @var array |
||
| 156 | */ |
||
| 157 | private $collectionUpdates = array(); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * A list of documents related to collections scheduled for update or deletion |
||
| 161 | * |
||
| 162 | * @var array |
||
| 163 | */ |
||
| 164 | private $hasScheduledCollections = array(); |
||
| 165 | |||
| 166 | /** |
||
| 167 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 168 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 169 | * of their data. |
||
| 170 | * |
||
| 171 | * @var array |
||
| 172 | */ |
||
| 173 | private $visitedCollections = array(); |
||
| 174 | |||
| 175 | /** |
||
| 176 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 177 | * |
||
| 178 | * @var DocumentManager |
||
| 179 | */ |
||
| 180 | private $dm; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * The EventManager used for dispatching events. |
||
| 184 | * |
||
| 185 | * @var EventManager |
||
| 186 | */ |
||
| 187 | private $evm; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Additional documents that are scheduled for removal. |
||
| 191 | * |
||
| 192 | * @var array |
||
| 193 | */ |
||
| 194 | private $orphanRemovals = array(); |
||
| 195 | |||
| 196 | /** |
||
| 197 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 198 | * |
||
| 199 | * @var HydratorFactory |
||
| 200 | */ |
||
| 201 | private $hydratorFactory; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * The document persister instances used to persist document instances. |
||
| 205 | * |
||
| 206 | * @var array |
||
| 207 | */ |
||
| 208 | private $persisters = array(); |
||
| 209 | |||
| 210 | /** |
||
| 211 | * The collection persister instance used to persist changes to collections. |
||
| 212 | * |
||
| 213 | * @var Persisters\CollectionPersister |
||
| 214 | */ |
||
| 215 | private $collectionPersister; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * The persistence builder instance used in DocumentPersisters. |
||
| 219 | * |
||
| 220 | * @var PersistenceBuilder |
||
| 221 | */ |
||
| 222 | private $persistenceBuilder; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Array of parent associations between embedded documents. |
||
| 226 | * |
||
| 227 | * @var array |
||
| 228 | */ |
||
| 229 | private $parentAssociations = array(); |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @var LifecycleEventManager |
||
| 233 | */ |
||
| 234 | private $lifecycleEventManager; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 238 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 239 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 240 | * |
||
| 241 | * @var array |
||
| 242 | */ |
||
| 243 | private $embeddedDocumentsRegistry = array(); |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @var int |
||
| 247 | */ |
||
| 248 | private $commitsInProgress = 0; |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 252 | * |
||
| 253 | * @param DocumentManager $dm |
||
| 254 | * @param EventManager $evm |
||
| 255 | * @param HydratorFactory $hydratorFactory |
||
| 256 | */ |
||
| 257 | 1627 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 258 | { |
||
| 259 | 1627 | $this->dm = $dm; |
|
| 260 | 1627 | $this->evm = $evm; |
|
| 261 | 1627 | $this->hydratorFactory = $hydratorFactory; |
|
| 262 | 1627 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 263 | 1627 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 267 | * queries for insert persistence. |
||
| 268 | * |
||
| 269 | * @return PersistenceBuilder $pb |
||
| 270 | */ |
||
| 271 | 1086 | public function getPersistenceBuilder() |
|
| 272 | { |
||
| 273 | 1086 | if ( ! $this->persistenceBuilder) { |
|
| 274 | 1086 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 275 | } |
||
| 276 | 1086 | return $this->persistenceBuilder; |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Sets the parent association for a given embedded document. |
||
| 281 | * |
||
| 282 | * @param object $document |
||
| 283 | * @param array $mapping |
||
| 284 | * @param object $parent |
||
| 285 | * @param string $propertyPath |
||
| 286 | */ |
||
| 287 | 177 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 288 | { |
||
| 289 | 177 | $oid = spl_object_hash($document); |
|
| 290 | 177 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 291 | 177 | $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath); |
|
| 292 | 177 | } |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets the parent association for a given embedded document. |
||
| 296 | * |
||
| 297 | * <code> |
||
| 298 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 299 | * </code> |
||
| 300 | * |
||
| 301 | * @param object $document |
||
| 302 | * @return array $association |
||
| 303 | */ |
||
| 304 | 203 | public function getParentAssociation($document) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Get the document persister instance for the given document name |
||
| 315 | * |
||
| 316 | * @param string $documentName |
||
| 317 | * @return Persisters\DocumentPersister |
||
| 318 | */ |
||
| 319 | 1084 | public function getDocumentPersister($documentName) |
|
| 320 | { |
||
| 321 | 1084 | if ( ! isset($this->persisters[$documentName])) { |
|
| 322 | 1071 | $class = $this->dm->getClassMetadata($documentName); |
|
| 323 | 1071 | $pb = $this->getPersistenceBuilder(); |
|
| 324 | 1071 | $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this->evm, $this, $this->hydratorFactory, $class); |
|
| 325 | } |
||
| 326 | 1084 | return $this->persisters[$documentName]; |
|
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the collection persister instance. |
||
| 331 | * |
||
| 332 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 333 | */ |
||
| 334 | 1084 | public function getCollectionPersister() |
|
| 335 | { |
||
| 336 | 1084 | if ( ! isset($this->collectionPersister)) { |
|
| 337 | 1084 | $pb = $this->getPersistenceBuilder(); |
|
| 338 | 1084 | $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this); |
|
| 339 | } |
||
| 340 | 1084 | return $this->collectionPersister; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set the document persister instance to use for the given document name |
||
| 345 | * |
||
| 346 | * @param string $documentName |
||
| 347 | * @param Persisters\DocumentPersister $persister |
||
| 348 | */ |
||
| 349 | 13 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 356 | * up to this point. The state of all managed documents will be synchronized with |
||
| 357 | * the database. |
||
| 358 | * |
||
| 359 | * The operations are executed in the following order: |
||
| 360 | * |
||
| 361 | * 1) All document insertions |
||
| 362 | * 2) All document updates |
||
| 363 | * 3) All document deletions |
||
| 364 | * |
||
| 365 | * @param object $document |
||
| 366 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 367 | */ |
||
| 368 | 577 | public function commit($document = null, array $options = array()) |
|
| 369 | { |
||
| 370 | // Raise preFlush |
||
| 371 | 577 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 372 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 373 | } |
||
| 374 | |||
| 375 | // Compute changes done since last commit. |
||
| 376 | 577 | if ($document === null) { |
|
| 377 | 572 | $this->computeChangeSets(); |
|
| 378 | 13 | } elseif (is_object($document)) { |
|
| 379 | 12 | $this->computeSingleDocumentChangeSet($document); |
|
| 380 | 1 | } elseif (is_array($document)) { |
|
| 381 | 1 | foreach ($document as $object) { |
|
| 382 | 1 | $this->computeSingleDocumentChangeSet($object); |
|
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | 575 | if ( ! ($this->documentInsertions || |
|
|
|
|||
| 387 | 245 | $this->documentUpserts || |
|
| 388 | 202 | $this->documentDeletions || |
|
| 389 | 188 | $this->documentUpdates || |
|
| 390 | 22 | $this->collectionUpdates || |
|
| 391 | 22 | $this->collectionDeletions || |
|
| 392 | 575 | $this->orphanRemovals) |
|
| 393 | ) { |
||
| 394 | 22 | return; // Nothing to do. |
|
| 395 | } |
||
| 396 | |||
| 397 | 572 | $this->commitsInProgress++; |
|
| 398 | 572 | if ($this->commitsInProgress > 1) { |
|
| 399 | @trigger_error('There is already a commit operation in progress. Calling flush in an event subscriber is deprecated and will be forbidden in 2.0.', E_USER_DEPRECATED); |
||
| 400 | } |
||
| 401 | try { |
||
| 402 | 572 | if ($this->orphanRemovals) { |
|
| 403 | 44 | foreach ($this->orphanRemovals as $removal) { |
|
| 404 | 44 | $this->remove($removal); |
|
| 405 | } |
||
| 406 | } |
||
| 407 | |||
| 408 | // Raise onFlush |
||
| 409 | 572 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 410 | 4 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 411 | } |
||
| 412 | |||
| 413 | 571 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 414 | 86 | list($class, $documents) = $classAndDocuments; |
|
| 415 | 86 | $this->executeUpserts($class, $documents, $options); |
|
| 416 | } |
||
| 417 | |||
| 418 | 571 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 419 | 496 | list($class, $documents) = $classAndDocuments; |
|
| 420 | 496 | $this->executeInserts($class, $documents, $options); |
|
| 421 | } |
||
| 422 | |||
| 423 | 570 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 424 | 208 | list($class, $documents) = $classAndDocuments; |
|
| 425 | 208 | $this->executeUpdates($class, $documents, $options); |
|
| 426 | } |
||
| 427 | |||
| 428 | 570 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 429 | 65 | list($class, $documents) = $classAndDocuments; |
|
| 430 | 65 | $this->executeDeletions($class, $documents, $options); |
|
| 431 | } |
||
| 432 | |||
| 433 | // Raise postFlush |
||
| 434 | 570 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 435 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 436 | } |
||
| 437 | |||
| 438 | // Clear up |
||
| 439 | 570 | $this->documentInsertions = |
|
| 440 | 570 | $this->documentUpserts = |
|
| 441 | 570 | $this->documentUpdates = |
|
| 442 | 570 | $this->documentDeletions = |
|
| 443 | 570 | $this->documentChangeSets = |
|
| 444 | 570 | $this->collectionUpdates = |
|
| 445 | 570 | $this->collectionDeletions = |
|
| 446 | 570 | $this->visitedCollections = |
|
| 447 | 570 | $this->scheduledForDirtyCheck = |
|
| 448 | 570 | $this->orphanRemovals = |
|
| 449 | 570 | $this->hasScheduledCollections = array(); |
|
| 450 | 570 | } finally { |
|
| 451 | 572 | $this->commitsInProgress--; |
|
| 452 | } |
||
| 453 | 570 | } |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Groups a list of scheduled documents by their class. |
||
| 457 | * |
||
| 458 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 459 | * @param bool $includeEmbedded |
||
| 460 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 461 | */ |
||
| 462 | 571 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 463 | { |
||
| 464 | 571 | if (empty($documents)) { |
|
| 465 | 571 | return array(); |
|
| 466 | } |
||
| 467 | 570 | $divided = array(); |
|
| 468 | 570 | $embeds = array(); |
|
| 469 | 570 | foreach ($documents as $oid => $d) { |
|
| 470 | 570 | $className = get_class($d); |
|
| 471 | 570 | if (isset($embeds[$className])) { |
|
| 472 | 69 | continue; |
|
| 473 | } |
||
| 474 | 570 | if (isset($divided[$className])) { |
|
| 475 | 158 | $divided[$className][1][$oid] = $d; |
|
| 476 | 158 | continue; |
|
| 477 | } |
||
| 478 | 570 | $class = $this->dm->getClassMetadata($className); |
|
| 479 | 570 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 480 | 154 | $embeds[$className] = true; |
|
| 481 | 154 | continue; |
|
| 482 | } |
||
| 483 | 570 | if (empty($divided[$class->name])) { |
|
| 484 | 570 | $divided[$class->name] = array($class, array($oid => $d)); |
|
| 485 | } else { |
||
| 486 | 570 | $divided[$class->name][1][$oid] = $d; |
|
| 487 | } |
||
| 488 | } |
||
| 489 | 570 | return $divided; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Compute changesets of all documents scheduled for insertion. |
||
| 494 | * |
||
| 495 | * Embedded documents will not be processed. |
||
| 496 | */ |
||
| 497 | 579 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 498 | { |
||
| 499 | 579 | foreach ($this->documentInsertions as $document) { |
|
| 500 | 507 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 501 | 507 | if ( ! $class->isEmbeddedDocument) { |
|
| 502 | 507 | $this->computeChangeSet($class, $document); |
|
| 503 | } |
||
| 504 | } |
||
| 505 | 578 | } |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Compute changesets of all documents scheduled for upsert. |
||
| 509 | * |
||
| 510 | * Embedded documents will not be processed. |
||
| 511 | */ |
||
| 512 | 578 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 513 | { |
||
| 514 | 578 | foreach ($this->documentUpserts as $document) { |
|
| 515 | 85 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 516 | 85 | if ( ! $class->isEmbeddedDocument) { |
|
| 517 | 85 | $this->computeChangeSet($class, $document); |
|
| 518 | } |
||
| 519 | } |
||
| 520 | 578 | } |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 524 | * |
||
| 525 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 526 | * 2. Proxies are skipped. |
||
| 527 | * 3. Only if document is properly managed. |
||
| 528 | * |
||
| 529 | * @param object $document |
||
| 530 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 531 | * @return void |
||
| 532 | */ |
||
| 533 | 13 | private function computeSingleDocumentChangeSet($document) |
|
| 534 | { |
||
| 535 | 13 | $state = $this->getDocumentState($document); |
|
| 536 | |||
| 537 | 13 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
|
| 538 | 1 | throw new \InvalidArgumentException('Document has to be managed or scheduled for removal for single computation ' . $this->objToStr($document)); |
|
| 539 | } |
||
| 540 | |||
| 541 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 542 | |||
| 543 | 12 | if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
|
| 544 | 10 | $this->persist($document); |
|
| 545 | } |
||
| 546 | |||
| 547 | // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case. |
||
| 548 | 12 | $this->computeScheduleInsertsChangeSets(); |
|
| 549 | 12 | $this->computeScheduleUpsertsChangeSets(); |
|
| 550 | |||
| 551 | // Ignore uninitialized proxy objects |
||
| 552 | 12 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 553 | return; |
||
| 554 | } |
||
| 555 | |||
| 556 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 557 | 12 | $oid = spl_object_hash($document); |
|
| 558 | |||
| 559 | 12 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 560 | 12 | && ! isset($this->documentUpserts[$oid]) |
|
| 561 | 12 | && ! isset($this->documentDeletions[$oid]) |
|
| 562 | 12 | && isset($this->documentStates[$oid]) |
|
| 563 | ) { |
||
| 564 | 7 | $this->computeChangeSet($class, $document); |
|
| 565 | } |
||
| 566 | 12 | } |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Gets the changeset for a document. |
||
| 570 | * |
||
| 571 | * @param object $document |
||
| 572 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 573 | */ |
||
| 574 | 573 | public function getDocumentChangeSet($document) |
|
| 575 | { |
||
| 576 | 573 | $oid = spl_object_hash($document); |
|
| 577 | 573 | if (isset($this->documentChangeSets[$oid])) { |
|
| 578 | 569 | return $this->documentChangeSets[$oid]; |
|
| 579 | } |
||
| 580 | 55 | return array(); |
|
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * INTERNAL: |
||
| 585 | * Sets the changeset for a document. |
||
| 586 | * |
||
| 587 | * @param object $document |
||
| 588 | * @param array $changeset |
||
| 589 | */ |
||
| 590 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 591 | { |
||
| 592 | 1 | $this->documentChangeSets[spl_object_hash($document)] = $changeset; |
|
| 593 | 1 | } |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 597 | * |
||
| 598 | * @param object $document |
||
| 599 | * @return array |
||
| 600 | */ |
||
| 601 | 580 | public function getDocumentActualData($document) |
|
| 602 | { |
||
| 603 | 580 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 604 | 580 | $actualData = array(); |
|
| 605 | 580 | foreach ($class->reflFields as $name => $refProp) { |
|
| 606 | 580 | $mapping = $class->fieldMappings[$name]; |
|
| 607 | // skip not saved fields |
||
| 608 | 580 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 609 | 27 | continue; |
|
| 610 | } |
||
| 611 | 580 | $value = $refProp->getValue($document); |
|
| 612 | 580 | if ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 613 | 580 | && $value !== null && ! ($value instanceof PersistentCollectionInterface)) { |
|
| 614 | // If $actualData[$name] is not a Collection then use an ArrayCollection. |
||
| 615 | 371 | if ( ! $value instanceof Collection) { |
|
| 616 | 139 | $value = new ArrayCollection($value); |
|
| 617 | } |
||
| 618 | |||
| 619 | // Inject PersistentCollection |
||
| 620 | 371 | $coll = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $mapping, $value); |
|
| 621 | 371 | $coll->setOwner($document, $mapping); |
|
| 622 | 371 | $coll->setDirty( ! $value->isEmpty()); |
|
| 623 | 371 | $class->reflFields[$name]->setValue($document, $coll); |
|
| 624 | 371 | $actualData[$name] = $coll; |
|
| 625 | } else { |
||
| 626 | 580 | $actualData[$name] = $value; |
|
| 627 | } |
||
| 628 | } |
||
| 629 | 580 | return $actualData; |
|
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Computes the changes that happened to a single document. |
||
| 634 | * |
||
| 635 | * Modifies/populates the following properties: |
||
| 636 | * |
||
| 637 | * {@link originalDocumentData} |
||
| 638 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 639 | * then it was not fetched from the database and therefore we have no original |
||
| 640 | * document data yet. All of the current document data is stored as the original document data. |
||
| 641 | * |
||
| 642 | * {@link documentChangeSets} |
||
| 643 | * The changes detected on all properties of the document are stored there. |
||
| 644 | * A change is a tuple array where the first entry is the old value and the second |
||
| 645 | * entry is the new value of the property. Changesets are used by persisters |
||
| 646 | * to INSERT/UPDATE the persistent document state. |
||
| 647 | * |
||
| 648 | * {@link documentUpdates} |
||
| 649 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 650 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 651 | * there to mark it for an update. |
||
| 652 | * |
||
| 653 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 654 | * @param object $document The document for which to compute the changes. |
||
| 655 | */ |
||
| 656 | 576 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 672 | * |
||
| 673 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 674 | * @param object $document |
||
| 675 | * @param boolean $recompute |
||
| 676 | */ |
||
| 677 | 576 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 678 | { |
||
| 679 | 576 | $oid = spl_object_hash($document); |
|
| 680 | 576 | $actualData = $this->getDocumentActualData($document); |
|
| 681 | 576 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 682 | 576 | if ($isNewDocument) { |
|
| 683 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 684 | // These result in an INSERT. |
||
| 685 | 576 | $this->originalDocumentData[$oid] = $actualData; |
|
| 686 | 576 | $changeSet = array(); |
|
| 687 | 576 | foreach ($actualData as $propName => $actualValue) { |
|
| 688 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 689 | * was cloned and its ownership must be fixed |
||
| 690 | */ |
||
| 691 | 576 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 692 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 693 | $actualValue = $actualData[$propName]; |
||
| 694 | } |
||
| 695 | // ignore inverse side of reference relationship |
||
| 696 | 576 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 697 | 184 | continue; |
|
| 698 | } |
||
| 699 | 576 | $changeSet[$propName] = array(null, $actualValue); |
|
| 700 | } |
||
| 701 | 576 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 702 | } else { |
||
| 703 | 264 | if ($class->isReadOnly) { |
|
| 704 | 2 | return; |
|
| 705 | } |
||
| 706 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 707 | // and we have a copy of the original data |
||
| 708 | 262 | $originalData = $this->originalDocumentData[$oid]; |
|
| 709 | 262 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 710 | 262 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 711 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 712 | } else { |
||
| 713 | 262 | $changeSet = array(); |
|
| 714 | } |
||
| 715 | |||
| 716 | 262 | foreach ($actualData as $propName => $actualValue) { |
|
| 717 | // skip not saved fields |
||
| 718 | 262 | if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) { |
|
| 719 | continue; |
||
| 720 | } |
||
| 721 | |||
| 722 | 262 | $orgValue = $originalData[$propName] ?? null; |
|
| 723 | |||
| 724 | // skip if value has not changed |
||
| 725 | 262 | if ($orgValue === $actualValue) { |
|
| 726 | 261 | if (!$actualValue instanceof PersistentCollectionInterface) { |
|
| 727 | 261 | continue; |
|
| 728 | } |
||
| 729 | |||
| 730 | 181 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
|
| 731 | // consider dirty collections as changed as well |
||
| 732 | 157 | continue; |
|
| 733 | } |
||
| 734 | } |
||
| 735 | |||
| 736 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 737 | 224 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 738 | 13 | if ($orgValue !== null) { |
|
| 739 | 8 | $this->scheduleOrphanRemoval($orgValue); |
|
| 740 | } |
||
| 741 | 13 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 742 | 13 | continue; |
|
| 743 | } |
||
| 744 | |||
| 745 | // if owning side of reference-one relationship |
||
| 746 | 218 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 747 | 13 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 748 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 749 | } |
||
| 750 | |||
| 751 | 13 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 752 | 13 | continue; |
|
| 753 | } |
||
| 754 | |||
| 755 | 211 | if ($isChangeTrackingNotify) { |
|
| 756 | 3 | continue; |
|
| 757 | } |
||
| 758 | |||
| 759 | // ignore inverse side of reference relationship |
||
| 760 | 209 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 761 | 6 | continue; |
|
| 762 | } |
||
| 763 | |||
| 764 | // Persistent collection was exchanged with the "originally" |
||
| 765 | // created one. This can only mean it was cloned and replaced |
||
| 766 | // on another document. |
||
| 767 | 207 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 768 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 769 | } |
||
| 770 | |||
| 771 | // if embed-many or reference-many relationship |
||
| 772 | 207 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 773 | 100 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 774 | /* If original collection was exchanged with a non-empty value |
||
| 775 | * and $set will be issued, there is no need to $unset it first |
||
| 776 | */ |
||
| 777 | 100 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 778 | 19 | continue; |
|
| 779 | } |
||
| 780 | 87 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
|
| 781 | 15 | $this->scheduleCollectionDeletion($orgValue); |
|
| 782 | } |
||
| 783 | 87 | continue; |
|
| 784 | } |
||
| 785 | |||
| 786 | // skip equivalent date values |
||
| 787 | 136 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 788 | 37 | $dateType = Type::getType('date'); |
|
| 789 | 37 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 790 | 37 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 791 | |||
| 792 | 37 | if ($dbOrgValue instanceof \MongoDB\BSON\UTCDateTime && $dbActualValue instanceof \MongoDB\BSON\UTCDateTime && $dbOrgValue == $dbActualValue) { |
|
| 793 | 30 | continue; |
|
| 794 | } |
||
| 795 | } |
||
| 796 | |||
| 797 | // regular field |
||
| 798 | 119 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 799 | } |
||
| 800 | 262 | if ($changeSet) { |
|
| 801 | 213 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 802 | 16 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 803 | 211 | : $changeSet; |
|
| 804 | |||
| 805 | 213 | $this->originalDocumentData[$oid] = $actualData; |
|
| 806 | 213 | $this->scheduleForUpdate($document); |
|
| 807 | } |
||
| 808 | } |
||
| 809 | |||
| 810 | // Look for changes in associations of the document |
||
| 811 | 576 | $associationMappings = array_filter( |
|
| 812 | 576 | $class->associationMappings, |
|
| 813 | function ($assoc) { return empty($assoc['notSaved']); } |
||
| 814 | ); |
||
| 815 | |||
| 816 | 576 | foreach ($associationMappings as $mapping) { |
|
| 817 | 437 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 818 | |||
| 819 | 437 | if ($value === null) { |
|
| 820 | 300 | continue; |
|
| 821 | } |
||
| 822 | |||
| 823 | 424 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 824 | |||
| 825 | 423 | if (isset($mapping['reference'])) { |
|
| 826 | 320 | continue; |
|
| 827 | } |
||
| 828 | |||
| 829 | 322 | $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value->unwrap(); |
|
| 830 | |||
| 831 | 322 | foreach ($values as $obj) { |
|
| 832 | 158 | $oid2 = spl_object_hash($obj); |
|
| 833 | |||
| 834 | 158 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 835 | 156 | if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) { |
|
| 836 | // instance of $value is the same as it was previously otherwise there would be |
||
| 837 | // change set already in place |
||
| 838 | 34 | $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value); |
|
| 839 | } |
||
| 840 | |||
| 841 | 156 | if ( ! $isNewDocument) { |
|
| 842 | 65 | $this->scheduleForUpdate($document); |
|
| 843 | } |
||
| 844 | |||
| 845 | 322 | break; |
|
| 846 | } |
||
| 847 | } |
||
| 848 | } |
||
| 849 | 575 | } |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Computes all the changes that have been done to documents and collections |
||
| 853 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 854 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 855 | */ |
||
| 856 | 575 | public function computeChangeSets() |
|
| 857 | { |
||
| 858 | 575 | $this->computeScheduleInsertsChangeSets(); |
|
| 859 | 574 | $this->computeScheduleUpsertsChangeSets(); |
|
| 860 | |||
| 861 | // Compute changes for other MANAGED documents. Change tracking policies take effect here. |
||
| 862 | 574 | foreach ($this->identityMap as $className => $documents) { |
|
| 863 | 574 | $class = $this->dm->getClassMetadata($className); |
|
| 864 | 574 | if ($class->isEmbeddedDocument) { |
|
| 865 | /* we do not want to compute changes to embedded documents up front |
||
| 866 | * in case embedded document was replaced and its changeset |
||
| 867 | * would corrupt data. Embedded documents' change set will |
||
| 868 | * be calculated by reachability from owning document. |
||
| 869 | */ |
||
| 870 | 149 | continue; |
|
| 871 | } |
||
| 872 | |||
| 873 | // If change tracking is explicit or happens through notification, then only compute |
||
| 874 | // changes on document of that type that are explicitly marked for synchronization. |
||
| 875 | switch (true) { |
||
| 876 | 574 | case ($class->isChangeTrackingDeferredImplicit()): |
|
| 877 | 573 | $documentsToProcess = $documents; |
|
| 878 | 573 | break; |
|
| 879 | |||
| 880 | 4 | case (isset($this->scheduledForDirtyCheck[$className])): |
|
| 881 | 3 | $documentsToProcess = $this->scheduledForDirtyCheck[$className]; |
|
| 882 | 3 | break; |
|
| 883 | |||
| 884 | default: |
||
| 885 | 4 | $documentsToProcess = array(); |
|
| 886 | |||
| 887 | } |
||
| 888 | |||
| 889 | 574 | foreach ($documentsToProcess as $document) { |
|
| 890 | // Ignore uninitialized proxy objects |
||
| 891 | 570 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 892 | 9 | continue; |
|
| 893 | } |
||
| 894 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 895 | 570 | $oid = spl_object_hash($document); |
|
| 896 | 570 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 897 | 570 | && ! isset($this->documentUpserts[$oid]) |
|
| 898 | 570 | && ! isset($this->documentDeletions[$oid]) |
|
| 899 | 570 | && isset($this->documentStates[$oid]) |
|
| 900 | ) { |
||
| 901 | 574 | $this->computeChangeSet($class, $document); |
|
| 902 | } |
||
| 903 | } |
||
| 904 | } |
||
| 905 | 574 | } |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Computes the changes of an association. |
||
| 909 | * |
||
| 910 | * @param object $parentDocument |
||
| 911 | * @param array $assoc |
||
| 912 | * @param mixed $value The value of the association. |
||
| 913 | * @throws \InvalidArgumentException |
||
| 914 | */ |
||
| 915 | 424 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 916 | { |
||
| 917 | 424 | $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]); |
|
| 918 | 424 | $class = $this->dm->getClassMetadata(get_class($parentDocument)); |
|
| 919 | 424 | $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); |
|
| 920 | |||
| 921 | 424 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
| 922 | 7 | return; |
|
| 923 | } |
||
| 924 | |||
| 925 | 423 | if ($value instanceof PersistentCollectionInterface && $value->isDirty() && ($assoc['isOwningSide'] || isset($assoc['embedded']))) { |
|
| 926 | 227 | if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) { |
|
| 927 | 223 | $this->scheduleCollectionUpdate($value); |
|
| 928 | } |
||
| 929 | 227 | $topmostOwner = $this->getOwningDocument($value->getOwner()); |
|
| 930 | 227 | $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value; |
|
| 931 | 227 | if ( ! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) { |
|
| 932 | 122 | $value->initialize(); |
|
| 933 | 122 | foreach ($value->getDeletedDocuments() as $orphan) { |
|
| 934 | 20 | $this->scheduleOrphanRemoval($orphan); |
|
| 935 | } |
||
| 936 | } |
||
| 937 | } |
||
| 938 | |||
| 939 | // Look through the documents, and in any of their associations, |
||
| 940 | // for transient (new) documents, recursively. ("Persistence by reachability") |
||
| 941 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 942 | 423 | $unwrappedValue = ($assoc['type'] === ClassMetadata::ONE) ? array($value) : $value->unwrap(); |
|
| 943 | |||
| 944 | 423 | $count = 0; |
|
| 945 | 423 | foreach ($unwrappedValue as $key => $entry) { |
|
| 946 | 336 | if ( ! is_object($entry)) { |
|
| 947 | 1 | throw new \InvalidArgumentException( |
|
| 948 | 1 | sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name']) |
|
| 949 | ); |
||
| 950 | } |
||
| 951 | |||
| 952 | 335 | $targetClass = $this->dm->getClassMetadata(get_class($entry)); |
|
| 953 | |||
| 954 | 335 | $state = $this->getDocumentState($entry, self::STATE_NEW); |
|
| 955 | |||
| 956 | // Handle "set" strategy for multi-level hierarchy |
||
| 957 | 335 | $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key; |
|
| 958 | 335 | $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name']; |
|
| 959 | |||
| 960 | 335 | $count++; |
|
| 961 | |||
| 962 | switch ($state) { |
||
| 963 | 335 | case self::STATE_NEW: |
|
| 964 | 53 | if ( ! $assoc['isCascadePersist']) { |
|
| 965 | throw new \InvalidArgumentException('A new document was found through a relationship that was not' |
||
| 966 | . ' configured to cascade persist operations: ' . $this->objToStr($entry) . '.' |
||
| 967 | . ' Explicitly persist the new document or configure cascading persist operations' |
||
| 968 | . ' on the relationship.'); |
||
| 969 | } |
||
| 970 | |||
| 971 | 53 | $this->persistNew($targetClass, $entry); |
|
| 972 | 53 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 973 | 53 | $this->computeChangeSet($targetClass, $entry); |
|
| 974 | 53 | break; |
|
| 975 | |||
| 976 | 331 | case self::STATE_MANAGED: |
|
| 977 | 331 | if ($targetClass->isEmbeddedDocument) { |
|
| 978 | 150 | list(, $knownParent, ) = $this->getParentAssociation($entry); |
|
| 979 | 150 | if ($knownParent && $knownParent !== $parentDocument) { |
|
| 980 | 6 | $entry = clone $entry; |
|
| 981 | 6 | if ($assoc['type'] === ClassMetadata::ONE) { |
|
| 982 | 3 | $class->setFieldValue($parentDocument, $assoc['fieldName'], $entry); |
|
| 983 | 3 | $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['fieldName'], $entry); |
|
| 984 | 3 | $poid = spl_object_hash($parentDocument); |
|
| 985 | 3 | if (isset($this->documentChangeSets[$poid][$assoc['fieldName']])) { |
|
| 986 | 3 | $this->documentChangeSets[$poid][$assoc['fieldName']][1] = $entry; |
|
| 987 | } |
||
| 988 | } else { |
||
| 989 | // must use unwrapped value to not trigger orphan removal |
||
| 990 | 4 | $unwrappedValue[$key] = $entry; |
|
| 991 | } |
||
| 992 | 6 | $this->persistNew($targetClass, $entry); |
|
| 993 | } |
||
| 994 | 150 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 995 | 150 | $this->computeChangeSet($targetClass, $entry); |
|
| 996 | } |
||
| 997 | 331 | break; |
|
| 998 | |||
| 999 | 1 | case self::STATE_REMOVED: |
|
| 1000 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 1001 | // and remove the element from Collection. |
||
| 1002 | 1 | if ($assoc['type'] === ClassMetadata::MANY) { |
|
| 1003 | unset($value[$key]); |
||
| 1004 | } |
||
| 1005 | 1 | break; |
|
| 1006 | |||
| 1007 | case self::STATE_DETACHED: |
||
| 1008 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 1009 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 1010 | throw new \InvalidArgumentException('A detached document was found through a ' |
||
| 1011 | . 'relationship during cascading a persist operation.'); |
||
| 1012 | |||
| 1013 | 335 | default: |
|
| 1014 | // MANAGED associated documents are already taken into account |
||
| 1015 | // during changeset calculation anyway, since they are in the identity map. |
||
| 1016 | |||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | 422 | } |
|
| 1020 | |||
| 1021 | /** |
||
| 1022 | * INTERNAL: |
||
| 1023 | * Computes the changeset of an individual document, independently of the |
||
| 1024 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1025 | * |
||
| 1026 | * The passed document must be a managed document. If the document already has a change set |
||
| 1027 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1028 | * whereby changes detected in this method prevail. |
||
| 1029 | * |
||
| 1030 | * @ignore |
||
| 1031 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1032 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1033 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1034 | */ |
||
| 1035 | 17 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1054 | |||
| 1055 | /** |
||
| 1056 | * @param ClassMetadata $class |
||
| 1057 | * @param object $document |
||
| 1058 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1059 | */ |
||
| 1060 | 606 | private function persistNew(ClassMetadata $class, $document) |
|
| 1061 | { |
||
| 1062 | 606 | $this->lifecycleEventManager->prePersist($class, $document); |
|
| 1063 | 606 | $oid = spl_object_hash($document); |
|
| 1064 | 606 | $upsert = false; |
|
| 1065 | 606 | if ($class->identifier) { |
|
| 1066 | 606 | $idValue = $class->getIdentifierValue($document); |
|
| 1067 | 606 | $upsert = ! $class->isEmbeddedDocument && $idValue !== null; |
|
| 1068 | |||
| 1069 | 606 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
|
| 1070 | 3 | throw new \InvalidArgumentException(sprintf( |
|
| 1071 | 3 | '%s uses NONE identifier generation strategy but no identifier was provided when persisting.', |
|
| 1072 | 3 | get_class($document) |
|
| 1073 | )); |
||
| 1074 | } |
||
| 1075 | |||
| 1076 | 605 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && ! preg_match('#^[0-9a-f]{24}$#', $idValue)) { |
|
| 1077 | 1 | throw new \InvalidArgumentException(sprintf( |
|
| 1078 | 1 | '%s uses AUTO identifier generation strategy but provided identifier is not a valid ObjectId.', |
|
| 1079 | 1 | get_class($document) |
|
| 1080 | )); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | 604 | if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
|
| 1084 | 525 | $idValue = $class->idGenerator->generate($this->dm, $document); |
|
| 1085 | 525 | $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue)); |
|
| 1086 | 525 | $class->setIdentifierValue($document, $idValue); |
|
| 1087 | } |
||
| 1088 | |||
| 1089 | 604 | $this->documentIdentifiers[$oid] = $idValue; |
|
| 1090 | } else { |
||
| 1091 | // this is for embedded documents without identifiers |
||
| 1092 | 130 | $this->documentIdentifiers[$oid] = $oid; |
|
| 1093 | } |
||
| 1094 | |||
| 1095 | 604 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 1096 | |||
| 1097 | 604 | if ($upsert) { |
|
| 1098 | 89 | $this->scheduleForUpsert($class, $document); |
|
| 1099 | } else { |
||
| 1100 | 533 | $this->scheduleForInsert($class, $document); |
|
| 1101 | } |
||
| 1102 | 604 | } |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Executes all document insertions for documents of the specified type. |
||
| 1106 | * |
||
| 1107 | * @param ClassMetadata $class |
||
| 1108 | * @param array $documents Array of documents to insert |
||
| 1109 | * @param array $options Array of options to be used with batchInsert() |
||
| 1110 | */ |
||
| 1111 | 496 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Executes all document upserts for documents of the specified type. |
||
| 1129 | * |
||
| 1130 | * @param ClassMetadata $class |
||
| 1131 | * @param array $documents Array of documents to upsert |
||
| 1132 | * @param array $options Array of options to be used with batchInsert() |
||
| 1133 | */ |
||
| 1134 | 86 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1150 | |||
| 1151 | /** |
||
| 1152 | * Executes all document updates for documents of the specified type. |
||
| 1153 | * |
||
| 1154 | * @param Mapping\ClassMetadata $class |
||
| 1155 | * @param array $documents Array of documents to update |
||
| 1156 | * @param array $options Array of options to be used with update() |
||
| 1157 | */ |
||
| 1158 | 208 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1159 | { |
||
| 1160 | 208 | if ($class->isReadOnly) { |
|
| 1161 | return; |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | 208 | $className = $class->name; |
|
| 1165 | 208 | $persister = $this->getDocumentPersister($className); |
|
| 1166 | |||
| 1167 | 208 | foreach ($documents as $oid => $document) { |
|
| 1168 | 208 | $this->lifecycleEventManager->preUpdate($class, $document); |
|
| 1169 | |||
| 1170 | 208 | if ( ! empty($this->documentChangeSets[$oid]) || $this->hasScheduledCollections($document)) { |
|
| 1171 | 207 | $persister->update($document, $options); |
|
| 1172 | } |
||
| 1173 | |||
| 1174 | 204 | unset($this->documentUpdates[$oid]); |
|
| 1175 | |||
| 1176 | 204 | $this->lifecycleEventManager->postUpdate($class, $document); |
|
| 1177 | } |
||
| 1178 | 204 | } |
|
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Executes all document deletions for documents of the specified type. |
||
| 1182 | * |
||
| 1183 | * @param ClassMetadata $class |
||
| 1184 | * @param array $documents Array of documents to delete |
||
| 1185 | * @param array $options Array of options to be used with remove() |
||
| 1186 | */ |
||
| 1187 | 65 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Schedules a document for insertion into the database. |
||
| 1222 | * If the document already has an identifier, it will be added to the |
||
| 1223 | * identity map. |
||
| 1224 | * |
||
| 1225 | * @param ClassMetadata $class |
||
| 1226 | * @param object $document The document to schedule for insertion. |
||
| 1227 | * @throws \InvalidArgumentException |
||
| 1228 | */ |
||
| 1229 | 536 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Schedules a document for upsert into the database and adds it to the |
||
| 1252 | * identity map |
||
| 1253 | * |
||
| 1254 | * @param ClassMetadata $class |
||
| 1255 | * @param object $document The document to schedule for upsert. |
||
| 1256 | * @throws \InvalidArgumentException |
||
| 1257 | */ |
||
| 1258 | 92 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Checks whether a document is scheduled for insertion. |
||
| 1282 | * |
||
| 1283 | * @param object $document |
||
| 1284 | * @return boolean |
||
| 1285 | */ |
||
| 1286 | 92 | public function isScheduledForInsert($document) |
|
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Checks whether a document is scheduled for upsert. |
||
| 1293 | * |
||
| 1294 | * @param object $document |
||
| 1295 | * @return boolean |
||
| 1296 | */ |
||
| 1297 | 5 | public function isScheduledForUpsert($document) |
|
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Schedules a document for being updated. |
||
| 1304 | * |
||
| 1305 | * @param object $document The document to schedule for being updated. |
||
| 1306 | * @throws \InvalidArgumentException |
||
| 1307 | */ |
||
| 1308 | 214 | public function scheduleForUpdate($document) |
|
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1328 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1329 | * at commit time. |
||
| 1330 | * |
||
| 1331 | * @param object $document |
||
| 1332 | * @return boolean |
||
| 1333 | */ |
||
| 1334 | 15 | public function isScheduledForUpdate($document) |
|
| 1338 | |||
| 1339 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1344 | |||
| 1345 | /** |
||
| 1346 | * INTERNAL: |
||
| 1347 | * Schedules a document for deletion. |
||
| 1348 | * |
||
| 1349 | * @param object $document |
||
| 1350 | */ |
||
| 1351 | 70 | public function scheduleForDelete($document) |
|
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1380 | * of work. |
||
| 1381 | * |
||
| 1382 | * @param object $document |
||
| 1383 | * @return boolean |
||
| 1384 | */ |
||
| 1385 | 8 | public function isScheduledForDelete($document) |
|
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1392 | * |
||
| 1393 | * @param $document |
||
| 1394 | * @return boolean |
||
| 1395 | */ |
||
| 1396 | 226 | public function isDocumentScheduled($document) |
|
| 1404 | |||
| 1405 | /** |
||
| 1406 | * INTERNAL: |
||
| 1407 | * Registers a document in the identity map. |
||
| 1408 | * |
||
| 1409 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1410 | * the root document. Identifiers are serialized before being used as array |
||
| 1411 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1412 | * |
||
| 1413 | * @ignore |
||
| 1414 | * @param object $document The document to register. |
||
| 1415 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1416 | * the document in question is already managed. |
||
| 1417 | */ |
||
| 1418 | 636 | public function addToIdentityMap($document) |
|
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Gets the state of a document with regard to the current unit of work. |
||
| 1439 | * |
||
| 1440 | * @param object $document |
||
| 1441 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1442 | * This parameter can be set to improve performance of document state detection |
||
| 1443 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1444 | * is either known or does not matter for the caller of the method. |
||
| 1445 | * @return int The document state. |
||
| 1446 | */ |
||
| 1447 | 609 | public function getDocumentState($document, $assume = null) |
|
| 1497 | |||
| 1498 | /** |
||
| 1499 | * INTERNAL: |
||
| 1500 | * Removes a document from the identity map. This effectively detaches the |
||
| 1501 | * document from the persistence management of Doctrine. |
||
| 1502 | * |
||
| 1503 | * @ignore |
||
| 1504 | * @param object $document |
||
| 1505 | * @throws \InvalidArgumentException |
||
| 1506 | * @return boolean |
||
| 1507 | */ |
||
| 1508 | 82 | public function removeFromIdentityMap($document) |
|
| 1528 | |||
| 1529 | /** |
||
| 1530 | * INTERNAL: |
||
| 1531 | * Gets a document in the identity map by its identifier hash. |
||
| 1532 | * |
||
| 1533 | * @ignore |
||
| 1534 | * @param mixed $id Document identifier |
||
| 1535 | * @param ClassMetadata $class Document class |
||
| 1536 | * @return object |
||
| 1537 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1538 | */ |
||
| 1539 | 38 | public function getById($id, ClassMetadata $class) |
|
| 1549 | |||
| 1550 | /** |
||
| 1551 | * INTERNAL: |
||
| 1552 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1553 | * for the given hash, FALSE is returned. |
||
| 1554 | * |
||
| 1555 | * @ignore |
||
| 1556 | * @param mixed $id Document identifier |
||
| 1557 | * @param ClassMetadata $class Document class |
||
| 1558 | * @return mixed The found document or FALSE. |
||
| 1559 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1560 | */ |
||
| 1561 | 298 | public function tryGetById($id, ClassMetadata $class) |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Schedules a document for dirty-checking at commit-time. |
||
| 1575 | * |
||
| 1576 | * @param object $document The document to schedule for dirty-checking. |
||
| 1577 | * @todo Rename: scheduleForSynchronization |
||
| 1578 | */ |
||
| 1579 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Checks whether a document is registered in the identity map. |
||
| 1587 | * |
||
| 1588 | * @param object $document |
||
| 1589 | * @return boolean |
||
| 1590 | */ |
||
| 1591 | 81 | public function isInIdentityMap($document) |
|
| 1604 | |||
| 1605 | /** |
||
| 1606 | * @param object $document |
||
| 1607 | * @return string |
||
| 1608 | */ |
||
| 1609 | 636 | private function getIdForIdentityMap($document) |
|
| 1622 | |||
| 1623 | /** |
||
| 1624 | * INTERNAL: |
||
| 1625 | * Checks whether an identifier exists in the identity map. |
||
| 1626 | * |
||
| 1627 | * @ignore |
||
| 1628 | * @param string $id |
||
| 1629 | * @param string $rootClassName |
||
| 1630 | * @return boolean |
||
| 1631 | */ |
||
| 1632 | public function containsId($id, $rootClassName) |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Persists a document as part of the current unit of work. |
||
| 1639 | * |
||
| 1640 | * @param object $document The document to persist. |
||
| 1641 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1642 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1643 | */ |
||
| 1644 | 606 | public function persist($document) |
|
| 1645 | { |
||
| 1646 | 606 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1647 | 606 | if ($class->isMappedSuperclass || $class->isQueryResultDocument) { |
|
| 1648 | 1 | throw MongoDBException::cannotPersistMappedSuperclass($class->name); |
|
| 1649 | } |
||
| 1650 | 605 | $visited = array(); |
|
| 1651 | 605 | $this->doPersist($document, $visited); |
|
| 1652 | 601 | } |
|
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Saves a document as part of the current unit of work. |
||
| 1656 | * This method is internally called during save() cascades as it tracks |
||
| 1657 | * the already visited documents to prevent infinite recursions. |
||
| 1658 | * |
||
| 1659 | * NOTE: This method always considers documents that are not yet known to |
||
| 1660 | * this UnitOfWork as NEW. |
||
| 1661 | * |
||
| 1662 | * @param object $document The document to persist. |
||
| 1663 | * @param array $visited The already visited documents. |
||
| 1664 | * @throws \InvalidArgumentException |
||
| 1665 | * @throws MongoDBException |
||
| 1666 | */ |
||
| 1667 | 605 | private function doPersist($document, array &$visited) |
|
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Deletes a document as part of the current unit of work. |
||
| 1710 | * |
||
| 1711 | * @param object $document The document to remove. |
||
| 1712 | */ |
||
| 1713 | 69 | public function remove($document) |
|
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Deletes a document as part of the current unit of work. |
||
| 1721 | * |
||
| 1722 | * This method is internally called during delete() cascades as it tracks |
||
| 1723 | * the already visited documents to prevent infinite recursions. |
||
| 1724 | * |
||
| 1725 | * @param object $document The document to delete. |
||
| 1726 | * @param array $visited The map of the already visited documents. |
||
| 1727 | * @throws MongoDBException |
||
| 1728 | */ |
||
| 1729 | 69 | private function doRemove($document, array &$visited) |
|
| 1730 | { |
||
| 1731 | 69 | $oid = spl_object_hash($document); |
|
| 1732 | 69 | if (isset($visited[$oid])) { |
|
| 1733 | 1 | return; // Prevent infinite recursion |
|
| 1734 | } |
||
| 1735 | |||
| 1736 | 69 | $visited[$oid] = $document; // mark visited |
|
| 1737 | |||
| 1738 | /* Cascade first, because scheduleForDelete() removes the entity from |
||
| 1739 | * the identity map, which can cause problems when a lazy Proxy has to |
||
| 1740 | * be initialized for the cascade operation. |
||
| 1741 | */ |
||
| 1742 | 69 | $this->cascadeRemove($document, $visited); |
|
| 1743 | |||
| 1744 | 69 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1745 | 69 | $documentState = $this->getDocumentState($document); |
|
| 1746 | switch ($documentState) { |
||
| 1747 | 69 | case self::STATE_NEW: |
|
| 1748 | 69 | case self::STATE_REMOVED: |
|
| 1749 | // nothing to do |
||
| 1750 | break; |
||
| 1751 | 69 | case self::STATE_MANAGED: |
|
| 1752 | 69 | $this->lifecycleEventManager->preRemove($class, $document); |
|
| 1753 | 69 | $this->scheduleForDelete($document); |
|
| 1754 | 69 | break; |
|
| 1755 | case self::STATE_DETACHED: |
||
| 1756 | throw MongoDBException::detachedDocumentCannotBeRemoved(); |
||
| 1757 | default: |
||
| 1758 | throw MongoDBException::invalidDocumentState($documentState); |
||
| 1759 | } |
||
| 1760 | 69 | } |
|
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1764 | * |
||
| 1765 | * @param object $document |
||
| 1766 | * @return object The managed copy of the document. |
||
| 1767 | */ |
||
| 1768 | 12 | public function merge($document) |
|
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Executes a merge operation on a document. |
||
| 1777 | * |
||
| 1778 | * @param object $document |
||
| 1779 | * @param array $visited |
||
| 1780 | * @param object|null $prevManagedCopy |
||
| 1781 | * @param array|null $assoc |
||
| 1782 | * |
||
| 1783 | * @return object The managed copy of the document. |
||
| 1784 | * |
||
| 1785 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1786 | * @throws LockException If the document uses optimistic locking through a |
||
| 1787 | * version attribute and the version check against the |
||
| 1788 | * managed copy fails. |
||
| 1789 | */ |
||
| 1790 | 12 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1791 | { |
||
| 1792 | 12 | $oid = spl_object_hash($document); |
|
| 1793 | |||
| 1794 | 12 | if (isset($visited[$oid])) { |
|
| 1795 | 1 | return $visited[$oid]; // Prevent infinite recursion |
|
| 1796 | } |
||
| 1797 | |||
| 1798 | 12 | $visited[$oid] = $document; // mark visited |
|
| 1799 | |||
| 1800 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1801 | |||
| 1802 | /* First we assume DETACHED, although it can still be NEW but we can |
||
| 1803 | * avoid an extra DB round trip this way. If it is not MANAGED but has |
||
| 1804 | * an identity, we need to fetch it from the DB anyway in order to |
||
| 1805 | * merge. MANAGED documents are ignored by the merge operation. |
||
| 1806 | */ |
||
| 1807 | 12 | $managedCopy = $document; |
|
| 1808 | |||
| 1809 | 12 | if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
|
| 1810 | 12 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 1811 | $document->__load(); |
||
| 1812 | } |
||
| 1813 | |||
| 1814 | 12 | $identifier = $class->getIdentifier(); |
|
| 1815 | // We always have one element in the identifier array but it might be null |
||
| 1816 | 12 | $id = $identifier[0] !== null ? $class->getIdentifierObject($document) : null; |
|
| 1817 | 12 | $managedCopy = null; |
|
| 1818 | |||
| 1819 | // Try to fetch document from the database |
||
| 1820 | 12 | if (! $class->isEmbeddedDocument && $id !== null) { |
|
| 1821 | 12 | $managedCopy = $this->dm->find($class->name, $id); |
|
| 1822 | |||
| 1823 | // Managed copy may be removed in which case we can't merge |
||
| 1824 | 12 | if ($managedCopy && $this->getDocumentState($managedCopy) === self::STATE_REMOVED) { |
|
| 1825 | throw new \InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.'); |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | 12 | if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) { |
|
| 1829 | $managedCopy->__load(); |
||
| 1830 | } |
||
| 1831 | } |
||
| 1832 | |||
| 1833 | 12 | if ($managedCopy === null) { |
|
| 1834 | // Create a new managed instance |
||
| 1835 | 4 | $managedCopy = $class->newInstance(); |
|
| 1836 | 4 | if ($id !== null) { |
|
| 1837 | 3 | $class->setIdentifierValue($managedCopy, $id); |
|
| 1838 | } |
||
| 1839 | 4 | $this->persistNew($class, $managedCopy); |
|
| 1840 | } |
||
| 1841 | |||
| 1842 | 12 | if ($class->isVersioned) { |
|
| 1843 | $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); |
||
| 1844 | $documentVersion = $class->reflFields[$class->versionField]->getValue($document); |
||
| 1845 | |||
| 1846 | // Throw exception if versions don't match |
||
| 1847 | if ($managedCopyVersion != $documentVersion) { |
||
| 1848 | throw LockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion); |
||
| 1849 | } |
||
| 1850 | } |
||
| 1851 | |||
| 1852 | // Merge state of $document into existing (managed) document |
||
| 1853 | 12 | foreach ($class->reflClass->getProperties() as $prop) { |
|
| 1854 | 12 | $name = $prop->name; |
|
| 1855 | 12 | $prop->setAccessible(true); |
|
| 1856 | 12 | if ( ! isset($class->associationMappings[$name])) { |
|
| 1857 | 12 | if ( ! $class->isIdentifier($name)) { |
|
| 1858 | 12 | $prop->setValue($managedCopy, $prop->getValue($document)); |
|
| 1859 | } |
||
| 1860 | } else { |
||
| 1861 | 12 | $assoc2 = $class->associationMappings[$name]; |
|
| 1862 | |||
| 1863 | 12 | if ($assoc2['type'] === 'one') { |
|
| 1864 | 6 | $other = $prop->getValue($document); |
|
| 1865 | |||
| 1866 | 6 | if ($other === null) { |
|
| 1867 | 2 | $prop->setValue($managedCopy, null); |
|
| 1868 | 5 | } elseif ($other instanceof Proxy && ! $other->__isInitialized__) { |
|
| 1869 | // Do not merge fields marked lazy that have not been fetched |
||
| 1870 | 1 | continue; |
|
| 1871 | 4 | } elseif ( ! $assoc2['isCascadeMerge']) { |
|
| 1872 | if ($this->getDocumentState($other) === self::STATE_DETACHED) { |
||
| 1873 | $targetDocument = $assoc2['targetDocument'] ?? get_class($other); |
||
| 1874 | /* @var $targetClass \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */ |
||
| 1875 | $targetClass = $this->dm->getClassMetadata($targetDocument); |
||
| 1876 | $relatedId = $targetClass->getIdentifierObject($other); |
||
| 1877 | |||
| 1878 | if ($targetClass->subClasses) { |
||
| 1879 | $other = $this->dm->find($targetClass->name, $relatedId); |
||
| 1880 | } else { |
||
| 1881 | $other = $this |
||
| 1882 | ->dm |
||
| 1883 | ->getProxyFactory() |
||
| 1884 | ->getProxy($assoc2['targetDocument'], array($targetClass->identifier => $relatedId)); |
||
| 1885 | $this->registerManaged($other, $relatedId, array()); |
||
| 1886 | } |
||
| 1887 | } |
||
| 1888 | |||
| 1889 | 5 | $prop->setValue($managedCopy, $other); |
|
| 1890 | } |
||
| 1891 | } else { |
||
| 1892 | 10 | $mergeCol = $prop->getValue($document); |
|
| 1893 | |||
| 1894 | 10 | if ($mergeCol instanceof PersistentCollectionInterface && ! $mergeCol->isInitialized() && ! $assoc2['isCascadeMerge']) { |
|
| 1895 | /* Do not merge fields marked lazy that have not |
||
| 1896 | * been fetched. Keep the lazy persistent collection |
||
| 1897 | * of the managed copy. |
||
| 1898 | */ |
||
| 1899 | 3 | continue; |
|
| 1900 | } |
||
| 1901 | |||
| 1902 | 10 | $managedCol = $prop->getValue($managedCopy); |
|
| 1903 | |||
| 1904 | 10 | if ( ! $managedCol) { |
|
| 1905 | 1 | $managedCol = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $assoc2, null); |
|
| 1906 | 1 | $managedCol->setOwner($managedCopy, $assoc2); |
|
| 1907 | 1 | $prop->setValue($managedCopy, $managedCol); |
|
| 1908 | 1 | $this->originalDocumentData[$oid][$name] = $managedCol; |
|
| 1909 | } |
||
| 1910 | |||
| 1911 | /* Note: do not process association's target documents. |
||
| 1912 | * They will be handled during the cascade. Initialize |
||
| 1913 | * and, if necessary, clear $managedCol for now. |
||
| 1914 | */ |
||
| 1915 | 10 | if ($assoc2['isCascadeMerge']) { |
|
| 1916 | 10 | $managedCol->initialize(); |
|
| 1917 | |||
| 1918 | // If $managedCol differs from the merged collection, clear and set dirty |
||
| 1919 | 10 | if ( ! $managedCol->isEmpty() && $managedCol !== $mergeCol) { |
|
| 1920 | 3 | $managedCol->unwrap()->clear(); |
|
| 1921 | 3 | $managedCol->setDirty(true); |
|
| 1922 | |||
| 1923 | 3 | if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) { |
|
| 1924 | $this->scheduleForDirtyCheck($managedCopy); |
||
| 1925 | } |
||
| 1926 | } |
||
| 1927 | } |
||
| 1928 | } |
||
| 1929 | } |
||
| 1930 | |||
| 1931 | 12 | if ($class->isChangeTrackingNotify()) { |
|
| 1932 | // Just treat all properties as changed, there is no other choice. |
||
| 1933 | 12 | $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); |
|
| 1934 | } |
||
| 1935 | } |
||
| 1936 | |||
| 1937 | 12 | if ($class->isChangeTrackingDeferredExplicit()) { |
|
| 1938 | $this->scheduleForDirtyCheck($document); |
||
| 1939 | } |
||
| 1940 | } |
||
| 1941 | |||
| 1942 | 12 | if ($prevManagedCopy !== null) { |
|
| 1943 | 5 | $assocField = $assoc['fieldName']; |
|
| 1944 | 5 | $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); |
|
| 1945 | |||
| 1946 | 5 | if ($assoc['type'] === 'one') { |
|
| 1947 | 3 | $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); |
|
| 1948 | } else { |
||
| 1949 | 4 | $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy); |
|
| 1950 | |||
| 1951 | 4 | if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) { |
|
| 1952 | 1 | $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy); |
|
| 1953 | } |
||
| 1954 | } |
||
| 1955 | } |
||
| 1956 | |||
| 1957 | // Mark the managed copy visited as well |
||
| 1958 | 12 | $visited[spl_object_hash($managedCopy)] = true; |
|
| 1959 | |||
| 1960 | 12 | $this->cascadeMerge($document, $managedCopy, $visited); |
|
| 1961 | |||
| 1962 | 12 | return $managedCopy; |
|
| 1963 | } |
||
| 1964 | |||
| 1965 | /** |
||
| 1966 | * Detaches a document from the persistence management. It's persistence will |
||
| 1967 | * no longer be managed by Doctrine. |
||
| 1968 | * |
||
| 1969 | * @param object $document The document to detach. |
||
| 1970 | */ |
||
| 1971 | 11 | public function detach($document) |
|
| 1976 | |||
| 1977 | /** |
||
| 1978 | * Executes a detach operation on the given document. |
||
| 1979 | * |
||
| 1980 | * @param object $document |
||
| 1981 | * @param array $visited |
||
| 1982 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1983 | */ |
||
| 1984 | 16 | private function doDetach($document, array &$visited) |
|
| 2009 | |||
| 2010 | /** |
||
| 2011 | * Refreshes the state of the given document from the database, overwriting |
||
| 2012 | * any local, unpersisted changes. |
||
| 2013 | * |
||
| 2014 | * @param object $document The document to refresh. |
||
| 2015 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2016 | */ |
||
| 2017 | 21 | public function refresh($document) |
|
| 2022 | |||
| 2023 | /** |
||
| 2024 | * Executes a refresh operation on a document. |
||
| 2025 | * |
||
| 2026 | * @param object $document The document to refresh. |
||
| 2027 | * @param array $visited The already visited documents during cascades. |
||
| 2028 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2029 | */ |
||
| 2030 | 21 | private function doRefresh($document, array &$visited) |
|
| 2031 | { |
||
| 2032 | 21 | $oid = spl_object_hash($document); |
|
| 2033 | 21 | if (isset($visited[$oid])) { |
|
| 2034 | return; // Prevent infinite recursion |
||
| 2035 | } |
||
| 2036 | |||
| 2037 | 21 | $visited[$oid] = $document; // mark visited |
|
| 2038 | |||
| 2039 | 21 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2040 | |||
| 2041 | 21 | if ( ! $class->isEmbeddedDocument) { |
|
| 2051 | |||
| 2052 | /** |
||
| 2053 | * Cascades a refresh operation to associated documents. |
||
| 2054 | * |
||
| 2055 | * @param object $document |
||
| 2056 | * @param array $visited |
||
| 2057 | */ |
||
| 2058 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2082 | |||
| 2083 | /** |
||
| 2084 | * Cascades a detach operation to associated documents. |
||
| 2085 | * |
||
| 2086 | * @param object $document |
||
| 2087 | * @param array $visited |
||
| 2088 | */ |
||
| 2089 | 16 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2110 | /** |
||
| 2111 | * Cascades a merge operation to associated documents. |
||
| 2112 | * |
||
| 2113 | * @param object $document |
||
| 2114 | * @param object $managedCopy |
||
| 2115 | * @param array $visited |
||
| 2116 | */ |
||
| 2117 | 12 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2143 | |||
| 2144 | /** |
||
| 2145 | * Cascades the save operation to associated documents. |
||
| 2146 | * |
||
| 2147 | * @param object $document |
||
| 2148 | * @param array $visited |
||
| 2149 | */ |
||
| 2150 | 603 | private function cascadePersist($document, array &$visited) |
|
| 2197 | |||
| 2198 | /** |
||
| 2199 | * Cascades the delete operation to associated documents. |
||
| 2200 | * |
||
| 2201 | * @param object $document |
||
| 2202 | * @param array $visited |
||
| 2203 | */ |
||
| 2204 | 69 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2226 | |||
| 2227 | /** |
||
| 2228 | * Acquire a lock on the given document. |
||
| 2229 | * |
||
| 2230 | * @param object $document |
||
| 2231 | * @param int $lockMode |
||
| 2232 | * @param int $lockVersion |
||
| 2233 | * @throws LockException |
||
| 2234 | * @throws \InvalidArgumentException |
||
| 2235 | */ |
||
| 2236 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2260 | |||
| 2261 | /** |
||
| 2262 | * Releases a lock on the given document. |
||
| 2263 | * |
||
| 2264 | * @param object $document |
||
| 2265 | * @throws \InvalidArgumentException |
||
| 2266 | */ |
||
| 2267 | 1 | public function unlock($document) |
|
| 2275 | |||
| 2276 | /** |
||
| 2277 | * Clears the UnitOfWork. |
||
| 2278 | * |
||
| 2279 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2280 | */ |
||
| 2281 | 377 | public function clear($documentName = null) |
|
| 2315 | |||
| 2316 | /** |
||
| 2317 | * INTERNAL: |
||
| 2318 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2319 | * invoked on that document at the beginning of the next commit of this |
||
| 2320 | * UnitOfWork. |
||
| 2321 | * |
||
| 2322 | * @ignore |
||
| 2323 | * @param object $document |
||
| 2324 | */ |
||
| 2325 | 47 | public function scheduleOrphanRemoval($document) |
|
| 2329 | |||
| 2330 | /** |
||
| 2331 | * INTERNAL: |
||
| 2332 | * Unschedules an embedded or referenced object for removal. |
||
| 2333 | * |
||
| 2334 | * @ignore |
||
| 2335 | * @param object $document |
||
| 2336 | */ |
||
| 2337 | 100 | public function unscheduleOrphanRemoval($document) |
|
| 2344 | |||
| 2345 | /** |
||
| 2346 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2347 | * 1) sets owner if it was cloned |
||
| 2348 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2349 | * 3) NOP if state is OK |
||
| 2350 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2351 | * |
||
| 2352 | * @param PersistentCollectionInterface $coll |
||
| 2353 | * @param object $document |
||
| 2354 | * @param ClassMetadata $class |
||
| 2355 | * @param string $propName |
||
| 2356 | * @return PersistentCollectionInterface |
||
| 2357 | */ |
||
| 2358 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2378 | |||
| 2379 | /** |
||
| 2380 | * INTERNAL: |
||
| 2381 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2382 | * |
||
| 2383 | * @param PersistentCollectionInterface $coll |
||
| 2384 | */ |
||
| 2385 | 35 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2394 | |||
| 2395 | /** |
||
| 2396 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2397 | * |
||
| 2398 | * @param PersistentCollectionInterface $coll |
||
| 2399 | * @return boolean |
||
| 2400 | */ |
||
| 2401 | 195 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2405 | |||
| 2406 | /** |
||
| 2407 | * INTERNAL: |
||
| 2408 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2409 | * |
||
| 2410 | * @param PersistentCollectionInterface $coll |
||
| 2411 | */ |
||
| 2412 | 202 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2421 | |||
| 2422 | /** |
||
| 2423 | * INTERNAL: |
||
| 2424 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2425 | * |
||
| 2426 | * @param PersistentCollectionInterface $coll |
||
| 2427 | */ |
||
| 2428 | 223 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2443 | |||
| 2444 | /** |
||
| 2445 | * INTERNAL: |
||
| 2446 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2447 | * |
||
| 2448 | * @param PersistentCollectionInterface $coll |
||
| 2449 | */ |
||
| 2450 | 202 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2459 | |||
| 2460 | /** |
||
| 2461 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2462 | * |
||
| 2463 | * @param PersistentCollectionInterface $coll |
||
| 2464 | * @return boolean |
||
| 2465 | */ |
||
| 2466 | 113 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2470 | |||
| 2471 | /** |
||
| 2472 | * INTERNAL: |
||
| 2473 | * Gets PersistentCollections that have been visited during computing change |
||
| 2474 | * set of $document |
||
| 2475 | * |
||
| 2476 | * @param object $document |
||
| 2477 | * @return PersistentCollectionInterface[] |
||
| 2478 | */ |
||
| 2479 | 557 | public function getVisitedCollections($document) |
|
| 2486 | |||
| 2487 | /** |
||
| 2488 | * INTERNAL: |
||
| 2489 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2490 | * |
||
| 2491 | * @param object $document |
||
| 2492 | * @return array |
||
| 2493 | */ |
||
| 2494 | 557 | public function getScheduledCollections($document) |
|
| 2501 | |||
| 2502 | /** |
||
| 2503 | * Checks whether the document is related to a PersistentCollection |
||
| 2504 | * scheduled for update or deletion. |
||
| 2505 | * |
||
| 2506 | * @param object $document |
||
| 2507 | * @return boolean |
||
| 2508 | */ |
||
| 2509 | 44 | public function hasScheduledCollections($document) |
|
| 2513 | |||
| 2514 | /** |
||
| 2515 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2516 | * a collection scheduled for update or deletion. |
||
| 2517 | * |
||
| 2518 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2519 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2520 | * |
||
| 2521 | * If the collection is nested within atomic collection, it is immediately |
||
| 2522 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2523 | * calculating update data way easier. |
||
| 2524 | * |
||
| 2525 | * @param PersistentCollectionInterface $coll |
||
| 2526 | */ |
||
| 2527 | 225 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2550 | |||
| 2551 | /** |
||
| 2552 | * Get the top-most owning document of a given document |
||
| 2553 | * |
||
| 2554 | * If a top-level document is provided, that same document will be returned. |
||
| 2555 | * For an embedded document, we will walk through parent associations until |
||
| 2556 | * we find a top-level document. |
||
| 2557 | * |
||
| 2558 | * @param object $document |
||
| 2559 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2560 | * @return object |
||
| 2561 | */ |
||
| 2562 | 227 | public function getOwningDocument($document) |
|
| 2578 | |||
| 2579 | /** |
||
| 2580 | * Gets the class name for an association (embed or reference) with respect |
||
| 2581 | * to any discriminator value. |
||
| 2582 | * |
||
| 2583 | * @param array $mapping Field mapping for the association |
||
| 2584 | * @param array|null $data Data for the embedded document or reference |
||
| 2585 | * @return string Class name. |
||
| 2586 | */ |
||
| 2587 | 218 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2619 | |||
| 2620 | /** |
||
| 2621 | * INTERNAL: |
||
| 2622 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2623 | * |
||
| 2624 | * @ignore |
||
| 2625 | * @param string $className The name of the document class. |
||
| 2626 | * @param array $data The data for the document. |
||
| 2627 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2628 | * @param object $document The document to be hydrated into in case of creation |
||
| 2629 | * @return object The document instance. |
||
| 2630 | * @internal Highly performance-sensitive method. |
||
| 2631 | */ |
||
| 2632 | 385 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2704 | |||
| 2705 | /** |
||
| 2706 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2707 | * |
||
| 2708 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2709 | */ |
||
| 2710 | 163 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2715 | |||
| 2716 | /** |
||
| 2717 | * Gets the identity map of the UnitOfWork. |
||
| 2718 | * |
||
| 2719 | * @return array |
||
| 2720 | */ |
||
| 2721 | public function getIdentityMap() |
||
| 2725 | |||
| 2726 | /** |
||
| 2727 | * Gets the original data of a document. The original data is the data that was |
||
| 2728 | * present at the time the document was reconstituted from the database. |
||
| 2729 | * |
||
| 2730 | * @param object $document |
||
| 2731 | * @return array |
||
| 2732 | */ |
||
| 2733 | 1 | public function getOriginalDocumentData($document) |
|
| 2741 | |||
| 2742 | /** |
||
| 2743 | * @ignore |
||
| 2744 | */ |
||
| 2745 | 58 | public function setOriginalDocumentData($document, array $data) |
|
| 2751 | |||
| 2752 | /** |
||
| 2753 | * INTERNAL: |
||
| 2754 | * Sets a property value of the original data array of a document. |
||
| 2755 | * |
||
| 2756 | * @ignore |
||
| 2757 | * @param string $oid |
||
| 2758 | * @param string $property |
||
| 2759 | * @param mixed $value |
||
| 2760 | */ |
||
| 2761 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2765 | |||
| 2766 | /** |
||
| 2767 | * Gets the identifier of a document. |
||
| 2768 | * |
||
| 2769 | * @param object $document |
||
| 2770 | * @return mixed The identifier value |
||
| 2771 | */ |
||
| 2772 | 418 | public function getDocumentIdentifier($document) |
|
| 2777 | |||
| 2778 | /** |
||
| 2779 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2780 | * |
||
| 2781 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2782 | */ |
||
| 2783 | public function hasPendingInsertions() |
||
| 2787 | |||
| 2788 | /** |
||
| 2789 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2790 | * number of documents in the identity map. |
||
| 2791 | * |
||
| 2792 | * @return integer |
||
| 2793 | */ |
||
| 2794 | 2 | public function size() |
|
| 2802 | |||
| 2803 | /** |
||
| 2804 | * INTERNAL: |
||
| 2805 | * Registers a document as managed. |
||
| 2806 | * |
||
| 2807 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2808 | * document class. If the class expects its database identifier to be an |
||
| 2809 | * ObjectId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2810 | * document identifiers map will become inconsistent with the identity map. |
||
| 2811 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2812 | * conversion and throw an exception if it's inconsistent. |
||
| 2813 | * |
||
| 2814 | * @param object $document The document. |
||
| 2815 | * @param array $id The identifier values. |
||
| 2816 | * @param array $data The original document data. |
||
| 2817 | */ |
||
| 2818 | 367 | public function registerManaged($document, $id, $data) |
|
| 2833 | |||
| 2834 | /** |
||
| 2835 | * INTERNAL: |
||
| 2836 | * Clears the property changeset of the document with the given OID. |
||
| 2837 | * |
||
| 2838 | * @param string $oid The document's OID. |
||
| 2839 | */ |
||
| 2840 | public function clearDocumentChangeSet($oid) |
||
| 2844 | |||
| 2845 | /* PropertyChangedListener implementation */ |
||
| 2846 | |||
| 2847 | /** |
||
| 2848 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2849 | * |
||
| 2850 | * @param object $document The document that owns the property. |
||
| 2851 | * @param string $propertyName The name of the property that changed. |
||
| 2852 | * @param mixed $oldValue The old value of the property. |
||
| 2853 | * @param mixed $newValue The new value of the property. |
||
| 2854 | */ |
||
| 2855 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2870 | |||
| 2871 | /** |
||
| 2872 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2873 | * |
||
| 2874 | * @return array |
||
| 2875 | */ |
||
| 2876 | 2 | public function getScheduledDocumentInsertions() |
|
| 2880 | |||
| 2881 | /** |
||
| 2882 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2883 | * |
||
| 2884 | * @return array |
||
| 2885 | */ |
||
| 2886 | 1 | public function getScheduledDocumentUpserts() |
|
| 2890 | |||
| 2891 | /** |
||
| 2892 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2893 | * |
||
| 2894 | * @return array |
||
| 2895 | */ |
||
| 2896 | 1 | public function getScheduledDocumentUpdates() |
|
| 2900 | |||
| 2901 | /** |
||
| 2902 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2903 | * |
||
| 2904 | * @return array |
||
| 2905 | */ |
||
| 2906 | public function getScheduledDocumentDeletions() |
||
| 2910 | |||
| 2911 | /** |
||
| 2912 | * Get the currently scheduled complete collection deletions |
||
| 2913 | * |
||
| 2914 | * @return array |
||
| 2915 | */ |
||
| 2916 | public function getScheduledCollectionDeletions() |
||
| 2920 | |||
| 2921 | /** |
||
| 2922 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2923 | * |
||
| 2924 | * @return array |
||
| 2925 | */ |
||
| 2926 | public function getScheduledCollectionUpdates() |
||
| 2930 | |||
| 2931 | /** |
||
| 2932 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2933 | * |
||
| 2934 | * @param object |
||
| 2935 | * @return void |
||
| 2936 | */ |
||
| 2937 | public function initializeObject($obj) |
||
| 2945 | |||
| 2946 | 1 | private function objToStr($obj) |
|
| 2950 | } |
||
| 2951 |
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.