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 | 1114 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 271 | { |
||
| 272 | 1114 | $this->dm = $dm; |
|
| 273 | 1114 | $this->evm = $evm; |
|
| 274 | 1114 | $this->hydratorFactory = $hydratorFactory; |
|
| 275 | 1114 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 276 | 1114 | } |
|
| 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 | 773 | public function getPersistenceBuilder() |
|
| 285 | { |
||
| 286 | 773 | if ( ! $this->persistenceBuilder) { |
|
| 287 | 773 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 288 | } |
||
| 289 | 773 | 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 | 771 | public function getDocumentPersister($documentName) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the collection persister instance. |
||
| 344 | * |
||
| 345 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 346 | */ |
||
| 347 | 771 | 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 | 637 | public function commit($document = null, array $options = array()) |
|
| 382 | { |
||
| 383 | // Raise preFlush |
||
| 384 | 637 | 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 | 637 | if ($document === null) { |
|
| 390 | 631 | $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 | 635 | if ( ! ($this->documentInsertions || |
|
|
|
|||
| 400 | 269 | $this->documentUpserts || |
|
| 401 | 224 | $this->documentDeletions || |
|
| 402 | 208 | $this->documentUpdates || |
|
| 403 | 26 | $this->collectionUpdates || |
|
| 404 | 26 | $this->collectionDeletions || |
|
| 405 | 635 | $this->orphanRemovals) |
|
| 406 | ) { |
||
| 407 | 26 | return; // Nothing to do. |
|
| 408 | } |
||
| 409 | |||
| 410 | 632 | if ($this->orphanRemovals) { |
|
| 411 | 50 | foreach ($this->orphanRemovals as $removal) { |
|
| 412 | 50 | $this->remove($removal); |
|
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 416 | // Raise onFlush |
||
| 417 | 632 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 418 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 419 | } |
||
| 420 | |||
| 421 | 632 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 422 | 90 | list($class, $documents) = $classAndDocuments; |
|
| 423 | 90 | $this->executeUpserts($class, $documents, $options); |
|
| 424 | } |
||
| 425 | |||
| 426 | 632 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 427 | 551 | list($class, $documents) = $classAndDocuments; |
|
| 428 | 551 | $this->executeInserts($class, $documents, $options); |
|
| 429 | } |
||
| 430 | |||
| 431 | 631 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 432 | 237 | list($class, $documents) = $classAndDocuments; |
|
| 433 | 237 | $this->executeUpdates($class, $documents, $options); |
|
| 434 | } |
||
| 435 | |||
| 436 | 630 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 437 | 73 | list($class, $documents) = $classAndDocuments; |
|
| 438 | 73 | $this->executeDeletions($class, $documents, $options); |
|
| 439 | } |
||
| 440 | |||
| 441 | // Raise postFlush |
||
| 442 | 630 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 443 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 444 | } |
||
| 445 | |||
| 446 | // Clear up |
||
| 447 | 630 | $this->documentInsertions = |
|
| 448 | 630 | $this->documentUpserts = |
|
| 449 | 630 | $this->documentUpdates = |
|
| 450 | 630 | $this->documentDeletions = |
|
| 451 | 630 | $this->documentChangeSets = |
|
| 452 | 630 | $this->collectionUpdates = |
|
| 453 | 630 | $this->collectionDeletions = |
|
| 454 | 630 | $this->visitedCollections = |
|
| 455 | 630 | $this->scheduledForDirtyCheck = |
|
| 456 | 630 | $this->orphanRemovals = |
|
| 457 | 630 | $this->hasScheduledCollections = array(); |
|
| 458 | 630 | } |
|
| 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 | 632 | 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 | 639 | 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 | 638 | 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 | 632 | 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 | 639 | public function getDocumentActualData($document) |
|
| 607 | { |
||
| 608 | 639 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 609 | 639 | $actualData = array(); |
|
| 610 | 639 | foreach ($class->reflFields as $name => $refProp) { |
|
| 611 | 639 | $mapping = $class->fieldMappings[$name]; |
|
| 612 | // skip not saved fields |
||
| 613 | 639 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 614 | 54 | continue; |
|
| 615 | } |
||
| 616 | 639 | $value = $refProp->getValue($document); |
|
| 617 | 639 | 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 | 639 | } elseif ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 622 | 639 | && $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 | 639 | $actualData[$name] = $value; |
|
| 636 | } |
||
| 637 | } |
||
| 638 | 639 | 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 | 636 | 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 | 636 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Computes all the changes that have been done to documents and collections |
||
| 863 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 864 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 865 | */ |
||
| 866 | 634 | public function computeChangeSets() |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Computes the changes of an association. |
||
| 919 | * |
||
| 920 | * @param object $parentDocument |
||
| 921 | * @param array $assoc |
||
| 922 | * @param mixed $value The value of the association. |
||
| 923 | * @throws \InvalidArgumentException |
||
| 924 | */ |
||
| 925 | 467 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * INTERNAL: |
||
| 1033 | * Computes the changeset of an individual document, independently of the |
||
| 1034 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1035 | * |
||
| 1036 | * The passed document must be a managed document. If the document already has a change set |
||
| 1037 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1038 | * whereby changes detected in this method prevail. |
||
| 1039 | * |
||
| 1040 | * @ignore |
||
| 1041 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1042 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1043 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1044 | */ |
||
| 1045 | 21 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1064 | |||
| 1065 | /** |
||
| 1066 | * @param ClassMetadata $class |
||
| 1067 | * @param object $document |
||
| 1068 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1069 | */ |
||
| 1070 | 669 | private function persistNew(ClassMetadata $class, $document) |
|
| 1113 | |||
| 1114 | /** |
||
| 1115 | * Executes all document insertions for documents of the specified type. |
||
| 1116 | * |
||
| 1117 | * @param ClassMetadata $class |
||
| 1118 | * @param array $documents Array of documents to insert |
||
| 1119 | * @param array $options Array of options to be used with batchInsert() |
||
| 1120 | */ |
||
| 1121 | 551 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Executes all document upserts for documents of the specified type. |
||
| 1139 | * |
||
| 1140 | * @param ClassMetadata $class |
||
| 1141 | * @param array $documents Array of documents to upsert |
||
| 1142 | * @param array $options Array of options to be used with batchInsert() |
||
| 1143 | */ |
||
| 1144 | 90 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Executes all document updates for documents of the specified type. |
||
| 1163 | * |
||
| 1164 | * @param Mapping\ClassMetadata $class |
||
| 1165 | * @param array $documents Array of documents to update |
||
| 1166 | * @param array $options Array of options to be used with update() |
||
| 1167 | */ |
||
| 1168 | 237 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Executes all document deletions for documents of the specified type. |
||
| 1192 | * |
||
| 1193 | * @param ClassMetadata $class |
||
| 1194 | * @param array $documents Array of documents to delete |
||
| 1195 | * @param array $options Array of options to be used with remove() |
||
| 1196 | */ |
||
| 1197 | 73 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Schedules a document for insertion into the database. |
||
| 1232 | * If the document already has an identifier, it will be added to the |
||
| 1233 | * identity map. |
||
| 1234 | * |
||
| 1235 | * @param ClassMetadata $class |
||
| 1236 | * @param object $document The document to schedule for insertion. |
||
| 1237 | * @throws \InvalidArgumentException |
||
| 1238 | */ |
||
| 1239 | 595 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1259 | |||
| 1260 | /** |
||
| 1261 | * Schedules a document for upsert into the database and adds it to the |
||
| 1262 | * identity map |
||
| 1263 | * |
||
| 1264 | * @param ClassMetadata $class |
||
| 1265 | * @param object $document The document to schedule for upsert. |
||
| 1266 | * @throws \InvalidArgumentException |
||
| 1267 | */ |
||
| 1268 | 97 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Checks whether a document is scheduled for insertion. |
||
| 1292 | * |
||
| 1293 | * @param object $document |
||
| 1294 | * @return boolean |
||
| 1295 | */ |
||
| 1296 | 108 | public function isScheduledForInsert($document) |
|
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Checks whether a document is scheduled for upsert. |
||
| 1303 | * |
||
| 1304 | * @param object $document |
||
| 1305 | * @return boolean |
||
| 1306 | */ |
||
| 1307 | 5 | public function isScheduledForUpsert($document) |
|
| 1311 | |||
| 1312 | /** |
||
| 1313 | * Schedules a document for being updated. |
||
| 1314 | * |
||
| 1315 | * @param object $document The document to schedule for being updated. |
||
| 1316 | * @throws \InvalidArgumentException |
||
| 1317 | */ |
||
| 1318 | 246 | public function scheduleForUpdate($document) |
|
| 1335 | |||
| 1336 | /** |
||
| 1337 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1338 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1339 | * at commit time. |
||
| 1340 | * |
||
| 1341 | * @param object $document |
||
| 1342 | * @return boolean |
||
| 1343 | */ |
||
| 1344 | 22 | public function isScheduledForUpdate($document) |
|
| 1348 | |||
| 1349 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1354 | |||
| 1355 | /** |
||
| 1356 | * INTERNAL: |
||
| 1357 | * Schedules a document for deletion. |
||
| 1358 | * |
||
| 1359 | * @param object $document |
||
| 1360 | */ |
||
| 1361 | 78 | public function scheduleForDelete($document) |
|
| 1387 | |||
| 1388 | /** |
||
| 1389 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1390 | * of work. |
||
| 1391 | * |
||
| 1392 | * @param object $document |
||
| 1393 | * @return boolean |
||
| 1394 | */ |
||
| 1395 | 8 | public function isScheduledForDelete($document) |
|
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1402 | * |
||
| 1403 | * @param $document |
||
| 1404 | * @return boolean |
||
| 1405 | */ |
||
| 1406 | 257 | public function isDocumentScheduled($document) |
|
| 1414 | |||
| 1415 | /** |
||
| 1416 | * INTERNAL: |
||
| 1417 | * Registers a document in the identity map. |
||
| 1418 | * |
||
| 1419 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1420 | * the root document. Identifiers are serialized before being used as array |
||
| 1421 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1422 | * |
||
| 1423 | * @ignore |
||
| 1424 | * @param object $document The document to register. |
||
| 1425 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1426 | * the document in question is already managed. |
||
| 1427 | */ |
||
| 1428 | 700 | public function addToIdentityMap($document) |
|
| 1446 | |||
| 1447 | /** |
||
| 1448 | * Gets the state of a document with regard to the current unit of work. |
||
| 1449 | * |
||
| 1450 | * @param object $document |
||
| 1451 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1452 | * This parameter can be set to improve performance of document state detection |
||
| 1453 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1454 | * is either known or does not matter for the caller of the method. |
||
| 1455 | * @return int The document state. |
||
| 1456 | */ |
||
| 1457 | 674 | public function getDocumentState($document, $assume = null) |
|
| 1507 | |||
| 1508 | /** |
||
| 1509 | * INTERNAL: |
||
| 1510 | * Removes a document from the identity map. This effectively detaches the |
||
| 1511 | * document from the persistence management of Doctrine. |
||
| 1512 | * |
||
| 1513 | * @ignore |
||
| 1514 | * @param object $document |
||
| 1515 | * @throws \InvalidArgumentException |
||
| 1516 | * @return boolean |
||
| 1517 | */ |
||
| 1518 | 91 | public function removeFromIdentityMap($document) |
|
| 1538 | |||
| 1539 | /** |
||
| 1540 | * INTERNAL: |
||
| 1541 | * Gets a document in the identity map by its identifier hash. |
||
| 1542 | * |
||
| 1543 | * @ignore |
||
| 1544 | * @param mixed $id Document identifier |
||
| 1545 | * @param ClassMetadata $class Document class |
||
| 1546 | * @return object |
||
| 1547 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1548 | */ |
||
| 1549 | 34 | public function getById($id, ClassMetadata $class) |
|
| 1559 | |||
| 1560 | /** |
||
| 1561 | * INTERNAL: |
||
| 1562 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1563 | * for the given hash, FALSE is returned. |
||
| 1564 | * |
||
| 1565 | * @ignore |
||
| 1566 | * @param mixed $id Document identifier |
||
| 1567 | * @param ClassMetadata $class Document class |
||
| 1568 | * @return mixed The found document or FALSE. |
||
| 1569 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1570 | */ |
||
| 1571 | 316 | public function tryGetById($id, ClassMetadata $class) |
|
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Schedules a document for dirty-checking at commit-time. |
||
| 1585 | * |
||
| 1586 | * @param object $document The document to schedule for dirty-checking. |
||
| 1587 | * @todo Rename: scheduleForSynchronization |
||
| 1588 | */ |
||
| 1589 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1594 | |||
| 1595 | /** |
||
| 1596 | * Checks whether a document is registered in the identity map. |
||
| 1597 | * |
||
| 1598 | * @param object $document |
||
| 1599 | * @return boolean |
||
| 1600 | */ |
||
| 1601 | 89 | public function isInIdentityMap($document) |
|
| 1614 | |||
| 1615 | /** |
||
| 1616 | * @param object $document |
||
| 1617 | * @return string |
||
| 1618 | */ |
||
| 1619 | 700 | private function getIdForIdentityMap($document) |
|
| 1632 | |||
| 1633 | /** |
||
| 1634 | * INTERNAL: |
||
| 1635 | * Checks whether an identifier exists in the identity map. |
||
| 1636 | * |
||
| 1637 | * @ignore |
||
| 1638 | * @param string $id |
||
| 1639 | * @param string $rootClassName |
||
| 1640 | * @return boolean |
||
| 1641 | */ |
||
| 1642 | public function containsId($id, $rootClassName) |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Persists a document as part of the current unit of work. |
||
| 1649 | * |
||
| 1650 | * @param object $document The document to persist. |
||
| 1651 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1652 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1653 | */ |
||
| 1654 | 668 | public function persist($document) |
|
| 1663 | |||
| 1664 | /** |
||
| 1665 | * Saves a document as part of the current unit of work. |
||
| 1666 | * This method is internally called during save() cascades as it tracks |
||
| 1667 | * the already visited documents to prevent infinite recursions. |
||
| 1668 | * |
||
| 1669 | * NOTE: This method always considers documents that are not yet known to |
||
| 1670 | * this UnitOfWork as NEW. |
||
| 1671 | * |
||
| 1672 | * @param object $document The document to persist. |
||
| 1673 | * @param array $visited The already visited documents. |
||
| 1674 | * @throws \InvalidArgumentException |
||
| 1675 | * @throws MongoDBException |
||
| 1676 | */ |
||
| 1677 | 667 | private function doPersist($document, array &$visited) |
|
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Deletes a document as part of the current unit of work. |
||
| 1720 | * |
||
| 1721 | * @param object $document The document to remove. |
||
| 1722 | */ |
||
| 1723 | 77 | public function remove($document) |
|
| 1728 | |||
| 1729 | /** |
||
| 1730 | * Deletes a document as part of the current unit of work. |
||
| 1731 | * |
||
| 1732 | * This method is internally called during delete() cascades as it tracks |
||
| 1733 | * the already visited documents to prevent infinite recursions. |
||
| 1734 | * |
||
| 1735 | * @param object $document The document to delete. |
||
| 1736 | * @param array $visited The map of the already visited documents. |
||
| 1737 | * @throws MongoDBException |
||
| 1738 | */ |
||
| 1739 | 77 | private function doRemove($document, array &$visited) |
|
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1774 | * |
||
| 1775 | * @param object $document |
||
| 1776 | * @return object The managed copy of the document. |
||
| 1777 | */ |
||
| 1778 | 15 | public function merge($document) |
|
| 1784 | |||
| 1785 | /** |
||
| 1786 | * Executes a merge operation on a document. |
||
| 1787 | * |
||
| 1788 | * @param object $document |
||
| 1789 | * @param array $visited |
||
| 1790 | * @param object|null $prevManagedCopy |
||
| 1791 | * @param array|null $assoc |
||
| 1792 | * |
||
| 1793 | * @return object The managed copy of the document. |
||
| 1794 | * |
||
| 1795 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1796 | * @throws LockException If the document uses optimistic locking through a |
||
| 1797 | * version attribute and the version check against the |
||
| 1798 | * managed copy fails. |
||
| 1799 | */ |
||
| 1800 | 15 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1974 | |||
| 1975 | /** |
||
| 1976 | * Detaches a document from the persistence management. It's persistence will |
||
| 1977 | * no longer be managed by Doctrine. |
||
| 1978 | * |
||
| 1979 | * @param object $document The document to detach. |
||
| 1980 | */ |
||
| 1981 | 12 | public function detach($document) |
|
| 1986 | |||
| 1987 | /** |
||
| 1988 | * Executes a detach operation on the given document. |
||
| 1989 | * |
||
| 1990 | * @param object $document |
||
| 1991 | * @param array $visited |
||
| 1992 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1993 | */ |
||
| 1994 | 17 | private function doDetach($document, array &$visited) |
|
| 2019 | |||
| 2020 | /** |
||
| 2021 | * Refreshes the state of the given document from the database, overwriting |
||
| 2022 | * any local, unpersisted changes. |
||
| 2023 | * |
||
| 2024 | * @param object $document The document to refresh. |
||
| 2025 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2026 | */ |
||
| 2027 | 23 | public function refresh($document) |
|
| 2032 | |||
| 2033 | /** |
||
| 2034 | * Executes a refresh operation on a document. |
||
| 2035 | * |
||
| 2036 | * @param object $document The document to refresh. |
||
| 2037 | * @param array $visited The already visited documents during cascades. |
||
| 2038 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2039 | */ |
||
| 2040 | 23 | private function doRefresh($document, array &$visited) |
|
| 2062 | |||
| 2063 | /** |
||
| 2064 | * Cascades a refresh operation to associated documents. |
||
| 2065 | * |
||
| 2066 | * @param object $document |
||
| 2067 | * @param array $visited |
||
| 2068 | */ |
||
| 2069 | 22 | private function cascadeRefresh($document, array &$visited) |
|
| 2093 | |||
| 2094 | /** |
||
| 2095 | * Cascades a detach operation to associated documents. |
||
| 2096 | * |
||
| 2097 | * @param object $document |
||
| 2098 | * @param array $visited |
||
| 2099 | */ |
||
| 2100 | 17 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2121 | /** |
||
| 2122 | * Cascades a merge operation to associated documents. |
||
| 2123 | * |
||
| 2124 | * @param object $document |
||
| 2125 | * @param object $managedCopy |
||
| 2126 | * @param array $visited |
||
| 2127 | */ |
||
| 2128 | 15 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2154 | |||
| 2155 | /** |
||
| 2156 | * Cascades the save operation to associated documents. |
||
| 2157 | * |
||
| 2158 | * @param object $document |
||
| 2159 | * @param array $visited |
||
| 2160 | */ |
||
| 2161 | 665 | private function cascadePersist($document, array &$visited) |
|
| 2208 | |||
| 2209 | /** |
||
| 2210 | * Cascades the delete operation to associated documents. |
||
| 2211 | * |
||
| 2212 | * @param object $document |
||
| 2213 | * @param array $visited |
||
| 2214 | */ |
||
| 2215 | 77 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2237 | |||
| 2238 | /** |
||
| 2239 | * Acquire a lock on the given document. |
||
| 2240 | * |
||
| 2241 | * @param object $document |
||
| 2242 | * @param int $lockMode |
||
| 2243 | * @param int $lockVersion |
||
| 2244 | * @throws LockException |
||
| 2245 | * @throws \InvalidArgumentException |
||
| 2246 | */ |
||
| 2247 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2271 | |||
| 2272 | /** |
||
| 2273 | * Releases a lock on the given document. |
||
| 2274 | * |
||
| 2275 | * @param object $document |
||
| 2276 | * @throws \InvalidArgumentException |
||
| 2277 | */ |
||
| 2278 | 1 | public function unlock($document) |
|
| 2286 | |||
| 2287 | /** |
||
| 2288 | * Clears the UnitOfWork. |
||
| 2289 | * |
||
| 2290 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2291 | */ |
||
| 2292 | 422 | public function clear($documentName = null) |
|
| 2326 | |||
| 2327 | /** |
||
| 2328 | * INTERNAL: |
||
| 2329 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2330 | * invoked on that document at the beginning of the next commit of this |
||
| 2331 | * UnitOfWork. |
||
| 2332 | * |
||
| 2333 | * @ignore |
||
| 2334 | * @param object $document |
||
| 2335 | */ |
||
| 2336 | 53 | public function scheduleOrphanRemoval($document) |
|
| 2340 | |||
| 2341 | /** |
||
| 2342 | * INTERNAL: |
||
| 2343 | * Unschedules an embedded or referenced object for removal. |
||
| 2344 | * |
||
| 2345 | * @ignore |
||
| 2346 | * @param object $document |
||
| 2347 | */ |
||
| 2348 | 114 | public function unscheduleOrphanRemoval($document) |
|
| 2355 | |||
| 2356 | /** |
||
| 2357 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2358 | * 1) sets owner if it was cloned |
||
| 2359 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2360 | * 3) NOP if state is OK |
||
| 2361 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2362 | * |
||
| 2363 | * @param PersistentCollectionInterface $coll |
||
| 2364 | * @param object $document |
||
| 2365 | * @param ClassMetadata $class |
||
| 2366 | * @param string $propName |
||
| 2367 | * @return PersistentCollectionInterface |
||
| 2368 | */ |
||
| 2369 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2389 | |||
| 2390 | /** |
||
| 2391 | * INTERNAL: |
||
| 2392 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2393 | * |
||
| 2394 | * @param PersistentCollectionInterface $coll |
||
| 2395 | */ |
||
| 2396 | 43 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2405 | |||
| 2406 | /** |
||
| 2407 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2408 | * |
||
| 2409 | * @param PersistentCollectionInterface $coll |
||
| 2410 | * @return boolean |
||
| 2411 | */ |
||
| 2412 | 220 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2416 | |||
| 2417 | /** |
||
| 2418 | * INTERNAL: |
||
| 2419 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2420 | * |
||
| 2421 | * @param PersistentCollectionInterface $coll |
||
| 2422 | */ |
||
| 2423 | 232 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2432 | |||
| 2433 | /** |
||
| 2434 | * INTERNAL: |
||
| 2435 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2436 | * |
||
| 2437 | * @param PersistentCollectionInterface $coll |
||
| 2438 | */ |
||
| 2439 | 254 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2454 | |||
| 2455 | /** |
||
| 2456 | * INTERNAL: |
||
| 2457 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2458 | * |
||
| 2459 | * @param PersistentCollectionInterface $coll |
||
| 2460 | */ |
||
| 2461 | 232 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2470 | |||
| 2471 | /** |
||
| 2472 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2473 | * |
||
| 2474 | * @param PersistentCollectionInterface $coll |
||
| 2475 | * @return boolean |
||
| 2476 | */ |
||
| 2477 | 133 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2481 | |||
| 2482 | /** |
||
| 2483 | * INTERNAL: |
||
| 2484 | * Gets PersistentCollections that have been visited during computing change |
||
| 2485 | * set of $document |
||
| 2486 | * |
||
| 2487 | * @param object $document |
||
| 2488 | * @return PersistentCollectionInterface[] |
||
| 2489 | */ |
||
| 2490 | 616 | public function getVisitedCollections($document) |
|
| 2497 | |||
| 2498 | /** |
||
| 2499 | * INTERNAL: |
||
| 2500 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2501 | * |
||
| 2502 | * @param object $document |
||
| 2503 | * @return array |
||
| 2504 | */ |
||
| 2505 | 617 | public function getScheduledCollections($document) |
|
| 2512 | |||
| 2513 | /** |
||
| 2514 | * Checks whether the document is related to a PersistentCollection |
||
| 2515 | * scheduled for update or deletion. |
||
| 2516 | * |
||
| 2517 | * @param object $document |
||
| 2518 | * @return boolean |
||
| 2519 | */ |
||
| 2520 | 52 | public function hasScheduledCollections($document) |
|
| 2524 | |||
| 2525 | /** |
||
| 2526 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2527 | * a collection scheduled for update or deletion. |
||
| 2528 | * |
||
| 2529 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2530 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2531 | * |
||
| 2532 | * If the collection is nested within atomic collection, it is immediately |
||
| 2533 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2534 | * calculating update data way easier. |
||
| 2535 | * |
||
| 2536 | * @param PersistentCollectionInterface $coll |
||
| 2537 | */ |
||
| 2538 | 256 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2561 | |||
| 2562 | /** |
||
| 2563 | * Get the top-most owning document of a given document |
||
| 2564 | * |
||
| 2565 | * If a top-level document is provided, that same document will be returned. |
||
| 2566 | * For an embedded document, we will walk through parent associations until |
||
| 2567 | * we find a top-level document. |
||
| 2568 | * |
||
| 2569 | * @param object $document |
||
| 2570 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2571 | * @return object |
||
| 2572 | */ |
||
| 2573 | 258 | public function getOwningDocument($document) |
|
| 2589 | |||
| 2590 | /** |
||
| 2591 | * Gets the class name for an association (embed or reference) with respect |
||
| 2592 | * to any discriminator value. |
||
| 2593 | * |
||
| 2594 | * @param array $mapping Field mapping for the association |
||
| 2595 | * @param array|null $data Data for the embedded document or reference |
||
| 2596 | * @return string Class name. |
||
| 2597 | */ |
||
| 2598 | 228 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2631 | |||
| 2632 | /** |
||
| 2633 | * INTERNAL: |
||
| 2634 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2635 | * |
||
| 2636 | * @ignore |
||
| 2637 | * @param string $className The name of the document class. |
||
| 2638 | * @param array $data The data for the document. |
||
| 2639 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2640 | * @param object $document The document to be hydrated into in case of creation |
||
| 2641 | * @return object The document instance. |
||
| 2642 | * @internal Highly performance-sensitive method. |
||
| 2643 | */ |
||
| 2644 | 426 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2645 | { |
||
| 2646 | 426 | $class = $this->dm->getClassMetadata($className); |
|
| 2647 | |||
| 2648 | // @TODO figure out how to remove this |
||
| 2649 | 426 | $discriminatorValue = null; |
|
| 2650 | 426 | View Code Duplication | if (isset($class->discriminatorField, $data[$class->discriminatorField])) { |
| 2651 | 19 | $discriminatorValue = $data[$class->discriminatorField]; |
|
| 2652 | 418 | } elseif (isset($class->defaultDiscriminatorValue)) { |
|
| 2653 | 2 | $discriminatorValue = $class->defaultDiscriminatorValue; |
|
| 2654 | } |
||
| 2655 | |||
| 2656 | 426 | if ($discriminatorValue !== null) { |
|
| 2657 | 20 | $className = isset($class->discriminatorMap[$discriminatorValue]) |
|
| 2658 | 18 | ? $class->discriminatorMap[$discriminatorValue] |
|
| 2659 | 20 | : $discriminatorValue; |
|
| 2660 | |||
| 2661 | 20 | $class = $this->dm->getClassMetadata($className); |
|
| 2662 | |||
| 2663 | 20 | unset($data[$class->discriminatorField]); |
|
| 2664 | } |
||
| 2665 | |||
| 2666 | 426 | if (! empty($hints[Query::HINT_READ_ONLY])) { |
|
| 2667 | 2 | $document = $class->newInstance(); |
|
| 2668 | 2 | $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2669 | 2 | return $document; |
|
| 2670 | } |
||
| 2671 | |||
| 2672 | 425 | $isManagedObject = false; |
|
| 2673 | 425 | if (! $class->isQueryResultDocument) { |
|
| 2674 | 425 | $id = $class->getDatabaseIdentifierValue($data['_id']); |
|
| 2675 | 425 | $serializedId = serialize($id); |
|
| 2676 | 425 | $isManagedObject = isset($this->identityMap[$class->name][$serializedId]); |
|
| 2677 | } |
||
| 2678 | |||
| 2679 | 425 | if ($isManagedObject) { |
|
| 2680 | 110 | $document = $this->identityMap[$class->name][$serializedId]; |
|
| 2681 | 110 | $oid = spl_object_hash($document); |
|
| 2682 | 110 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 2683 | 14 | $document->__isInitialized__ = true; |
|
| 2684 | 14 | $overrideLocalValues = true; |
|
| 2685 | 14 | if ($document instanceof NotifyPropertyChanged) { |
|
| 2686 | 14 | $document->addPropertyChangedListener($this); |
|
| 2687 | } |
||
| 2688 | } else { |
||
| 2689 | 106 | $overrideLocalValues = ! empty($hints[Query::HINT_REFRESH]); |
|
| 2690 | } |
||
| 2691 | 110 | if ($overrideLocalValues) { |
|
| 2692 | 52 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2693 | 110 | $this->originalDocumentData[$oid] = $data; |
|
| 2694 | } |
||
| 2695 | } else { |
||
| 2696 | 384 | if ($document === null) { |
|
| 2697 | 384 | $document = $class->newInstance(); |
|
| 2698 | } |
||
| 2699 | |||
| 2700 | 384 | if (! $class->isQueryResultDocument) { |
|
| 2701 | 383 | $this->registerManaged($document, $id, $data); |
|
| 2702 | 383 | $oid = spl_object_hash($document); |
|
| 2703 | 383 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 2704 | 383 | $this->identityMap[$class->name][$serializedId] = $document; |
|
| 2705 | } |
||
| 2706 | |||
| 2707 | 384 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2708 | |||
| 2709 | 384 | if (! $class->isQueryResultDocument) { |
|
| 2710 | 383 | $this->originalDocumentData[$oid] = $data; |
|
| 2711 | } |
||
| 2712 | } |
||
| 2713 | |||
| 2714 | 425 | return $document; |
|
| 2715 | } |
||
| 2716 | |||
| 2717 | /** |
||
| 2718 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2719 | * |
||
| 2720 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2721 | */ |
||
| 2722 | 173 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2727 | |||
| 2728 | /** |
||
| 2729 | * Gets the identity map of the UnitOfWork. |
||
| 2730 | * |
||
| 2731 | * @return array |
||
| 2732 | */ |
||
| 2733 | public function getIdentityMap() |
||
| 2737 | |||
| 2738 | /** |
||
| 2739 | * Gets the original data of a document. The original data is the data that was |
||
| 2740 | * present at the time the document was reconstituted from the database. |
||
| 2741 | * |
||
| 2742 | * @param object $document |
||
| 2743 | * @return array |
||
| 2744 | */ |
||
| 2745 | 1 | public function getOriginalDocumentData($document) |
|
| 2753 | |||
| 2754 | /** |
||
| 2755 | * @ignore |
||
| 2756 | */ |
||
| 2757 | 56 | public function setOriginalDocumentData($document, array $data) |
|
| 2763 | |||
| 2764 | /** |
||
| 2765 | * INTERNAL: |
||
| 2766 | * Sets a property value of the original data array of a document. |
||
| 2767 | * |
||
| 2768 | * @ignore |
||
| 2769 | * @param string $oid |
||
| 2770 | * @param string $property |
||
| 2771 | * @param mixed $value |
||
| 2772 | */ |
||
| 2773 | 6 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2777 | |||
| 2778 | /** |
||
| 2779 | * Gets the identifier of a document. |
||
| 2780 | * |
||
| 2781 | * @param object $document |
||
| 2782 | * @return mixed The identifier value |
||
| 2783 | */ |
||
| 2784 | 459 | public function getDocumentIdentifier($document) |
|
| 2789 | |||
| 2790 | /** |
||
| 2791 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2792 | * |
||
| 2793 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2794 | */ |
||
| 2795 | public function hasPendingInsertions() |
||
| 2799 | |||
| 2800 | /** |
||
| 2801 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2802 | * number of documents in the identity map. |
||
| 2803 | * |
||
| 2804 | * @return integer |
||
| 2805 | */ |
||
| 2806 | 2 | public function size() |
|
| 2814 | |||
| 2815 | /** |
||
| 2816 | * INTERNAL: |
||
| 2817 | * Registers a document as managed. |
||
| 2818 | * |
||
| 2819 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2820 | * document class. If the class expects its database identifier to be a |
||
| 2821 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2822 | * document identifiers map will become inconsistent with the identity map. |
||
| 2823 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2824 | * conversion and throw an exception if it's inconsistent. |
||
| 2825 | * |
||
| 2826 | * @param object $document The document. |
||
| 2827 | * @param array $id The identifier values. |
||
| 2828 | * @param array $data The original document data. |
||
| 2829 | */ |
||
| 2830 | 408 | public function registerManaged($document, $id, array $data) |
|
| 2845 | |||
| 2846 | /** |
||
| 2847 | * INTERNAL: |
||
| 2848 | * Clears the property changeset of the document with the given OID. |
||
| 2849 | * |
||
| 2850 | * @param string $oid The document's OID. |
||
| 2851 | */ |
||
| 2852 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2856 | |||
| 2857 | /* PropertyChangedListener implementation */ |
||
| 2858 | |||
| 2859 | /** |
||
| 2860 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2861 | * |
||
| 2862 | * @param object $document The document that owns the property. |
||
| 2863 | * @param string $propertyName The name of the property that changed. |
||
| 2864 | * @param mixed $oldValue The old value of the property. |
||
| 2865 | * @param mixed $newValue The new value of the property. |
||
| 2866 | */ |
||
| 2867 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2882 | |||
| 2883 | /** |
||
| 2884 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2885 | * |
||
| 2886 | * @return array |
||
| 2887 | */ |
||
| 2888 | 5 | public function getScheduledDocumentInsertions() |
|
| 2892 | |||
| 2893 | /** |
||
| 2894 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2895 | * |
||
| 2896 | * @return array |
||
| 2897 | */ |
||
| 2898 | 3 | public function getScheduledDocumentUpserts() |
|
| 2902 | |||
| 2903 | /** |
||
| 2904 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2905 | * |
||
| 2906 | * @return array |
||
| 2907 | */ |
||
| 2908 | 3 | public function getScheduledDocumentUpdates() |
|
| 2912 | |||
| 2913 | /** |
||
| 2914 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2915 | * |
||
| 2916 | * @return array |
||
| 2917 | */ |
||
| 2918 | public function getScheduledDocumentDeletions() |
||
| 2922 | |||
| 2923 | /** |
||
| 2924 | * Get the currently scheduled complete collection deletions |
||
| 2925 | * |
||
| 2926 | * @return array |
||
| 2927 | */ |
||
| 2928 | public function getScheduledCollectionDeletions() |
||
| 2932 | |||
| 2933 | /** |
||
| 2934 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2935 | * |
||
| 2936 | * @return array |
||
| 2937 | */ |
||
| 2938 | public function getScheduledCollectionUpdates() |
||
| 2942 | |||
| 2943 | /** |
||
| 2944 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2945 | * |
||
| 2946 | * @param object |
||
| 2947 | * @return void |
||
| 2948 | */ |
||
| 2949 | public function initializeObject($obj) |
||
| 2957 | |||
| 2958 | 1 | private function objToStr($obj) |
|
| 2962 | } |
||
| 2963 |
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.