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 | * @todo We might need to clean up this array in clear(), doDetach(), etc. |
||
| 246 | * @var array |
||
| 247 | */ |
||
| 248 | private $parentAssociations = array(); |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @var LifecycleEventManager |
||
| 252 | */ |
||
| 253 | private $lifecycleEventManager; |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 257 | * |
||
| 258 | * @param DocumentManager $dm |
||
| 259 | * @param EventManager $evm |
||
| 260 | * @param HydratorFactory $hydratorFactory |
||
| 261 | */ |
||
| 262 | 1040 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 263 | { |
||
| 264 | 1040 | $this->dm = $dm; |
|
| 265 | 1040 | $this->evm = $evm; |
|
| 266 | 1040 | $this->hydratorFactory = $hydratorFactory; |
|
| 267 | 1040 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 268 | 1040 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 272 | * queries for insert persistence. |
||
| 273 | * |
||
| 274 | * @return PersistenceBuilder $pb |
||
| 275 | */ |
||
| 276 | 726 | public function getPersistenceBuilder() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Sets the parent association for a given embedded document. |
||
| 286 | * |
||
| 287 | * @param object $document |
||
| 288 | * @param array $mapping |
||
| 289 | * @param object $parent |
||
| 290 | * @param string $propertyPath |
||
| 291 | */ |
||
| 292 | 193 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Gets the parent association for a given embedded document. |
||
| 300 | * |
||
| 301 | * <code> |
||
| 302 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 303 | * </code> |
||
| 304 | * |
||
| 305 | * @param object $document |
||
| 306 | * @return array $association |
||
| 307 | */ |
||
| 308 | 222 | public function getParentAssociation($document) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get the document persister instance for the given document name |
||
| 319 | * |
||
| 320 | * @param string $documentName |
||
| 321 | * @return Persisters\DocumentPersister |
||
| 322 | */ |
||
| 323 | 724 | public function getDocumentPersister($documentName) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get the collection persister instance. |
||
| 335 | * |
||
| 336 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 337 | */ |
||
| 338 | 724 | public function getCollectionPersister() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Set the document persister instance to use for the given document name |
||
| 349 | * |
||
| 350 | * @param string $documentName |
||
| 351 | * @param Persisters\DocumentPersister $persister |
||
| 352 | */ |
||
| 353 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 360 | * up to this point. The state of all managed documents will be synchronized with |
||
| 361 | * the database. |
||
| 362 | * |
||
| 363 | * The operations are executed in the following order: |
||
| 364 | * |
||
| 365 | * 1) All document insertions |
||
| 366 | * 2) All document updates |
||
| 367 | * 3) All document deletions |
||
| 368 | * |
||
| 369 | * @param object $document |
||
| 370 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 371 | */ |
||
| 372 | 604 | public function commit($document = null, array $options = array()) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Groups a list of scheduled documents by their class. |
||
| 453 | * |
||
| 454 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 455 | * @param bool $includeEmbedded |
||
| 456 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 457 | */ |
||
| 458 | 599 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Compute changesets of all documents scheduled for insertion. |
||
| 490 | * |
||
| 491 | * Embedded documents will not be processed. |
||
| 492 | */ |
||
| 493 | 606 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 502 | |||
| 503 | /** |
||
| 504 | * Compute changesets of all documents scheduled for upsert. |
||
| 505 | * |
||
| 506 | * Embedded documents will not be processed. |
||
| 507 | */ |
||
| 508 | 605 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 517 | |||
| 518 | /** |
||
| 519 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 520 | * |
||
| 521 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 522 | * 2. Proxies are skipped. |
||
| 523 | * 3. Only if document is properly managed. |
||
| 524 | * |
||
| 525 | * @param object $document |
||
| 526 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 13 | private function computeSingleDocumentChangeSet($document) |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Gets the changeset for a document. |
||
| 566 | * |
||
| 567 | * @param object $document |
||
| 568 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 569 | */ |
||
| 570 | 599 | public function getDocumentChangeSet($document) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * INTERNAL: |
||
| 581 | * Sets the changeset for a document. |
||
| 582 | * |
||
| 583 | * @param object $document |
||
| 584 | * @param array $changeset |
||
| 585 | */ |
||
| 586 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 593 | * |
||
| 594 | * @param object $document |
||
| 595 | * @return array |
||
| 596 | */ |
||
| 597 | 606 | public function getDocumentActualData($document) |
|
| 631 | |||
| 632 | /** |
||
| 633 | * Computes the changes that happened to a single document. |
||
| 634 | * |
||
| 635 | * Modifies/populates the following properties: |
||
| 636 | * |
||
| 637 | * {@link originalDocumentData} |
||
| 638 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 639 | * then it was not fetched from the database and therefore we have no original |
||
| 640 | * document data yet. All of the current document data is stored as the original document data. |
||
| 641 | * |
||
| 642 | * {@link documentChangeSets} |
||
| 643 | * The changes detected on all properties of the document are stored there. |
||
| 644 | * A change is a tuple array where the first entry is the old value and the second |
||
| 645 | * entry is the new value of the property. Changesets are used by persisters |
||
| 646 | * to INSERT/UPDATE the persistent document state. |
||
| 647 | * |
||
| 648 | * {@link documentUpdates} |
||
| 649 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 650 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 651 | * there to mark it for an update. |
||
| 652 | * |
||
| 653 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 654 | * @param object $document The document for which to compute the changes. |
||
| 655 | */ |
||
| 656 | 603 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 672 | * |
||
| 673 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 674 | * @param object $document |
||
| 675 | * @param boolean $recompute |
||
| 676 | */ |
||
| 677 | 603 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 678 | { |
||
| 679 | 603 | $oid = spl_object_hash($document); |
|
| 680 | 603 | $actualData = $this->getDocumentActualData($document); |
|
| 681 | 603 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 682 | 603 | if ($isNewDocument) { |
|
| 683 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 684 | // These result in an INSERT. |
||
| 685 | 603 | $this->originalDocumentData[$oid] = $actualData; |
|
| 686 | 603 | $changeSet = array(); |
|
| 687 | 603 | foreach ($actualData as $propName => $actualValue) { |
|
| 688 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 689 | * was cloned and its ownership must be fixed |
||
| 690 | */ |
||
| 691 | 603 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 692 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 693 | $actualValue = $actualData[$propName]; |
||
| 694 | } |
||
| 695 | // ignore inverse side of reference relationship |
||
| 696 | 603 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 697 | 184 | continue; |
|
| 698 | } |
||
| 699 | 603 | $changeSet[$propName] = array(null, $actualValue); |
|
| 700 | } |
||
| 701 | 603 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 702 | } else { |
||
| 703 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 704 | // and we have a copy of the original data |
||
| 705 | 292 | $originalData = $this->originalDocumentData[$oid]; |
|
| 706 | 292 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 707 | 292 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 708 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 709 | } else { |
||
| 710 | 292 | $changeSet = array(); |
|
| 711 | } |
||
| 712 | |||
| 713 | 292 | foreach ($actualData as $propName => $actualValue) { |
|
| 714 | // skip not saved fields |
||
| 715 | 292 | if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) { |
|
| 716 | continue; |
||
| 717 | } |
||
| 718 | |||
| 719 | 292 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; |
|
| 720 | |||
| 721 | // skip if value has not changed |
||
| 722 | 292 | if ($orgValue === $actualValue) { |
|
| 723 | 291 | if ($actualValue instanceof PersistentCollectionInterface) { |
|
| 724 | 203 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
|
| 725 | // consider dirty collections as changed as well |
||
| 726 | 203 | continue; |
|
| 727 | } |
||
| 728 | 291 | } elseif ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) { |
|
| 729 | // but consider dirty GridFSFile instances as changed |
||
| 730 | 291 | continue; |
|
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 735 | 251 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 736 | 12 | if ($orgValue !== null) { |
|
| 737 | 7 | $this->scheduleOrphanRemoval($orgValue); |
|
| 738 | } |
||
| 739 | |||
| 740 | 12 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 741 | 12 | continue; |
|
| 742 | } |
||
| 743 | |||
| 744 | // if owning side of reference-one relationship |
||
| 745 | 244 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 746 | 13 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 747 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 748 | } |
||
| 749 | |||
| 750 | 13 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 751 | 13 | continue; |
|
| 752 | } |
||
| 753 | |||
| 754 | 237 | if ($isChangeTrackingNotify) { |
|
| 755 | 3 | continue; |
|
| 756 | } |
||
| 757 | |||
| 758 | // ignore inverse side of reference relationship |
||
| 759 | 235 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 760 | 6 | continue; |
|
| 761 | } |
||
| 762 | |||
| 763 | // Persistent collection was exchanged with the "originally" |
||
| 764 | // created one. This can only mean it was cloned and replaced |
||
| 765 | // on another document. |
||
| 766 | 233 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 767 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 768 | } |
||
| 769 | |||
| 770 | // if embed-many or reference-many relationship |
||
| 771 | 233 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 772 | 117 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 773 | /* If original collection was exchanged with a non-empty value |
||
| 774 | * and $set will be issued, there is no need to $unset it first |
||
| 775 | */ |
||
| 776 | 117 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 777 | 28 | continue; |
|
| 778 | } |
||
| 779 | 97 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
|
| 780 | 17 | $this->scheduleCollectionDeletion($orgValue); |
|
| 781 | } |
||
| 782 | 97 | continue; |
|
| 783 | } |
||
| 784 | |||
| 785 | // skip equivalent date values |
||
| 786 | 153 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 787 | 36 | $dateType = Type::getType('date'); |
|
| 788 | 36 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 789 | 36 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 790 | |||
| 791 | 36 | if ($dbOrgValue instanceof \MongoDate && $dbActualValue instanceof \MongoDate && $dbOrgValue == $dbActualValue) { |
|
| 792 | 29 | continue; |
|
| 793 | } |
||
| 794 | } |
||
| 795 | |||
| 796 | // regular field |
||
| 797 | 137 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 798 | } |
||
| 799 | 292 | if ($changeSet) { |
|
| 800 | 240 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 801 | 21 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 802 | 235 | : $changeSet; |
|
| 803 | |||
| 804 | 240 | $this->originalDocumentData[$oid] = $actualData; |
|
| 805 | 240 | $this->scheduleForUpdate($document); |
|
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | // Look for changes in associations of the document |
||
| 810 | 603 | $associationMappings = array_filter( |
|
| 811 | 603 | $class->associationMappings, |
|
| 812 | function ($assoc) { return empty($assoc['notSaved']); } |
||
| 813 | ); |
||
| 814 | |||
| 815 | 603 | foreach ($associationMappings as $mapping) { |
|
| 816 | 458 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 817 | |||
| 818 | 458 | if ($value === null) { |
|
| 819 | 309 | continue; |
|
| 820 | } |
||
| 821 | |||
| 822 | 445 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 823 | |||
| 824 | 444 | if (isset($mapping['reference'])) { |
|
| 825 | 337 | continue; |
|
| 826 | } |
||
| 827 | |||
| 828 | 346 | $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value->unwrap(); |
|
| 829 | |||
| 830 | 346 | foreach ($values as $obj) { |
|
| 831 | 181 | $oid2 = spl_object_hash($obj); |
|
| 832 | |||
| 833 | 181 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 834 | 179 | $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value); |
|
| 835 | |||
| 836 | 179 | if ( ! $isNewDocument) { |
|
| 837 | 79 | $this->scheduleForUpdate($document); |
|
| 838 | } |
||
| 839 | |||
| 840 | 346 | break; |
|
| 841 | } |
||
| 842 | } |
||
| 843 | } |
||
| 844 | 602 | } |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Computes all the changes that have been done to documents and collections |
||
| 848 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 849 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 850 | */ |
||
| 851 | 601 | public function computeChangeSets() |
|
| 901 | |||
| 902 | /** |
||
| 903 | * Computes the changes of an association. |
||
| 904 | * |
||
| 905 | * @param object $parentDocument |
||
| 906 | * @param array $assoc |
||
| 907 | * @param mixed $value The value of the association. |
||
| 908 | * @throws \InvalidArgumentException |
||
| 909 | */ |
||
| 910 | 445 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1011 | |||
| 1012 | /** |
||
| 1013 | * INTERNAL: |
||
| 1014 | * Computes the changeset of an individual document, independently of the |
||
| 1015 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1016 | * |
||
| 1017 | * The passed document must be a managed document. If the document already has a change set |
||
| 1018 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1019 | * whereby changes detected in this method prevail. |
||
| 1020 | * |
||
| 1021 | * @ignore |
||
| 1022 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1023 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1024 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1025 | */ |
||
| 1026 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * @param ClassMetadata $class |
||
| 1048 | * @param object $document |
||
| 1049 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1050 | */ |
||
| 1051 | 628 | private function persistNew(ClassMetadata $class, $document) |
|
| 1094 | |||
| 1095 | /** |
||
| 1096 | * Executes all document insertions for documents of the specified type. |
||
| 1097 | * |
||
| 1098 | * @param ClassMetadata $class |
||
| 1099 | * @param array $documents Array of documents to insert |
||
| 1100 | * @param array $options Array of options to be used with batchInsert() |
||
| 1101 | */ |
||
| 1102 | 525 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Executes all document upserts for documents of the specified type. |
||
| 1120 | * |
||
| 1121 | * @param ClassMetadata $class |
||
| 1122 | * @param array $documents Array of documents to upsert |
||
| 1123 | * @param array $options Array of options to be used with batchInsert() |
||
| 1124 | */ |
||
| 1125 | 85 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Executes all document updates for documents of the specified type. |
||
| 1144 | * |
||
| 1145 | * @param Mapping\ClassMetadata $class |
||
| 1146 | * @param array $documents Array of documents to update |
||
| 1147 | * @param array $options Array of options to be used with update() |
||
| 1148 | */ |
||
| 1149 | 232 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Executes all document deletions for documents of the specified type. |
||
| 1169 | * |
||
| 1170 | * @param ClassMetadata $class |
||
| 1171 | * @param array $documents Array of documents to delete |
||
| 1172 | * @param array $options Array of options to be used with remove() |
||
| 1173 | */ |
||
| 1174 | 70 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Schedules a document for insertion into the database. |
||
| 1209 | * If the document already has an identifier, it will be added to the |
||
| 1210 | * identity map. |
||
| 1211 | * |
||
| 1212 | * @param ClassMetadata $class |
||
| 1213 | * @param object $document The document to schedule for insertion. |
||
| 1214 | * @throws \InvalidArgumentException |
||
| 1215 | */ |
||
| 1216 | 558 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Schedules a document for upsert into the database and adds it to the |
||
| 1239 | * identity map |
||
| 1240 | * |
||
| 1241 | * @param ClassMetadata $class |
||
| 1242 | * @param object $document The document to schedule for upsert. |
||
| 1243 | * @throws \InvalidArgumentException |
||
| 1244 | */ |
||
| 1245 | 91 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Checks whether a document is scheduled for insertion. |
||
| 1269 | * |
||
| 1270 | * @param object $document |
||
| 1271 | * @return boolean |
||
| 1272 | */ |
||
| 1273 | 105 | public function isScheduledForInsert($document) |
|
| 1277 | |||
| 1278 | /** |
||
| 1279 | * Checks whether a document is scheduled for upsert. |
||
| 1280 | * |
||
| 1281 | * @param object $document |
||
| 1282 | * @return boolean |
||
| 1283 | */ |
||
| 1284 | 5 | public function isScheduledForUpsert($document) |
|
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Schedules a document for being updated. |
||
| 1291 | * |
||
| 1292 | * @param object $document The document to schedule for being updated. |
||
| 1293 | * @throws \InvalidArgumentException |
||
| 1294 | */ |
||
| 1295 | 241 | public function scheduleForUpdate($document) |
|
| 1312 | |||
| 1313 | /** |
||
| 1314 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1315 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1316 | * at commit time. |
||
| 1317 | * |
||
| 1318 | * @param object $document |
||
| 1319 | * @return boolean |
||
| 1320 | */ |
||
| 1321 | 22 | public function isScheduledForUpdate($document) |
|
| 1325 | |||
| 1326 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1331 | |||
| 1332 | /** |
||
| 1333 | * INTERNAL: |
||
| 1334 | * Schedules a document for deletion. |
||
| 1335 | * |
||
| 1336 | * @param object $document |
||
| 1337 | */ |
||
| 1338 | 75 | public function scheduleForDelete($document) |
|
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1367 | * of work. |
||
| 1368 | * |
||
| 1369 | * @param object $document |
||
| 1370 | * @return boolean |
||
| 1371 | */ |
||
| 1372 | 8 | public function isScheduledForDelete($document) |
|
| 1376 | |||
| 1377 | /** |
||
| 1378 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1379 | * |
||
| 1380 | * @param $document |
||
| 1381 | * @return boolean |
||
| 1382 | */ |
||
| 1383 | 242 | public function isDocumentScheduled($document) |
|
| 1391 | |||
| 1392 | /** |
||
| 1393 | * INTERNAL: |
||
| 1394 | * Registers a document in the identity map. |
||
| 1395 | * |
||
| 1396 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1397 | * the root document. Identifiers are serialized before being used as array |
||
| 1398 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1399 | * |
||
| 1400 | * @ignore |
||
| 1401 | * @param object $document The document to register. |
||
| 1402 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1403 | * the document in question is already managed. |
||
| 1404 | */ |
||
| 1405 | 657 | public function addToIdentityMap($document) |
|
| 1423 | |||
| 1424 | /** |
||
| 1425 | * Gets the state of a document with regard to the current unit of work. |
||
| 1426 | * |
||
| 1427 | * @param object $document |
||
| 1428 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1429 | * This parameter can be set to improve performance of document state detection |
||
| 1430 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1431 | * is either known or does not matter for the caller of the method. |
||
| 1432 | * @return int The document state. |
||
| 1433 | */ |
||
| 1434 | 631 | public function getDocumentState($document, $assume = null) |
|
| 1484 | |||
| 1485 | /** |
||
| 1486 | * INTERNAL: |
||
| 1487 | * Removes a document from the identity map. This effectively detaches the |
||
| 1488 | * document from the persistence management of Doctrine. |
||
| 1489 | * |
||
| 1490 | * @ignore |
||
| 1491 | * @param object $document |
||
| 1492 | * @throws \InvalidArgumentException |
||
| 1493 | * @return boolean |
||
| 1494 | */ |
||
| 1495 | 84 | public function removeFromIdentityMap($document) |
|
| 1515 | |||
| 1516 | /** |
||
| 1517 | * INTERNAL: |
||
| 1518 | * Gets a document in the identity map by its identifier hash. |
||
| 1519 | * |
||
| 1520 | * @ignore |
||
| 1521 | * @param mixed $id Document identifier |
||
| 1522 | * @param ClassMetadata $class Document class |
||
| 1523 | * @return object |
||
| 1524 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1525 | */ |
||
| 1526 | 34 | public function getById($id, ClassMetadata $class) |
|
| 1536 | |||
| 1537 | /** |
||
| 1538 | * INTERNAL: |
||
| 1539 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1540 | * for the given hash, FALSE is returned. |
||
| 1541 | * |
||
| 1542 | * @ignore |
||
| 1543 | * @param mixed $id Document identifier |
||
| 1544 | * @param ClassMetadata $class Document class |
||
| 1545 | * @return mixed The found document or FALSE. |
||
| 1546 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1547 | */ |
||
| 1548 | 305 | public function tryGetById($id, ClassMetadata $class) |
|
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Schedules a document for dirty-checking at commit-time. |
||
| 1562 | * |
||
| 1563 | * @param object $document The document to schedule for dirty-checking. |
||
| 1564 | * @todo Rename: scheduleForSynchronization |
||
| 1565 | */ |
||
| 1566 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Checks whether a document is registered in the identity map. |
||
| 1574 | * |
||
| 1575 | * @param object $document |
||
| 1576 | * @return boolean |
||
| 1577 | */ |
||
| 1578 | 86 | public function isInIdentityMap($document) |
|
| 1591 | |||
| 1592 | /** |
||
| 1593 | * @param object $document |
||
| 1594 | * @return string |
||
| 1595 | */ |
||
| 1596 | 657 | private function getIdForIdentityMap($document) |
|
| 1609 | |||
| 1610 | /** |
||
| 1611 | * INTERNAL: |
||
| 1612 | * Checks whether an identifier exists in the identity map. |
||
| 1613 | * |
||
| 1614 | * @ignore |
||
| 1615 | * @param string $id |
||
| 1616 | * @param string $rootClassName |
||
| 1617 | * @return boolean |
||
| 1618 | */ |
||
| 1619 | public function containsId($id, $rootClassName) |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * Persists a document as part of the current unit of work. |
||
| 1626 | * |
||
| 1627 | * @param object $document The document to persist. |
||
| 1628 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1629 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1630 | */ |
||
| 1631 | 625 | public function persist($document) |
|
| 1640 | |||
| 1641 | /** |
||
| 1642 | * Saves a document as part of the current unit of work. |
||
| 1643 | * This method is internally called during save() cascades as it tracks |
||
| 1644 | * the already visited documents to prevent infinite recursions. |
||
| 1645 | * |
||
| 1646 | * NOTE: This method always considers documents that are not yet known to |
||
| 1647 | * this UnitOfWork as NEW. |
||
| 1648 | * |
||
| 1649 | * @param object $document The document to persist. |
||
| 1650 | * @param array $visited The already visited documents. |
||
| 1651 | * @throws \InvalidArgumentException |
||
| 1652 | * @throws MongoDBException |
||
| 1653 | */ |
||
| 1654 | 624 | private function doPersist($document, array &$visited) |
|
| 1694 | |||
| 1695 | /** |
||
| 1696 | * Deletes a document as part of the current unit of work. |
||
| 1697 | * |
||
| 1698 | * @param object $document The document to remove. |
||
| 1699 | */ |
||
| 1700 | 74 | public function remove($document) |
|
| 1705 | |||
| 1706 | /** |
||
| 1707 | * Deletes a document as part of the current unit of work. |
||
| 1708 | * |
||
| 1709 | * This method is internally called during delete() cascades as it tracks |
||
| 1710 | * the already visited documents to prevent infinite recursions. |
||
| 1711 | * |
||
| 1712 | * @param object $document The document to delete. |
||
| 1713 | * @param array $visited The map of the already visited documents. |
||
| 1714 | * @throws MongoDBException |
||
| 1715 | */ |
||
| 1716 | 74 | private function doRemove($document, array &$visited) |
|
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1751 | * |
||
| 1752 | * @param object $document |
||
| 1753 | * @return object The managed copy of the document. |
||
| 1754 | */ |
||
| 1755 | 15 | public function merge($document) |
|
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Executes a merge operation on a document. |
||
| 1764 | * |
||
| 1765 | * @param object $document |
||
| 1766 | * @param array $visited |
||
| 1767 | * @param object|null $prevManagedCopy |
||
| 1768 | * @param array|null $assoc |
||
| 1769 | * |
||
| 1770 | * @return object The managed copy of the document. |
||
| 1771 | * |
||
| 1772 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1773 | * @throws LockException If the document uses optimistic locking through a |
||
| 1774 | * version attribute and the version check against the |
||
| 1775 | * managed copy fails. |
||
| 1776 | */ |
||
| 1777 | 15 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1951 | |||
| 1952 | /** |
||
| 1953 | * Detaches a document from the persistence management. It's persistence will |
||
| 1954 | * no longer be managed by Doctrine. |
||
| 1955 | * |
||
| 1956 | * @param object $document The document to detach. |
||
| 1957 | */ |
||
| 1958 | 9 | public function detach($document) |
|
| 1963 | |||
| 1964 | /** |
||
| 1965 | * Executes a detach operation on the given document. |
||
| 1966 | * |
||
| 1967 | * @param object $document |
||
| 1968 | * @param array $visited |
||
| 1969 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1970 | */ |
||
| 1971 | 12 | private function doDetach($document, array &$visited) |
|
| 1996 | |||
| 1997 | /** |
||
| 1998 | * Refreshes the state of the given document from the database, overwriting |
||
| 1999 | * any local, unpersisted changes. |
||
| 2000 | * |
||
| 2001 | * @param object $document The document to refresh. |
||
| 2002 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2003 | */ |
||
| 2004 | 22 | public function refresh($document) |
|
| 2009 | |||
| 2010 | /** |
||
| 2011 | * Executes a refresh operation on a document. |
||
| 2012 | * |
||
| 2013 | * @param object $document The document to refresh. |
||
| 2014 | * @param array $visited The already visited documents during cascades. |
||
| 2015 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2016 | */ |
||
| 2017 | 22 | private function doRefresh($document, array &$visited) |
|
| 2039 | |||
| 2040 | /** |
||
| 2041 | * Cascades a refresh operation to associated documents. |
||
| 2042 | * |
||
| 2043 | * @param object $document |
||
| 2044 | * @param array $visited |
||
| 2045 | */ |
||
| 2046 | 21 | private function cascadeRefresh($document, array &$visited) |
|
| 2070 | |||
| 2071 | /** |
||
| 2072 | * Cascades a detach operation to associated documents. |
||
| 2073 | * |
||
| 2074 | * @param object $document |
||
| 2075 | * @param array $visited |
||
| 2076 | */ |
||
| 2077 | 12 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2098 | /** |
||
| 2099 | * Cascades a merge operation to associated documents. |
||
| 2100 | * |
||
| 2101 | * @param object $document |
||
| 2102 | * @param object $managedCopy |
||
| 2103 | * @param array $visited |
||
| 2104 | */ |
||
| 2105 | 15 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2131 | |||
| 2132 | /** |
||
| 2133 | * Cascades the save operation to associated documents. |
||
| 2134 | * |
||
| 2135 | * @param object $document |
||
| 2136 | * @param array $visited |
||
| 2137 | */ |
||
| 2138 | 622 | private function cascadePersist($document, array &$visited) |
|
| 2185 | |||
| 2186 | /** |
||
| 2187 | * Cascades the delete operation to associated documents. |
||
| 2188 | * |
||
| 2189 | * @param object $document |
||
| 2190 | * @param array $visited |
||
| 2191 | */ |
||
| 2192 | 74 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2214 | |||
| 2215 | /** |
||
| 2216 | * Acquire a lock on the given document. |
||
| 2217 | * |
||
| 2218 | * @param object $document |
||
| 2219 | * @param int $lockMode |
||
| 2220 | * @param int $lockVersion |
||
| 2221 | * @throws LockException |
||
| 2222 | * @throws \InvalidArgumentException |
||
| 2223 | */ |
||
| 2224 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2248 | |||
| 2249 | /** |
||
| 2250 | * Releases a lock on the given document. |
||
| 2251 | * |
||
| 2252 | * @param object $document |
||
| 2253 | * @throws \InvalidArgumentException |
||
| 2254 | */ |
||
| 2255 | 1 | public function unlock($document) |
|
| 2263 | |||
| 2264 | /** |
||
| 2265 | * Clears the UnitOfWork. |
||
| 2266 | * |
||
| 2267 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2268 | */ |
||
| 2269 | 402 | public function clear($documentName = null) |
|
| 2302 | |||
| 2303 | /** |
||
| 2304 | * INTERNAL: |
||
| 2305 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2306 | * invoked on that document at the beginning of the next commit of this |
||
| 2307 | * UnitOfWork. |
||
| 2308 | * |
||
| 2309 | * @ignore |
||
| 2310 | * @param object $document |
||
| 2311 | */ |
||
| 2312 | 50 | public function scheduleOrphanRemoval($document) |
|
| 2316 | |||
| 2317 | /** |
||
| 2318 | * INTERNAL: |
||
| 2319 | * Unschedules an embedded or referenced object for removal. |
||
| 2320 | * |
||
| 2321 | * @ignore |
||
| 2322 | * @param object $document |
||
| 2323 | */ |
||
| 2324 | 112 | public function unscheduleOrphanRemoval($document) |
|
| 2331 | |||
| 2332 | /** |
||
| 2333 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2334 | * 1) sets owner if it was cloned |
||
| 2335 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2336 | * 3) NOP if state is OK |
||
| 2337 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2338 | * |
||
| 2339 | * @param PersistentCollectionInterface $coll |
||
| 2340 | * @param object $document |
||
| 2341 | * @param ClassMetadata $class |
||
| 2342 | * @param string $propName |
||
| 2343 | * @return PersistentCollectionInterface |
||
| 2344 | */ |
||
| 2345 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2365 | |||
| 2366 | /** |
||
| 2367 | * INTERNAL: |
||
| 2368 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2369 | * |
||
| 2370 | * @param PersistentCollectionInterface $coll |
||
| 2371 | */ |
||
| 2372 | 42 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2381 | |||
| 2382 | /** |
||
| 2383 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2384 | * |
||
| 2385 | * @param PersistentCollectionInterface $coll |
||
| 2386 | * @return boolean |
||
| 2387 | */ |
||
| 2388 | 218 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2392 | |||
| 2393 | /** |
||
| 2394 | * INTERNAL: |
||
| 2395 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2396 | * |
||
| 2397 | * @param PersistentCollectionInterface $coll |
||
| 2398 | */ |
||
| 2399 | 218 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2408 | |||
| 2409 | /** |
||
| 2410 | * INTERNAL: |
||
| 2411 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2412 | * |
||
| 2413 | * @param PersistentCollectionInterface $coll |
||
| 2414 | */ |
||
| 2415 | 239 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2430 | |||
| 2431 | /** |
||
| 2432 | * INTERNAL: |
||
| 2433 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2434 | * |
||
| 2435 | * @param PersistentCollectionInterface $coll |
||
| 2436 | */ |
||
| 2437 | 218 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2446 | |||
| 2447 | /** |
||
| 2448 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2449 | * |
||
| 2450 | * @param PersistentCollectionInterface $coll |
||
| 2451 | * @return boolean |
||
| 2452 | */ |
||
| 2453 | 131 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2457 | |||
| 2458 | /** |
||
| 2459 | * INTERNAL: |
||
| 2460 | * Gets PersistentCollections that have been visited during computing change |
||
| 2461 | * set of $document |
||
| 2462 | * |
||
| 2463 | * @param object $document |
||
| 2464 | * @return PersistentCollectionInterface[] |
||
| 2465 | */ |
||
| 2466 | 584 | public function getVisitedCollections($document) |
|
| 2473 | |||
| 2474 | /** |
||
| 2475 | * INTERNAL: |
||
| 2476 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2477 | * |
||
| 2478 | * @param object $document |
||
| 2479 | * @return array |
||
| 2480 | */ |
||
| 2481 | 584 | public function getScheduledCollections($document) |
|
| 2488 | |||
| 2489 | /** |
||
| 2490 | * Checks whether the document is related to a PersistentCollection |
||
| 2491 | * scheduled for update or deletion. |
||
| 2492 | * |
||
| 2493 | * @param object $document |
||
| 2494 | * @return boolean |
||
| 2495 | */ |
||
| 2496 | 52 | public function hasScheduledCollections($document) |
|
| 2500 | |||
| 2501 | /** |
||
| 2502 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2503 | * a collection scheduled for update or deletion. |
||
| 2504 | * |
||
| 2505 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2506 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2507 | * |
||
| 2508 | * If the collection is nested within atomic collection, it is immediately |
||
| 2509 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2510 | * calculating update data way easier. |
||
| 2511 | * |
||
| 2512 | * @param PersistentCollectionInterface $coll |
||
| 2513 | */ |
||
| 2514 | 241 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2537 | |||
| 2538 | /** |
||
| 2539 | * Get the top-most owning document of a given document |
||
| 2540 | * |
||
| 2541 | * If a top-level document is provided, that same document will be returned. |
||
| 2542 | * For an embedded document, we will walk through parent associations until |
||
| 2543 | * we find a top-level document. |
||
| 2544 | * |
||
| 2545 | * @param object $document |
||
| 2546 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2547 | * @return object |
||
| 2548 | */ |
||
| 2549 | 243 | public function getOwningDocument($document) |
|
| 2565 | |||
| 2566 | /** |
||
| 2567 | * Gets the class name for an association (embed or reference) with respect |
||
| 2568 | * to any discriminator value. |
||
| 2569 | * |
||
| 2570 | * @param array $mapping Field mapping for the association |
||
| 2571 | * @param array|null $data Data for the embedded document or reference |
||
| 2572 | * @return string Class name. |
||
| 2573 | */ |
||
| 2574 | 220 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2607 | |||
| 2608 | /** |
||
| 2609 | * INTERNAL: |
||
| 2610 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2611 | * |
||
| 2612 | * @ignore |
||
| 2613 | * @param string $className The name of the document class. |
||
| 2614 | * @param array $data The data for the document. |
||
| 2615 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2616 | * @param object $document The document to be hydrated into in case of creation |
||
| 2617 | * @return object The document instance. |
||
| 2618 | * @internal Highly performance-sensitive method. |
||
| 2619 | */ |
||
| 2620 | 408 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2680 | |||
| 2681 | /** |
||
| 2682 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2683 | * |
||
| 2684 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2685 | */ |
||
| 2686 | 168 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2691 | |||
| 2692 | /** |
||
| 2693 | * Gets the identity map of the UnitOfWork. |
||
| 2694 | * |
||
| 2695 | * @return array |
||
| 2696 | */ |
||
| 2697 | public function getIdentityMap() |
||
| 2701 | |||
| 2702 | /** |
||
| 2703 | * Gets the original data of a document. The original data is the data that was |
||
| 2704 | * present at the time the document was reconstituted from the database. |
||
| 2705 | * |
||
| 2706 | * @param object $document |
||
| 2707 | * @return array |
||
| 2708 | */ |
||
| 2709 | 1 | public function getOriginalDocumentData($document) |
|
| 2717 | |||
| 2718 | /** |
||
| 2719 | * @ignore |
||
| 2720 | */ |
||
| 2721 | 55 | public function setOriginalDocumentData($document, array $data) |
|
| 2727 | |||
| 2728 | /** |
||
| 2729 | * INTERNAL: |
||
| 2730 | * Sets a property value of the original data array of a document. |
||
| 2731 | * |
||
| 2732 | * @ignore |
||
| 2733 | * @param string $oid |
||
| 2734 | * @param string $property |
||
| 2735 | * @param mixed $value |
||
| 2736 | */ |
||
| 2737 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2741 | |||
| 2742 | /** |
||
| 2743 | * Gets the identifier of a document. |
||
| 2744 | * |
||
| 2745 | * @param object $document |
||
| 2746 | * @return mixed The identifier value |
||
| 2747 | */ |
||
| 2748 | 426 | public function getDocumentIdentifier($document) |
|
| 2753 | |||
| 2754 | /** |
||
| 2755 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2756 | * |
||
| 2757 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2758 | */ |
||
| 2759 | public function hasPendingInsertions() |
||
| 2763 | |||
| 2764 | /** |
||
| 2765 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2766 | * number of documents in the identity map. |
||
| 2767 | * |
||
| 2768 | * @return integer |
||
| 2769 | */ |
||
| 2770 | 2 | public function size() |
|
| 2778 | |||
| 2779 | /** |
||
| 2780 | * INTERNAL: |
||
| 2781 | * Registers a document as managed. |
||
| 2782 | * |
||
| 2783 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2784 | * document class. If the class expects its database identifier to be a |
||
| 2785 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2786 | * document identifiers map will become inconsistent with the identity map. |
||
| 2787 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2788 | * conversion and throw an exception if it's inconsistent. |
||
| 2789 | * |
||
| 2790 | * @param object $document The document. |
||
| 2791 | * @param array $id The identifier values. |
||
| 2792 | * @param array $data The original document data. |
||
| 2793 | */ |
||
| 2794 | 391 | public function registerManaged($document, $id, array $data) |
|
| 2809 | |||
| 2810 | /** |
||
| 2811 | * INTERNAL: |
||
| 2812 | * Clears the property changeset of the document with the given OID. |
||
| 2813 | * |
||
| 2814 | * @param string $oid The document's OID. |
||
| 2815 | */ |
||
| 2816 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2820 | |||
| 2821 | /* PropertyChangedListener implementation */ |
||
| 2822 | |||
| 2823 | /** |
||
| 2824 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2825 | * |
||
| 2826 | * @param object $document The document that owns the property. |
||
| 2827 | * @param string $propertyName The name of the property that changed. |
||
| 2828 | * @param mixed $oldValue The old value of the property. |
||
| 2829 | * @param mixed $newValue The new value of the property. |
||
| 2830 | */ |
||
| 2831 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2846 | |||
| 2847 | /** |
||
| 2848 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2849 | * |
||
| 2850 | * @return array |
||
| 2851 | */ |
||
| 2852 | 5 | public function getScheduledDocumentInsertions() |
|
| 2856 | |||
| 2857 | /** |
||
| 2858 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2859 | * |
||
| 2860 | * @return array |
||
| 2861 | */ |
||
| 2862 | 3 | public function getScheduledDocumentUpserts() |
|
| 2866 | |||
| 2867 | /** |
||
| 2868 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2869 | * |
||
| 2870 | * @return array |
||
| 2871 | */ |
||
| 2872 | 3 | public function getScheduledDocumentUpdates() |
|
| 2876 | |||
| 2877 | /** |
||
| 2878 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2879 | * |
||
| 2880 | * @return array |
||
| 2881 | */ |
||
| 2882 | public function getScheduledDocumentDeletions() |
||
| 2886 | |||
| 2887 | /** |
||
| 2888 | * Get the currently scheduled complete collection deletions |
||
| 2889 | * |
||
| 2890 | * @return array |
||
| 2891 | */ |
||
| 2892 | public function getScheduledCollectionDeletions() |
||
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2899 | * |
||
| 2900 | * @return array |
||
| 2901 | */ |
||
| 2902 | public function getScheduledCollectionUpdates() |
||
| 2906 | |||
| 2907 | /** |
||
| 2908 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2909 | * |
||
| 2910 | * @param object |
||
| 2911 | * @return void |
||
| 2912 | */ |
||
| 2913 | public function initializeObject($obj) |
||
| 2921 | |||
| 2922 | 1 | private function objToStr($obj) |
|
| 2926 | } |
||
| 2927 |
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.