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 |
||
| 45 | class UnitOfWork implements PropertyChangedListener |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 49 | */ |
||
| 50 | const STATE_MANAGED = 1; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 54 | * and is not (yet) managed by a DocumentManager. |
||
| 55 | */ |
||
| 56 | const STATE_NEW = 2; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * A detached document is an instance with a persistent identity that is not |
||
| 60 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 61 | */ |
||
| 62 | const STATE_DETACHED = 3; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A removed document instance is an instance with a persistent identity, |
||
| 66 | * associated with a DocumentManager, whose persistent state has been |
||
| 67 | * deleted (or is scheduled for deletion). |
||
| 68 | */ |
||
| 69 | const STATE_REMOVED = 4; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The identity map holds references to all managed documents. |
||
| 73 | * |
||
| 74 | * Documents are grouped by their class name, and then indexed by the |
||
| 75 | * serialized string of their database identifier field or, if the class |
||
| 76 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 77 | * differentiation of values that may be equal (via type juggling) but not |
||
| 78 | * identical. |
||
| 79 | * |
||
| 80 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 81 | * we always take the root class name of the hierarchy. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $identityMap = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Map of all identifiers of managed documents. |
||
| 89 | * Keys are object ids (spl_object_hash). |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $documentIdentifiers = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Map of the original document data of managed documents. |
||
| 97 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 98 | * at commit time. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 102 | * A value will only really be copied if the value in the document is modified |
||
| 103 | * by the user. |
||
| 104 | */ |
||
| 105 | private $originalDocumentData = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 109 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | private $documentChangeSets = array(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The (cached) states of any known documents. |
||
| 117 | * Keys are object ids (spl_object_hash). |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $documentStates = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 125 | * |
||
| 126 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 127 | * object hash. This is only used for documents with a change tracking |
||
| 128 | * policy of DEFERRED_EXPLICIT. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | * @todo rename: scheduledForSynchronization |
||
| 132 | */ |
||
| 133 | private $scheduledForDirtyCheck = array(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of all pending document insertions. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $documentInsertions = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A list of all pending document updates. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $documentUpdates = array(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A list of all pending document upserts. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $documentUpserts = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending document deletions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $documentDeletions = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * All pending collection deletions. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $collectionDeletions = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * All pending collection updates. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $collectionUpdates = array(); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of documents related to collections scheduled for update or deletion |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $hasScheduledCollections = array(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 186 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 187 | * of their data. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $visitedCollections = array(); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 195 | * |
||
| 196 | * @var DocumentManager |
||
| 197 | */ |
||
| 198 | private $dm; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The EventManager used for dispatching events. |
||
| 202 | * |
||
| 203 | * @var EventManager |
||
| 204 | */ |
||
| 205 | private $evm; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Additional documents that are scheduled for removal. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | private $orphanRemovals = array(); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 216 | * |
||
| 217 | * @var HydratorFactory |
||
| 218 | */ |
||
| 219 | private $hydratorFactory; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The document persister instances used to persist document instances. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $persisters = array(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The collection persister instance used to persist changes to collections. |
||
| 230 | * |
||
| 231 | * @var Persisters\CollectionPersister |
||
| 232 | */ |
||
| 233 | private $collectionPersister; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The persistence builder instance used in DocumentPersisters. |
||
| 237 | * |
||
| 238 | * @var PersistenceBuilder |
||
| 239 | */ |
||
| 240 | private $persistenceBuilder; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Array of parent associations between embedded documents. |
||
| 244 | * |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | private $parentAssociations = array(); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @var LifecycleEventManager |
||
| 251 | */ |
||
| 252 | private $lifecycleEventManager; |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 256 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 257 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 258 | * |
||
| 259 | * @var array |
||
| 260 | */ |
||
| 261 | private $embeddedDocumentsRegistry = array(); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 265 | * |
||
| 266 | * @param DocumentManager $dm |
||
| 267 | * @param EventManager $evm |
||
| 268 | * @param HydratorFactory $hydratorFactory |
||
| 269 | */ |
||
| 270 | 1101 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 271 | { |
||
| 272 | 1101 | $this->dm = $dm; |
|
| 273 | 1101 | $this->evm = $evm; |
|
| 274 | 1101 | $this->hydratorFactory = $hydratorFactory; |
|
| 275 | 1101 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 276 | 1101 | } |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 280 | * queries for insert persistence. |
||
| 281 | * |
||
| 282 | * @return PersistenceBuilder $pb |
||
| 283 | */ |
||
| 284 | 766 | public function getPersistenceBuilder() |
|
| 285 | { |
||
| 286 | 766 | if ( ! $this->persistenceBuilder) { |
|
| 287 | 766 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 288 | } |
||
| 289 | 766 | return $this->persistenceBuilder; |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sets the parent association for a given embedded document. |
||
| 294 | * |
||
| 295 | * @param object $document |
||
| 296 | * @param array $mapping |
||
| 297 | * @param object $parent |
||
| 298 | * @param string $propertyPath |
||
| 299 | */ |
||
| 300 | 205 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 301 | { |
||
| 302 | 205 | $oid = spl_object_hash($document); |
|
| 303 | 205 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 304 | 205 | $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath); |
|
| 305 | 205 | } |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Gets the parent association for a given embedded document. |
||
| 309 | * |
||
| 310 | * <code> |
||
| 311 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 312 | * </code> |
||
| 313 | * |
||
| 314 | * @param object $document |
||
| 315 | * @return array $association |
||
| 316 | */ |
||
| 317 | 233 | public function getParentAssociation($document) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get the document persister instance for the given document name |
||
| 328 | * |
||
| 329 | * @param string $documentName |
||
| 330 | * @return Persisters\DocumentPersister |
||
| 331 | */ |
||
| 332 | 764 | public function getDocumentPersister($documentName) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the collection persister instance. |
||
| 344 | * |
||
| 345 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 346 | */ |
||
| 347 | 764 | public function getCollectionPersister() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Set the document persister instance to use for the given document name |
||
| 358 | * |
||
| 359 | * @param string $documentName |
||
| 360 | * @param Persisters\DocumentPersister $persister |
||
| 361 | */ |
||
| 362 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 369 | * up to this point. The state of all managed documents will be synchronized with |
||
| 370 | * the database. |
||
| 371 | * |
||
| 372 | * The operations are executed in the following order: |
||
| 373 | * |
||
| 374 | * 1) All document insertions |
||
| 375 | * 2) All document updates |
||
| 376 | * 3) All document deletions |
||
| 377 | * |
||
| 378 | * @param object $document |
||
| 379 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 380 | */ |
||
| 381 | 630 | public function commit($document = null, array $options = array()) |
|
| 382 | { |
||
| 383 | // Raise preFlush |
||
| 384 | 630 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 385 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 386 | } |
||
| 387 | |||
| 388 | // Compute changes done since last commit. |
||
| 389 | 630 | if ($document === null) { |
|
| 390 | 624 | $this->computeChangeSets(); |
|
| 391 | 14 | } elseif (is_object($document)) { |
|
| 392 | 13 | $this->computeSingleDocumentChangeSet($document); |
|
| 393 | 1 | } elseif (is_array($document)) { |
|
| 394 | 1 | foreach ($document as $object) { |
|
| 395 | 1 | $this->computeSingleDocumentChangeSet($object); |
|
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | 628 | if ( ! ($this->documentInsertions || |
|
|
|
|||
| 400 | 264 | $this->documentUpserts || |
|
| 401 | 220 | $this->documentDeletions || |
|
| 402 | 205 | $this->documentUpdates || |
|
| 403 | 25 | $this->collectionUpdates || |
|
| 404 | 25 | $this->collectionDeletions || |
|
| 405 | 25 | $this->orphanRemovals) |
|
| 406 | ) { |
||
| 407 | 25 | return; // Nothing to do. |
|
| 408 | } |
||
| 409 | |||
| 410 | 625 | if ($this->orphanRemovals) { |
|
| 411 | 50 | foreach ($this->orphanRemovals as $removal) { |
|
| 412 | 50 | $this->remove($removal); |
|
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | // Raise onFlush |
||
| 417 | 625 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 418 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 419 | } |
||
| 420 | |||
| 421 | 625 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 422 | 89 | list($class, $documents) = $classAndDocuments; |
|
| 423 | 89 | $this->executeUpserts($class, $documents, $options); |
|
| 424 | } |
||
| 425 | |||
| 426 | 625 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 427 | 547 | list($class, $documents) = $classAndDocuments; |
|
| 428 | 547 | $this->executeInserts($class, $documents, $options); |
|
| 429 | } |
||
| 430 | |||
| 431 | 624 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 432 | 235 | list($class, $documents) = $classAndDocuments; |
|
| 433 | 235 | $this->executeUpdates($class, $documents, $options); |
|
| 434 | } |
||
| 435 | |||
| 436 | 624 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 437 | 72 | list($class, $documents) = $classAndDocuments; |
|
| 438 | 72 | $this->executeDeletions($class, $documents, $options); |
|
| 439 | } |
||
| 440 | |||
| 441 | // Raise postFlush |
||
| 442 | 624 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 443 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 444 | } |
||
| 445 | |||
| 446 | // Clear up |
||
| 447 | 624 | $this->documentInsertions = |
|
| 448 | 624 | $this->documentUpserts = |
|
| 449 | 624 | $this->documentUpdates = |
|
| 450 | 624 | $this->documentDeletions = |
|
| 451 | 624 | $this->documentChangeSets = |
|
| 452 | 624 | $this->collectionUpdates = |
|
| 453 | 624 | $this->collectionDeletions = |
|
| 454 | 624 | $this->visitedCollections = |
|
| 455 | 624 | $this->scheduledForDirtyCheck = |
|
| 456 | 624 | $this->orphanRemovals = |
|
| 457 | 624 | $this->hasScheduledCollections = array(); |
|
| 458 | 624 | } |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Groups a list of scheduled documents by their class. |
||
| 462 | * |
||
| 463 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 464 | * @param bool $includeEmbedded |
||
| 465 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 466 | */ |
||
| 467 | 625 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Compute changesets of all documents scheduled for insertion. |
||
| 499 | * |
||
| 500 | * Embedded documents will not be processed. |
||
| 501 | */ |
||
| 502 | 632 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 511 | |||
| 512 | /** |
||
| 513 | * Compute changesets of all documents scheduled for upsert. |
||
| 514 | * |
||
| 515 | * Embedded documents will not be processed. |
||
| 516 | */ |
||
| 517 | 631 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 526 | |||
| 527 | /** |
||
| 528 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 529 | * |
||
| 530 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 531 | * 2. Proxies are skipped. |
||
| 532 | * 3. Only if document is properly managed. |
||
| 533 | * |
||
| 534 | * @param object $document |
||
| 535 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | 14 | private function computeSingleDocumentChangeSet($document) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Gets the changeset for a document. |
||
| 575 | * |
||
| 576 | * @param object $document |
||
| 577 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 578 | */ |
||
| 579 | 625 | public function getDocumentChangeSet($document) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * INTERNAL: |
||
| 590 | * Sets the changeset for a document. |
||
| 591 | * |
||
| 592 | * @param object $document |
||
| 593 | * @param array $changeset |
||
| 594 | */ |
||
| 595 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 602 | * |
||
| 603 | * @param object $document |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 632 | public function getDocumentActualData($document) |
|
| 607 | { |
||
| 608 | 632 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 609 | 632 | $actualData = array(); |
|
| 610 | 632 | foreach ($class->reflFields as $name => $refProp) { |
|
| 611 | 632 | $mapping = $class->fieldMappings[$name]; |
|
| 612 | // skip not saved fields |
||
| 613 | 632 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 614 | 52 | continue; |
|
| 615 | } |
||
| 616 | 632 | $value = $refProp->getValue($document); |
|
| 617 | 632 | if (isset($mapping['file']) && ! $value instanceof GridFSFile) { |
|
| 618 | 7 | $value = new GridFSFile($value); |
|
| 619 | 7 | $class->reflFields[$name]->setValue($document, $value); |
|
| 620 | 7 | $actualData[$name] = $value; |
|
| 621 | 632 | } elseif ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 622 | 422 | && $value !== null && ! ($value instanceof PersistentCollectionInterface)) { |
|
| 623 | // If $actualData[$name] is not a Collection then use an ArrayCollection. |
||
| 624 | 408 | if ( ! $value instanceof Collection) { |
|
| 625 | 144 | $value = new ArrayCollection($value); |
|
| 626 | } |
||
| 627 | |||
| 628 | // Inject PersistentCollection |
||
| 629 | 408 | $coll = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $mapping, $value); |
|
| 630 | 408 | $coll->setOwner($document, $mapping); |
|
| 631 | 408 | $coll->setDirty( ! $value->isEmpty()); |
|
| 632 | 408 | $class->reflFields[$name]->setValue($document, $coll); |
|
| 633 | 408 | $actualData[$name] = $coll; |
|
| 634 | } else { |
||
| 635 | 632 | $actualData[$name] = $value; |
|
| 636 | } |
||
| 637 | } |
||
| 638 | 632 | return $actualData; |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Computes the changes that happened to a single document. |
||
| 643 | * |
||
| 644 | * Modifies/populates the following properties: |
||
| 645 | * |
||
| 646 | * {@link originalDocumentData} |
||
| 647 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 648 | * then it was not fetched from the database and therefore we have no original |
||
| 649 | * document data yet. All of the current document data is stored as the original document data. |
||
| 650 | * |
||
| 651 | * {@link documentChangeSets} |
||
| 652 | * The changes detected on all properties of the document are stored there. |
||
| 653 | * A change is a tuple array where the first entry is the old value and the second |
||
| 654 | * entry is the new value of the property. Changesets are used by persisters |
||
| 655 | * to INSERT/UPDATE the persistent document state. |
||
| 656 | * |
||
| 657 | * {@link documentUpdates} |
||
| 658 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 659 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 660 | * there to mark it for an update. |
||
| 661 | * |
||
| 662 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 663 | * @param object $document The document for which to compute the changes. |
||
| 664 | */ |
||
| 665 | 629 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 681 | * |
||
| 682 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 683 | * @param object $document |
||
| 684 | * @param boolean $recompute |
||
| 685 | */ |
||
| 686 | 629 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Computes all the changes that have been done to documents and collections |
||
| 860 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 861 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 862 | */ |
||
| 863 | 627 | public function computeChangeSets() |
|
| 913 | |||
| 914 | /** |
||
| 915 | * Computes the changes of an association. |
||
| 916 | * |
||
| 917 | * @param object $parentDocument |
||
| 918 | * @param array $assoc |
||
| 919 | * @param mixed $value The value of the association. |
||
| 920 | * @throws \InvalidArgumentException |
||
| 921 | */ |
||
| 922 | 467 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 923 | { |
||
| 924 | 467 | $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]); |
|
| 925 | 467 | $class = $this->dm->getClassMetadata(get_class($parentDocument)); |
|
| 926 | 467 | $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); |
|
| 927 | |||
| 928 | 467 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
| 929 | 8 | return; |
|
| 930 | } |
||
| 931 | |||
| 932 | 466 | if ($value instanceof PersistentCollectionInterface && $value->isDirty() && ($assoc['isOwningSide'] || isset($assoc['embedded']))) { |
|
| 933 | 258 | if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) { |
|
| 934 | 254 | $this->scheduleCollectionUpdate($value); |
|
| 935 | } |
||
| 936 | 258 | $topmostOwner = $this->getOwningDocument($value->getOwner()); |
|
| 937 | 258 | $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value; |
|
| 938 | 258 | if ( ! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) { |
|
| 939 | 147 | $value->initialize(); |
|
| 940 | 147 | foreach ($value->getDeletedDocuments() as $orphan) { |
|
| 941 | 23 | $this->scheduleOrphanRemoval($orphan); |
|
| 942 | } |
||
| 943 | } |
||
| 944 | } |
||
| 945 | |||
| 946 | // Look through the documents, and in any of their associations, |
||
| 947 | // for transient (new) documents, recursively. ("Persistence by reachability") |
||
| 948 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 949 | 466 | $unwrappedValue = ($assoc['type'] === ClassMetadata::ONE) ? array($value) : $value->unwrap(); |
|
| 950 | |||
| 951 | 466 | $count = 0; |
|
| 952 | 466 | foreach ($unwrappedValue as $key => $entry) { |
|
| 953 | 371 | if ( ! is_object($entry)) { |
|
| 954 | 1 | throw new \InvalidArgumentException( |
|
| 955 | 1 | sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name']) |
|
| 956 | ); |
||
| 957 | } |
||
| 958 | |||
| 959 | 370 | $targetClass = $this->dm->getClassMetadata(get_class($entry)); |
|
| 960 | |||
| 961 | 370 | $state = $this->getDocumentState($entry, self::STATE_NEW); |
|
| 962 | |||
| 963 | // Handle "set" strategy for multi-level hierarchy |
||
| 964 | 370 | $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key; |
|
| 965 | 370 | $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name']; |
|
| 966 | |||
| 967 | 370 | $count++; |
|
| 968 | |||
| 969 | switch ($state) { |
||
| 970 | 370 | case self::STATE_NEW: |
|
| 971 | 68 | if ( ! $assoc['isCascadePersist']) { |
|
| 972 | throw new \InvalidArgumentException('A new document was found through a relationship that was not' |
||
| 973 | . ' configured to cascade persist operations: ' . $this->objToStr($entry) . '.' |
||
| 974 | . ' Explicitly persist the new document or configure cascading persist operations' |
||
| 975 | . ' on the relationship.'); |
||
| 976 | } |
||
| 977 | |||
| 978 | 68 | $this->persistNew($targetClass, $entry); |
|
| 979 | 68 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 980 | 68 | $this->computeChangeSet($targetClass, $entry); |
|
| 981 | 68 | break; |
|
| 982 | |||
| 983 | 365 | case self::STATE_MANAGED: |
|
| 984 | 365 | if ($targetClass->isEmbeddedDocument) { |
|
| 985 | 178 | list(, $knownParent, ) = $this->getParentAssociation($entry); |
|
| 986 | 178 | if ($knownParent && $knownParent !== $parentDocument) { |
|
| 987 | 9 | $entry = clone $entry; |
|
| 988 | 9 | if ($assoc['type'] === ClassMetadata::ONE) { |
|
| 989 | 6 | $class->setFieldValue($parentDocument, $assoc['fieldName'], $entry); |
|
| 990 | 6 | $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['fieldName'], $entry); |
|
| 991 | 6 | $poid = spl_object_hash($parentDocument); |
|
| 992 | 6 | if (isset($this->documentChangeSets[$poid][$assoc['fieldName']])) { |
|
| 993 | 6 | $this->documentChangeSets[$poid][$assoc['fieldName']][1] = $entry; |
|
| 994 | } |
||
| 995 | } else { |
||
| 996 | // must use unwrapped value to not trigger orphan removal |
||
| 997 | 7 | $unwrappedValue[$key] = $entry; |
|
| 998 | } |
||
| 999 | 9 | $this->persistNew($targetClass, $entry); |
|
| 1000 | } |
||
| 1001 | 178 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 1002 | 178 | $this->computeChangeSet($targetClass, $entry); |
|
| 1003 | } |
||
| 1004 | 365 | break; |
|
| 1005 | |||
| 1006 | 1 | case self::STATE_REMOVED: |
|
| 1007 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 1008 | // and remove the element from Collection. |
||
| 1009 | 1 | if ($assoc['type'] === ClassMetadata::MANY) { |
|
| 1010 | unset($value[$key]); |
||
| 1011 | } |
||
| 1012 | 1 | break; |
|
| 1013 | |||
| 1014 | case self::STATE_DETACHED: |
||
| 1015 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 1016 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 1017 | throw new \InvalidArgumentException('A detached document was found through a ' |
||
| 1018 | . 'relationship during cascading a persist operation.'); |
||
| 1019 | |||
| 1020 | default: |
||
| 1021 | // MANAGED associated documents are already taken into account |
||
| 1022 | // during changeset calculation anyway, since they are in the identity map. |
||
| 1023 | |||
| 1024 | } |
||
| 1025 | } |
||
| 1026 | 465 | } |
|
| 1027 | |||
| 1028 | /** |
||
| 1029 | * INTERNAL: |
||
| 1030 | * Computes the changeset of an individual document, independently of the |
||
| 1031 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1032 | * |
||
| 1033 | * The passed document must be a managed document. If the document already has a change set |
||
| 1034 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1035 | * whereby changes detected in this method prevail. |
||
| 1036 | * |
||
| 1037 | * @ignore |
||
| 1038 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1039 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1040 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1041 | */ |
||
| 1042 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1061 | |||
| 1062 | /** |
||
| 1063 | * @param ClassMetadata $class |
||
| 1064 | * @param object $document |
||
| 1065 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1066 | */ |
||
| 1067 | 664 | private function persistNew(ClassMetadata $class, $document) |
|
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Executes all document insertions for documents of the specified type. |
||
| 1113 | * |
||
| 1114 | * @param ClassMetadata $class |
||
| 1115 | * @param array $documents Array of documents to insert |
||
| 1116 | * @param array $options Array of options to be used with batchInsert() |
||
| 1117 | */ |
||
| 1118 | 547 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Executes all document upserts for documents of the specified type. |
||
| 1136 | * |
||
| 1137 | * @param ClassMetadata $class |
||
| 1138 | * @param array $documents Array of documents to upsert |
||
| 1139 | * @param array $options Array of options to be used with batchInsert() |
||
| 1140 | */ |
||
| 1141 | 89 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Executes all document updates for documents of the specified type. |
||
| 1160 | * |
||
| 1161 | * @param Mapping\ClassMetadata $class |
||
| 1162 | * @param array $documents Array of documents to update |
||
| 1163 | * @param array $options Array of options to be used with update() |
||
| 1164 | */ |
||
| 1165 | 235 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Executes all document deletions for documents of the specified type. |
||
| 1185 | * |
||
| 1186 | * @param ClassMetadata $class |
||
| 1187 | * @param array $documents Array of documents to delete |
||
| 1188 | * @param array $options Array of options to be used with remove() |
||
| 1189 | */ |
||
| 1190 | 72 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Schedules a document for insertion into the database. |
||
| 1225 | * If the document already has an identifier, it will be added to the |
||
| 1226 | * identity map. |
||
| 1227 | * |
||
| 1228 | * @param ClassMetadata $class |
||
| 1229 | * @param object $document The document to schedule for insertion. |
||
| 1230 | * @throws \InvalidArgumentException |
||
| 1231 | */ |
||
| 1232 | 591 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Schedules a document for upsert into the database and adds it to the |
||
| 1255 | * identity map |
||
| 1256 | * |
||
| 1257 | * @param ClassMetadata $class |
||
| 1258 | * @param object $document The document to schedule for upsert. |
||
| 1259 | * @throws \InvalidArgumentException |
||
| 1260 | */ |
||
| 1261 | 96 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Checks whether a document is scheduled for insertion. |
||
| 1285 | * |
||
| 1286 | * @param object $document |
||
| 1287 | * @return boolean |
||
| 1288 | */ |
||
| 1289 | 108 | public function isScheduledForInsert($document) |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * Checks whether a document is scheduled for upsert. |
||
| 1296 | * |
||
| 1297 | * @param object $document |
||
| 1298 | * @return boolean |
||
| 1299 | */ |
||
| 1300 | 5 | public function isScheduledForUpsert($document) |
|
| 1304 | |||
| 1305 | /** |
||
| 1306 | * Schedules a document for being updated. |
||
| 1307 | * |
||
| 1308 | * @param object $document The document to schedule for being updated. |
||
| 1309 | * @throws \InvalidArgumentException |
||
| 1310 | */ |
||
| 1311 | 244 | public function scheduleForUpdate($document) |
|
| 1328 | |||
| 1329 | /** |
||
| 1330 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1331 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1332 | * at commit time. |
||
| 1333 | * |
||
| 1334 | * @param object $document |
||
| 1335 | * @return boolean |
||
| 1336 | */ |
||
| 1337 | 22 | public function isScheduledForUpdate($document) |
|
| 1341 | |||
| 1342 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1347 | |||
| 1348 | /** |
||
| 1349 | * INTERNAL: |
||
| 1350 | * Schedules a document for deletion. |
||
| 1351 | * |
||
| 1352 | * @param object $document |
||
| 1353 | */ |
||
| 1354 | 77 | public function scheduleForDelete($document) |
|
| 1380 | |||
| 1381 | /** |
||
| 1382 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1383 | * of work. |
||
| 1384 | * |
||
| 1385 | * @param object $document |
||
| 1386 | * @return boolean |
||
| 1387 | */ |
||
| 1388 | 8 | public function isScheduledForDelete($document) |
|
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1395 | * |
||
| 1396 | * @param $document |
||
| 1397 | * @return boolean |
||
| 1398 | */ |
||
| 1399 | 257 | public function isDocumentScheduled($document) |
|
| 1407 | |||
| 1408 | /** |
||
| 1409 | * INTERNAL: |
||
| 1410 | * Registers a document in the identity map. |
||
| 1411 | * |
||
| 1412 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1413 | * the root document. Identifiers are serialized before being used as array |
||
| 1414 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1415 | * |
||
| 1416 | * @ignore |
||
| 1417 | * @param object $document The document to register. |
||
| 1418 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1419 | * the document in question is already managed. |
||
| 1420 | */ |
||
| 1421 | 693 | public function addToIdentityMap($document) |
|
| 1439 | |||
| 1440 | /** |
||
| 1441 | * Gets the state of a document with regard to the current unit of work. |
||
| 1442 | * |
||
| 1443 | * @param object $document |
||
| 1444 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1445 | * This parameter can be set to improve performance of document state detection |
||
| 1446 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1447 | * is either known or does not matter for the caller of the method. |
||
| 1448 | * @return int The document state. |
||
| 1449 | */ |
||
| 1450 | 667 | public function getDocumentState($document, $assume = null) |
|
| 1451 | { |
||
| 1452 | 667 | $oid = spl_object_hash($document); |
|
| 1453 | |||
| 1454 | 667 | if (isset($this->documentStates[$oid])) { |
|
| 1455 | 414 | return $this->documentStates[$oid]; |
|
| 1456 | } |
||
| 1457 | |||
| 1458 | 667 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1459 | |||
| 1460 | 667 | if ($class->isEmbeddedDocument) { |
|
| 1461 | 196 | return self::STATE_NEW; |
|
| 1462 | } |
||
| 1463 | |||
| 1464 | 664 | if ($assume !== null) { |
|
| 1465 | 661 | return $assume; |
|
| 1466 | } |
||
| 1467 | |||
| 1468 | /* State can only be NEW or DETACHED, because MANAGED/REMOVED states are |
||
| 1469 | * known. Note that you cannot remember the NEW or DETACHED state in |
||
| 1470 | * _documentStates since the UoW does not hold references to such |
||
| 1471 | * objects and the object hash can be reused. More generally, because |
||
| 1472 | * the state may "change" between NEW/DETACHED without the UoW being |
||
| 1473 | * aware of it. |
||
| 1474 | */ |
||
| 1475 | 4 | $id = $class->getIdentifierObject($document); |
|
| 1476 | |||
| 1477 | 4 | if ($id === null) { |
|
| 1478 | 3 | return self::STATE_NEW; |
|
| 1479 | } |
||
| 1480 | |||
| 1481 | // Check for a version field, if available, to avoid a DB lookup. |
||
| 1482 | 2 | if ($class->isVersioned) { |
|
| 1483 | return $class->getFieldValue($document, $class->versionField) |
||
| 1484 | ? self::STATE_DETACHED |
||
| 1485 | : self::STATE_NEW; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | // Last try before DB lookup: check the identity map. |
||
| 1489 | 2 | if ($this->tryGetById($id, $class)) { |
|
| 1490 | 1 | return self::STATE_DETACHED; |
|
| 1491 | } |
||
| 1492 | |||
| 1493 | // DB lookup |
||
| 1494 | 2 | if ($this->getDocumentPersister($class->name)->exists($document)) { |
|
| 1495 | 1 | return self::STATE_DETACHED; |
|
| 1496 | } |
||
| 1497 | |||
| 1498 | 1 | return self::STATE_NEW; |
|
| 1499 | } |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * INTERNAL: |
||
| 1503 | * Removes a document from the identity map. This effectively detaches the |
||
| 1504 | * document from the persistence management of Doctrine. |
||
| 1505 | * |
||
| 1506 | * @ignore |
||
| 1507 | * @param object $document |
||
| 1508 | * @throws \InvalidArgumentException |
||
| 1509 | * @return boolean |
||
| 1510 | */ |
||
| 1511 | 90 | public function removeFromIdentityMap($document) |
|
| 1531 | |||
| 1532 | /** |
||
| 1533 | * INTERNAL: |
||
| 1534 | * Gets a document in the identity map by its identifier hash. |
||
| 1535 | * |
||
| 1536 | * @ignore |
||
| 1537 | * @param mixed $id Document identifier |
||
| 1538 | * @param ClassMetadata $class Document class |
||
| 1539 | * @return object |
||
| 1540 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1541 | */ |
||
| 1542 | 34 | public function getById($id, ClassMetadata $class) |
|
| 1552 | |||
| 1553 | /** |
||
| 1554 | * INTERNAL: |
||
| 1555 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1556 | * for the given hash, FALSE is returned. |
||
| 1557 | * |
||
| 1558 | * @ignore |
||
| 1559 | * @param mixed $id Document identifier |
||
| 1560 | * @param ClassMetadata $class Document class |
||
| 1561 | * @return mixed The found document or FALSE. |
||
| 1562 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1563 | */ |
||
| 1564 | 312 | public function tryGetById($id, ClassMetadata $class) |
|
| 1575 | |||
| 1576 | /** |
||
| 1577 | * Schedules a document for dirty-checking at commit-time. |
||
| 1578 | * |
||
| 1579 | * @param object $document The document to schedule for dirty-checking. |
||
| 1580 | * @todo Rename: scheduleForSynchronization |
||
| 1581 | */ |
||
| 1582 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1587 | |||
| 1588 | /** |
||
| 1589 | * Checks whether a document is registered in the identity map. |
||
| 1590 | * |
||
| 1591 | * @param object $document |
||
| 1592 | * @return boolean |
||
| 1593 | */ |
||
| 1594 | 88 | public function isInIdentityMap($document) |
|
| 1607 | |||
| 1608 | /** |
||
| 1609 | * @param object $document |
||
| 1610 | * @return string |
||
| 1611 | */ |
||
| 1612 | 693 | private function getIdForIdentityMap($document) |
|
| 1625 | |||
| 1626 | /** |
||
| 1627 | * INTERNAL: |
||
| 1628 | * Checks whether an identifier exists in the identity map. |
||
| 1629 | * |
||
| 1630 | * @ignore |
||
| 1631 | * @param string $id |
||
| 1632 | * @param string $rootClassName |
||
| 1633 | * @return boolean |
||
| 1634 | */ |
||
| 1635 | public function containsId($id, $rootClassName) |
||
| 1639 | |||
| 1640 | /** |
||
| 1641 | * Persists a document as part of the current unit of work. |
||
| 1642 | * |
||
| 1643 | * @param object $document The document to persist. |
||
| 1644 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1645 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1646 | */ |
||
| 1647 | 661 | public function persist($document) |
|
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Saves a document as part of the current unit of work. |
||
| 1659 | * This method is internally called during save() cascades as it tracks |
||
| 1660 | * the already visited documents to prevent infinite recursions. |
||
| 1661 | * |
||
| 1662 | * NOTE: This method always considers documents that are not yet known to |
||
| 1663 | * this UnitOfWork as NEW. |
||
| 1664 | * |
||
| 1665 | * @param object $document The document to persist. |
||
| 1666 | * @param array $visited The already visited documents. |
||
| 1667 | * @throws \InvalidArgumentException |
||
| 1668 | * @throws MongoDBException |
||
| 1669 | */ |
||
| 1670 | 660 | private function doPersist($document, array &$visited) |
|
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Deletes a document as part of the current unit of work. |
||
| 1713 | * |
||
| 1714 | * @param object $document The document to remove. |
||
| 1715 | */ |
||
| 1716 | 76 | public function remove($document) |
|
| 1721 | |||
| 1722 | /** |
||
| 1723 | * Deletes a document as part of the current unit of work. |
||
| 1724 | * |
||
| 1725 | * This method is internally called during delete() cascades as it tracks |
||
| 1726 | * the already visited documents to prevent infinite recursions. |
||
| 1727 | * |
||
| 1728 | * @param object $document The document to delete. |
||
| 1729 | * @param array $visited The map of the already visited documents. |
||
| 1730 | * @throws MongoDBException |
||
| 1731 | */ |
||
| 1732 | 76 | private function doRemove($document, array &$visited) |
|
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1767 | * |
||
| 1768 | * @param object $document |
||
| 1769 | * @return object The managed copy of the document. |
||
| 1770 | */ |
||
| 1771 | 15 | public function merge($document) |
|
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Executes a merge operation on a document. |
||
| 1780 | * |
||
| 1781 | * @param object $document |
||
| 1782 | * @param array $visited |
||
| 1783 | * @param object|null $prevManagedCopy |
||
| 1784 | * @param array|null $assoc |
||
| 1785 | * |
||
| 1786 | * @return object The managed copy of the document. |
||
| 1787 | * |
||
| 1788 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1789 | * @throws LockException If the document uses optimistic locking through a |
||
| 1790 | * version attribute and the version check against the |
||
| 1791 | * managed copy fails. |
||
| 1792 | */ |
||
| 1793 | 15 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1794 | { |
||
| 1795 | 15 | $oid = spl_object_hash($document); |
|
| 1796 | |||
| 1797 | 15 | if (isset($visited[$oid])) { |
|
| 1798 | 1 | return $visited[$oid]; // Prevent infinite recursion |
|
| 1799 | } |
||
| 1800 | |||
| 1801 | 15 | $visited[$oid] = $document; // mark visited |
|
| 1802 | |||
| 1803 | 15 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1804 | |||
| 1805 | /* First we assume DETACHED, although it can still be NEW but we can |
||
| 1806 | * avoid an extra DB round trip this way. If it is not MANAGED but has |
||
| 1807 | * an identity, we need to fetch it from the DB anyway in order to |
||
| 1808 | * merge. MANAGED documents are ignored by the merge operation. |
||
| 1809 | */ |
||
| 1810 | 15 | $managedCopy = $document; |
|
| 1811 | |||
| 1812 | 15 | if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
|
| 1813 | 15 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 1814 | $document->__load(); |
||
| 1815 | } |
||
| 1816 | |||
| 1817 | 15 | $identifier = $class->getIdentifier(); |
|
| 1818 | // We always have one element in the identifier array but it might be null |
||
| 1819 | 15 | $id = $identifier[0] !== null ? $class->getIdentifierObject($document) : null; |
|
| 1820 | 15 | $managedCopy = null; |
|
| 1821 | |||
| 1822 | // Try to fetch document from the database |
||
| 1823 | 15 | if (! $class->isEmbeddedDocument && $id !== null) { |
|
| 1824 | 12 | $managedCopy = $this->dm->find($class->name, $id); |
|
| 1825 | |||
| 1826 | // Managed copy may be removed in which case we can't merge |
||
| 1827 | 12 | if ($managedCopy && $this->getDocumentState($managedCopy) === self::STATE_REMOVED) { |
|
| 1828 | throw new \InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.'); |
||
| 1829 | } |
||
| 1830 | |||
| 1831 | 12 | if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) { |
|
| 1832 | $managedCopy->__load(); |
||
| 1833 | } |
||
| 1834 | } |
||
| 1835 | |||
| 1836 | 15 | if ($managedCopy === null) { |
|
| 1837 | // Create a new managed instance |
||
| 1838 | 7 | $managedCopy = $class->newInstance(); |
|
| 1839 | 7 | if ($id !== null) { |
|
| 1840 | 3 | $class->setIdentifierValue($managedCopy, $id); |
|
| 1841 | } |
||
| 1842 | 7 | $this->persistNew($class, $managedCopy); |
|
| 1843 | } |
||
| 1844 | |||
| 1845 | 15 | if ($class->isVersioned) { |
|
| 1846 | $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); |
||
| 1847 | $documentVersion = $class->reflFields[$class->versionField]->getValue($document); |
||
| 1848 | |||
| 1849 | // Throw exception if versions don't match |
||
| 1850 | if ($managedCopyVersion != $documentVersion) { |
||
| 1851 | throw LockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion); |
||
| 1852 | } |
||
| 1853 | } |
||
| 1854 | |||
| 1855 | // Merge state of $document into existing (managed) document |
||
| 1856 | 15 | foreach ($class->reflClass->getProperties() as $prop) { |
|
| 1857 | 15 | $name = $prop->name; |
|
| 1858 | 15 | $prop->setAccessible(true); |
|
| 1859 | 15 | if ( ! isset($class->associationMappings[$name])) { |
|
| 1860 | 15 | if ( ! $class->isIdentifier($name)) { |
|
| 1861 | 15 | $prop->setValue($managedCopy, $prop->getValue($document)); |
|
| 1862 | } |
||
| 1863 | } else { |
||
| 1864 | 15 | $assoc2 = $class->associationMappings[$name]; |
|
| 1865 | |||
| 1866 | 15 | if ($assoc2['type'] === 'one') { |
|
| 1867 | 7 | $other = $prop->getValue($document); |
|
| 1868 | |||
| 1869 | 7 | if ($other === null) { |
|
| 1870 | 2 | $prop->setValue($managedCopy, null); |
|
| 1871 | 6 | } elseif ($other instanceof Proxy && ! $other->__isInitialized__) { |
|
| 1872 | // Do not merge fields marked lazy that have not been fetched |
||
| 1873 | 1 | continue; |
|
| 1874 | 5 | } elseif ( ! $assoc2['isCascadeMerge']) { |
|
| 1875 | if ($this->getDocumentState($other) === self::STATE_DETACHED) { |
||
| 1876 | $targetDocument = isset($assoc2['targetDocument']) ? $assoc2['targetDocument'] : get_class($other); |
||
| 1877 | /* @var $targetClass \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */ |
||
| 1878 | $targetClass = $this->dm->getClassMetadata($targetDocument); |
||
| 1879 | $relatedId = $targetClass->getIdentifierObject($other); |
||
| 1880 | |||
| 1881 | if ($targetClass->subClasses) { |
||
| 1882 | $other = $this->dm->find($targetClass->name, $relatedId); |
||
| 1883 | } else { |
||
| 1884 | $other = $this |
||
| 1885 | ->dm |
||
| 1886 | ->getProxyFactory() |
||
| 1887 | ->getProxy($assoc2['targetDocument'], array($targetClass->identifier => $relatedId)); |
||
| 1888 | $this->registerManaged($other, $relatedId, array()); |
||
| 1889 | } |
||
| 1890 | } |
||
| 1891 | |||
| 1892 | $prop->setValue($managedCopy, $other); |
||
| 1893 | } |
||
| 1894 | } else { |
||
| 1895 | 12 | $mergeCol = $prop->getValue($document); |
|
| 1896 | |||
| 1897 | 12 | if ($mergeCol instanceof PersistentCollectionInterface && ! $mergeCol->isInitialized() && ! $assoc2['isCascadeMerge']) { |
|
| 1898 | /* Do not merge fields marked lazy that have not |
||
| 1899 | * been fetched. Keep the lazy persistent collection |
||
| 1900 | * of the managed copy. |
||
| 1901 | */ |
||
| 1902 | 3 | continue; |
|
| 1903 | } |
||
| 1904 | |||
| 1905 | 12 | $managedCol = $prop->getValue($managedCopy); |
|
| 1906 | |||
| 1907 | 12 | if ( ! $managedCol) { |
|
| 1908 | 3 | $managedCol = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $assoc2, null); |
|
| 1909 | 3 | $managedCol->setOwner($managedCopy, $assoc2); |
|
| 1910 | 3 | $prop->setValue($managedCopy, $managedCol); |
|
| 1911 | 3 | $this->originalDocumentData[$oid][$name] = $managedCol; |
|
| 1912 | } |
||
| 1913 | |||
| 1914 | /* Note: do not process association's target documents. |
||
| 1915 | * They will be handled during the cascade. Initialize |
||
| 1916 | * and, if necessary, clear $managedCol for now. |
||
| 1917 | */ |
||
| 1918 | 12 | if ($assoc2['isCascadeMerge']) { |
|
| 1919 | 12 | $managedCol->initialize(); |
|
| 1920 | |||
| 1921 | // If $managedCol differs from the merged collection, clear and set dirty |
||
| 1922 | 12 | if ( ! $managedCol->isEmpty() && $managedCol !== $mergeCol) { |
|
| 1923 | 3 | $managedCol->unwrap()->clear(); |
|
| 1924 | 3 | $managedCol->setDirty(true); |
|
| 1925 | |||
| 1926 | 3 | if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) { |
|
| 1927 | $this->scheduleForDirtyCheck($managedCopy); |
||
| 1928 | } |
||
| 1929 | } |
||
| 1930 | } |
||
| 1931 | } |
||
| 1932 | } |
||
| 1933 | |||
| 1934 | 15 | if ($class->isChangeTrackingNotify()) { |
|
| 1935 | // Just treat all properties as changed, there is no other choice. |
||
| 1936 | $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); |
||
| 1937 | } |
||
| 1938 | } |
||
| 1939 | |||
| 1940 | 15 | if ($class->isChangeTrackingDeferredExplicit()) { |
|
| 1941 | $this->scheduleForDirtyCheck($document); |
||
| 1942 | } |
||
| 1943 | } |
||
| 1944 | |||
| 1945 | 15 | if ($prevManagedCopy !== null) { |
|
| 1946 | 8 | $assocField = $assoc['fieldName']; |
|
| 1947 | 8 | $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); |
|
| 1948 | |||
| 1949 | 8 | if ($assoc['type'] === 'one') { |
|
| 1950 | 4 | $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); |
|
| 1951 | } else { |
||
| 1952 | 6 | $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy); |
|
| 1953 | |||
| 1954 | 6 | if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) { |
|
| 1955 | 1 | $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy); |
|
| 1956 | } |
||
| 1957 | } |
||
| 1958 | } |
||
| 1959 | |||
| 1960 | // Mark the managed copy visited as well |
||
| 1961 | 15 | $visited[spl_object_hash($managedCopy)] = true; |
|
| 1962 | |||
| 1963 | 15 | $this->cascadeMerge($document, $managedCopy, $visited); |
|
| 1964 | |||
| 1965 | 15 | return $managedCopy; |
|
| 1966 | } |
||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Detaches a document from the persistence management. It's persistence will |
||
| 1970 | * no longer be managed by Doctrine. |
||
| 1971 | * |
||
| 1972 | * @param object $document The document to detach. |
||
| 1973 | */ |
||
| 1974 | 12 | public function detach($document) |
|
| 1979 | |||
| 1980 | /** |
||
| 1981 | * Executes a detach operation on the given document. |
||
| 1982 | * |
||
| 1983 | * @param object $document |
||
| 1984 | * @param array $visited |
||
| 1985 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1986 | */ |
||
| 1987 | 17 | private function doDetach($document, array &$visited) |
|
| 2012 | |||
| 2013 | /** |
||
| 2014 | * Refreshes the state of the given document from the database, overwriting |
||
| 2015 | * any local, unpersisted changes. |
||
| 2016 | * |
||
| 2017 | * @param object $document The document to refresh. |
||
| 2018 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2019 | */ |
||
| 2020 | 23 | public function refresh($document) |
|
| 2025 | |||
| 2026 | /** |
||
| 2027 | * Executes a refresh operation on a document. |
||
| 2028 | * |
||
| 2029 | * @param object $document The document to refresh. |
||
| 2030 | * @param array $visited The already visited documents during cascades. |
||
| 2031 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2032 | */ |
||
| 2033 | 23 | private function doRefresh($document, array &$visited) |
|
| 2055 | |||
| 2056 | /** |
||
| 2057 | * Cascades a refresh operation to associated documents. |
||
| 2058 | * |
||
| 2059 | * @param object $document |
||
| 2060 | * @param array $visited |
||
| 2061 | */ |
||
| 2062 | 22 | private function cascadeRefresh($document, array &$visited) |
|
| 2086 | |||
| 2087 | /** |
||
| 2088 | * Cascades a detach operation to associated documents. |
||
| 2089 | * |
||
| 2090 | * @param object $document |
||
| 2091 | * @param array $visited |
||
| 2092 | */ |
||
| 2093 | 17 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2114 | /** |
||
| 2115 | * Cascades a merge operation to associated documents. |
||
| 2116 | * |
||
| 2117 | * @param object $document |
||
| 2118 | * @param object $managedCopy |
||
| 2119 | * @param array $visited |
||
| 2120 | */ |
||
| 2121 | 15 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2147 | |||
| 2148 | /** |
||
| 2149 | * Cascades the save operation to associated documents. |
||
| 2150 | * |
||
| 2151 | * @param object $document |
||
| 2152 | * @param array $visited |
||
| 2153 | */ |
||
| 2154 | 658 | private function cascadePersist($document, array &$visited) |
|
| 2201 | |||
| 2202 | /** |
||
| 2203 | * Cascades the delete operation to associated documents. |
||
| 2204 | * |
||
| 2205 | * @param object $document |
||
| 2206 | * @param array $visited |
||
| 2207 | */ |
||
| 2208 | 76 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2230 | |||
| 2231 | /** |
||
| 2232 | * Acquire a lock on the given document. |
||
| 2233 | * |
||
| 2234 | * @param object $document |
||
| 2235 | * @param int $lockMode |
||
| 2236 | * @param int $lockVersion |
||
| 2237 | * @throws LockException |
||
| 2238 | * @throws \InvalidArgumentException |
||
| 2239 | */ |
||
| 2240 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2264 | |||
| 2265 | /** |
||
| 2266 | * Releases a lock on the given document. |
||
| 2267 | * |
||
| 2268 | * @param object $document |
||
| 2269 | * @throws \InvalidArgumentException |
||
| 2270 | */ |
||
| 2271 | 1 | public function unlock($document) |
|
| 2279 | |||
| 2280 | /** |
||
| 2281 | * Clears the UnitOfWork. |
||
| 2282 | * |
||
| 2283 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2284 | */ |
||
| 2285 | 418 | public function clear($documentName = null) |
|
| 2319 | |||
| 2320 | /** |
||
| 2321 | * INTERNAL: |
||
| 2322 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2323 | * invoked on that document at the beginning of the next commit of this |
||
| 2324 | * UnitOfWork. |
||
| 2325 | * |
||
| 2326 | * @ignore |
||
| 2327 | * @param object $document |
||
| 2328 | */ |
||
| 2329 | 53 | public function scheduleOrphanRemoval($document) |
|
| 2333 | |||
| 2334 | /** |
||
| 2335 | * INTERNAL: |
||
| 2336 | * Unschedules an embedded or referenced object for removal. |
||
| 2337 | * |
||
| 2338 | * @ignore |
||
| 2339 | * @param object $document |
||
| 2340 | */ |
||
| 2341 | 114 | public function unscheduleOrphanRemoval($document) |
|
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2351 | * 1) sets owner if it was cloned |
||
| 2352 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2353 | * 3) NOP if state is OK |
||
| 2354 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2355 | * |
||
| 2356 | * @param PersistentCollectionInterface $coll |
||
| 2357 | * @param object $document |
||
| 2358 | * @param ClassMetadata $class |
||
| 2359 | * @param string $propName |
||
| 2360 | * @return PersistentCollectionInterface |
||
| 2361 | */ |
||
| 2362 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2382 | |||
| 2383 | /** |
||
| 2384 | * INTERNAL: |
||
| 2385 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2386 | * |
||
| 2387 | * @param PersistentCollectionInterface $coll |
||
| 2388 | */ |
||
| 2389 | 43 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2398 | |||
| 2399 | /** |
||
| 2400 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2401 | * |
||
| 2402 | * @param PersistentCollectionInterface $coll |
||
| 2403 | * @return boolean |
||
| 2404 | */ |
||
| 2405 | 220 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2409 | |||
| 2410 | /** |
||
| 2411 | * INTERNAL: |
||
| 2412 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2413 | * |
||
| 2414 | * @param PersistentCollectionInterface $coll |
||
| 2415 | */ |
||
| 2416 | 232 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2425 | |||
| 2426 | /** |
||
| 2427 | * INTERNAL: |
||
| 2428 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2429 | * |
||
| 2430 | * @param PersistentCollectionInterface $coll |
||
| 2431 | */ |
||
| 2432 | 254 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2447 | |||
| 2448 | /** |
||
| 2449 | * INTERNAL: |
||
| 2450 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2451 | * |
||
| 2452 | * @param PersistentCollectionInterface $coll |
||
| 2453 | */ |
||
| 2454 | 232 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2463 | |||
| 2464 | /** |
||
| 2465 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2466 | * |
||
| 2467 | * @param PersistentCollectionInterface $coll |
||
| 2468 | * @return boolean |
||
| 2469 | */ |
||
| 2470 | 133 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2474 | |||
| 2475 | /** |
||
| 2476 | * INTERNAL: |
||
| 2477 | * Gets PersistentCollections that have been visited during computing change |
||
| 2478 | * set of $document |
||
| 2479 | * |
||
| 2480 | * @param object $document |
||
| 2481 | * @return PersistentCollectionInterface[] |
||
| 2482 | */ |
||
| 2483 | 610 | public function getVisitedCollections($document) |
|
| 2490 | |||
| 2491 | /** |
||
| 2492 | * INTERNAL: |
||
| 2493 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2494 | * |
||
| 2495 | * @param object $document |
||
| 2496 | * @return array |
||
| 2497 | */ |
||
| 2498 | 610 | public function getScheduledCollections($document) |
|
| 2505 | |||
| 2506 | /** |
||
| 2507 | * Checks whether the document is related to a PersistentCollection |
||
| 2508 | * scheduled for update or deletion. |
||
| 2509 | * |
||
| 2510 | * @param object $document |
||
| 2511 | * @return boolean |
||
| 2512 | */ |
||
| 2513 | 52 | public function hasScheduledCollections($document) |
|
| 2517 | |||
| 2518 | /** |
||
| 2519 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2520 | * a collection scheduled for update or deletion. |
||
| 2521 | * |
||
| 2522 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2523 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2524 | * |
||
| 2525 | * If the collection is nested within atomic collection, it is immediately |
||
| 2526 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2527 | * calculating update data way easier. |
||
| 2528 | * |
||
| 2529 | * @param PersistentCollectionInterface $coll |
||
| 2530 | */ |
||
| 2531 | 256 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Get the top-most owning document of a given document |
||
| 2557 | * |
||
| 2558 | * If a top-level document is provided, that same document will be returned. |
||
| 2559 | * For an embedded document, we will walk through parent associations until |
||
| 2560 | * we find a top-level document. |
||
| 2561 | * |
||
| 2562 | * @param object $document |
||
| 2563 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2564 | * @return object |
||
| 2565 | */ |
||
| 2566 | 258 | public function getOwningDocument($document) |
|
| 2582 | |||
| 2583 | /** |
||
| 2584 | * Gets the class name for an association (embed or reference) with respect |
||
| 2585 | * to any discriminator value. |
||
| 2586 | * |
||
| 2587 | * @param array $mapping Field mapping for the association |
||
| 2588 | * @param array|null $data Data for the embedded document or reference |
||
| 2589 | * @return string Class name. |
||
| 2590 | */ |
||
| 2591 | 228 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2624 | |||
| 2625 | /** |
||
| 2626 | * INTERNAL: |
||
| 2627 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2628 | * |
||
| 2629 | * @ignore |
||
| 2630 | * @param string $className The name of the document class. |
||
| 2631 | * @param array $data The data for the document. |
||
| 2632 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2633 | * @param object $document The document to be hydrated into in case of creation |
||
| 2634 | * @return object The document instance. |
||
| 2635 | * @internal Highly performance-sensitive method. |
||
| 2636 | */ |
||
| 2637 | 422 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2638 | { |
||
| 2639 | 422 | $class = $this->dm->getClassMetadata($className); |
|
| 2640 | |||
| 2641 | // @TODO figure out how to remove this |
||
| 2642 | 422 | $discriminatorValue = null; |
|
| 2643 | 422 | View Code Duplication | if (isset($class->discriminatorField, $data[$class->discriminatorField])) { |
| 2644 | 19 | $discriminatorValue = $data[$class->discriminatorField]; |
|
| 2645 | } elseif (isset($class->defaultDiscriminatorValue)) { |
||
| 2646 | 2 | $discriminatorValue = $class->defaultDiscriminatorValue; |
|
| 2647 | } |
||
| 2648 | |||
| 2649 | 422 | if ($discriminatorValue !== null) { |
|
| 2650 | 20 | $className = isset($class->discriminatorMap[$discriminatorValue]) |
|
| 2651 | 18 | ? $class->discriminatorMap[$discriminatorValue] |
|
| 2652 | 20 | : $discriminatorValue; |
|
| 2653 | |||
| 2654 | 20 | $class = $this->dm->getClassMetadata($className); |
|
| 2655 | |||
| 2656 | 20 | unset($data[$class->discriminatorField]); |
|
| 2657 | } |
||
| 2658 | |||
| 2659 | 422 | if (! empty($hints[Query::HINT_READ_ONLY])) { |
|
| 2660 | 2 | $document = $class->newInstance(); |
|
| 2661 | 2 | $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2662 | 2 | return $document; |
|
| 2663 | } |
||
| 2664 | |||
| 2665 | 421 | $isManagedObject = false; |
|
| 2666 | 421 | if (! $class->isQueryResultDocument) { |
|
| 2667 | 421 | $id = $class->getDatabaseIdentifierValue($data['_id']); |
|
| 2668 | 421 | $serializedId = serialize($id); |
|
| 2669 | 421 | $isManagedObject = isset($this->identityMap[$class->name][$serializedId]); |
|
| 2670 | } |
||
| 2671 | |||
| 2672 | 421 | if ($isManagedObject) { |
|
| 2673 | 110 | $document = $this->identityMap[$class->name][$serializedId]; |
|
| 2674 | 110 | $oid = spl_object_hash($document); |
|
| 2675 | 110 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 2676 | 14 | $document->__isInitialized__ = true; |
|
| 2677 | 14 | $overrideLocalValues = true; |
|
| 2678 | 14 | if ($document instanceof NotifyPropertyChanged) { |
|
| 2679 | $document->addPropertyChangedListener($this); |
||
| 2680 | } |
||
| 2681 | } else { |
||
| 2682 | 106 | $overrideLocalValues = ! empty($hints[Query::HINT_REFRESH]); |
|
| 2683 | } |
||
| 2684 | 110 | if ($overrideLocalValues) { |
|
| 2685 | 52 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2686 | 52 | $this->originalDocumentData[$oid] = $data; |
|
| 2687 | } |
||
| 2688 | } else { |
||
| 2689 | 380 | if ($document === null) { |
|
| 2690 | 380 | $document = $class->newInstance(); |
|
| 2691 | } |
||
| 2692 | |||
| 2693 | 380 | if (! $class->isQueryResultDocument) { |
|
| 2694 | 379 | $this->registerManaged($document, $id, $data); |
|
| 2695 | 379 | $oid = spl_object_hash($document); |
|
| 2696 | 379 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 2697 | 379 | $this->identityMap[$class->name][$serializedId] = $document; |
|
| 2698 | } |
||
| 2699 | |||
| 2700 | 380 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2701 | |||
| 2702 | 380 | if (! $class->isQueryResultDocument) { |
|
| 2703 | 379 | $this->originalDocumentData[$oid] = $data; |
|
| 2704 | } |
||
| 2705 | } |
||
| 2706 | |||
| 2707 | 421 | return $document; |
|
| 2708 | } |
||
| 2709 | |||
| 2710 | /** |
||
| 2711 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2712 | * |
||
| 2713 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2714 | */ |
||
| 2715 | 173 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2720 | |||
| 2721 | /** |
||
| 2722 | * Gets the identity map of the UnitOfWork. |
||
| 2723 | * |
||
| 2724 | * @return array |
||
| 2725 | */ |
||
| 2726 | public function getIdentityMap() |
||
| 2730 | |||
| 2731 | /** |
||
| 2732 | * Gets the original data of a document. The original data is the data that was |
||
| 2733 | * present at the time the document was reconstituted from the database. |
||
| 2734 | * |
||
| 2735 | * @param object $document |
||
| 2736 | * @return array |
||
| 2737 | */ |
||
| 2738 | 1 | public function getOriginalDocumentData($document) |
|
| 2746 | |||
| 2747 | /** |
||
| 2748 | * @ignore |
||
| 2749 | */ |
||
| 2750 | 56 | public function setOriginalDocumentData($document, array $data) |
|
| 2756 | |||
| 2757 | /** |
||
| 2758 | * INTERNAL: |
||
| 2759 | * Sets a property value of the original data array of a document. |
||
| 2760 | * |
||
| 2761 | * @ignore |
||
| 2762 | * @param string $oid |
||
| 2763 | * @param string $property |
||
| 2764 | * @param mixed $value |
||
| 2765 | */ |
||
| 2766 | 6 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2770 | |||
| 2771 | /** |
||
| 2772 | * Gets the identifier of a document. |
||
| 2773 | * |
||
| 2774 | * @param object $document |
||
| 2775 | * @return mixed The identifier value |
||
| 2776 | */ |
||
| 2777 | 455 | public function getDocumentIdentifier($document) |
|
| 2782 | |||
| 2783 | /** |
||
| 2784 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2785 | * |
||
| 2786 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2787 | */ |
||
| 2788 | public function hasPendingInsertions() |
||
| 2792 | |||
| 2793 | /** |
||
| 2794 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2795 | * number of documents in the identity map. |
||
| 2796 | * |
||
| 2797 | * @return integer |
||
| 2798 | */ |
||
| 2799 | 2 | public function size() |
|
| 2807 | |||
| 2808 | /** |
||
| 2809 | * INTERNAL: |
||
| 2810 | * Registers a document as managed. |
||
| 2811 | * |
||
| 2812 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2813 | * document class. If the class expects its database identifier to be a |
||
| 2814 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2815 | * document identifiers map will become inconsistent with the identity map. |
||
| 2816 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2817 | * conversion and throw an exception if it's inconsistent. |
||
| 2818 | * |
||
| 2819 | * @param object $document The document. |
||
| 2820 | * @param array $id The identifier values. |
||
| 2821 | * @param array $data The original document data. |
||
| 2822 | */ |
||
| 2823 | 402 | public function registerManaged($document, $id, array $data) |
|
| 2838 | |||
| 2839 | /** |
||
| 2840 | * INTERNAL: |
||
| 2841 | * Clears the property changeset of the document with the given OID. |
||
| 2842 | * |
||
| 2843 | * @param string $oid The document's OID. |
||
| 2844 | */ |
||
| 2845 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2849 | |||
| 2850 | /* PropertyChangedListener implementation */ |
||
| 2851 | |||
| 2852 | /** |
||
| 2853 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2854 | * |
||
| 2855 | * @param object $document The document that owns the property. |
||
| 2856 | * @param string $propertyName The name of the property that changed. |
||
| 2857 | * @param mixed $oldValue The old value of the property. |
||
| 2858 | * @param mixed $newValue The new value of the property. |
||
| 2859 | */ |
||
| 2860 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2875 | |||
| 2876 | /** |
||
| 2877 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2878 | * |
||
| 2879 | * @return array |
||
| 2880 | */ |
||
| 2881 | 5 | public function getScheduledDocumentInsertions() |
|
| 2885 | |||
| 2886 | /** |
||
| 2887 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2888 | * |
||
| 2889 | * @return array |
||
| 2890 | */ |
||
| 2891 | 3 | public function getScheduledDocumentUpserts() |
|
| 2895 | |||
| 2896 | /** |
||
| 2897 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2898 | * |
||
| 2899 | * @return array |
||
| 2900 | */ |
||
| 2901 | 3 | public function getScheduledDocumentUpdates() |
|
| 2905 | |||
| 2906 | /** |
||
| 2907 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2908 | * |
||
| 2909 | * @return array |
||
| 2910 | */ |
||
| 2911 | public function getScheduledDocumentDeletions() |
||
| 2915 | |||
| 2916 | /** |
||
| 2917 | * Get the currently scheduled complete collection deletions |
||
| 2918 | * |
||
| 2919 | * @return array |
||
| 2920 | */ |
||
| 2921 | public function getScheduledCollectionDeletions() |
||
| 2925 | |||
| 2926 | /** |
||
| 2927 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2928 | * |
||
| 2929 | * @return array |
||
| 2930 | */ |
||
| 2931 | public function getScheduledCollectionUpdates() |
||
| 2935 | |||
| 2936 | /** |
||
| 2937 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2938 | * |
||
| 2939 | * @param object |
||
| 2940 | * @return void |
||
| 2941 | */ |
||
| 2942 | public function initializeObject($obj) |
||
| 2950 | |||
| 2951 | 1 | private function objToStr($obj) |
|
| 2955 | } |
||
| 2956 |
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.