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 | * @var int |
||
| 265 | */ |
||
| 266 | private $commitsInProgress = 0; |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 270 | * |
||
| 271 | * @param DocumentManager $dm |
||
| 272 | * @param EventManager $evm |
||
| 273 | * @param HydratorFactory $hydratorFactory |
||
| 274 | */ |
||
| 275 | 1006 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 285 | * queries for insert persistence. |
||
| 286 | * |
||
| 287 | * @return PersistenceBuilder $pb |
||
| 288 | */ |
||
| 289 | 748 | public function getPersistenceBuilder() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Sets the parent association for a given embedded document. |
||
| 299 | * |
||
| 300 | * @param object $document |
||
| 301 | * @param array $mapping |
||
| 302 | * @param object $parent |
||
| 303 | * @param string $propertyPath |
||
| 304 | */ |
||
| 305 | 202 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the parent association for a given embedded document. |
||
| 314 | * |
||
| 315 | * <code> |
||
| 316 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 317 | * </code> |
||
| 318 | * |
||
| 319 | * @param object $document |
||
| 320 | * @return array $association |
||
| 321 | */ |
||
| 322 | 230 | public function getParentAssociation($document) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get the document persister instance for the given document name |
||
| 333 | * |
||
| 334 | * @param string $documentName |
||
| 335 | * @return Persisters\DocumentPersister |
||
| 336 | */ |
||
| 337 | 746 | public function getDocumentPersister($documentName) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get the collection persister instance. |
||
| 349 | * |
||
| 350 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 351 | */ |
||
| 352 | 746 | public function getCollectionPersister() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Set the document persister instance to use for the given document name |
||
| 363 | * |
||
| 364 | * @param string $documentName |
||
| 365 | * @param Persisters\DocumentPersister $persister |
||
| 366 | */ |
||
| 367 | 13 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 374 | * up to this point. The state of all managed documents will be synchronized with |
||
| 375 | * the database. |
||
| 376 | * |
||
| 377 | * The operations are executed in the following order: |
||
| 378 | * |
||
| 379 | * 1) All document insertions |
||
| 380 | * 2) All document updates |
||
| 381 | * 3) All document deletions |
||
| 382 | * |
||
| 383 | * @param object $document |
||
| 384 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 385 | */ |
||
| 386 | 605 | public function commit($document = null, array $options = array()) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Groups a list of scheduled documents by their class. |
||
| 475 | * |
||
| 476 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 477 | * @param bool $includeEmbedded |
||
| 478 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 479 | */ |
||
| 480 | 600 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Compute changesets of all documents scheduled for insertion. |
||
| 512 | * |
||
| 513 | * Embedded documents will not be processed. |
||
| 514 | */ |
||
| 515 | 608 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 524 | |||
| 525 | /** |
||
| 526 | * Compute changesets of all documents scheduled for upsert. |
||
| 527 | * |
||
| 528 | * Embedded documents will not be processed. |
||
| 529 | */ |
||
| 530 | 607 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 539 | |||
| 540 | /** |
||
| 541 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 542 | * |
||
| 543 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 544 | * 2. Proxies are skipped. |
||
| 545 | * 3. Only if document is properly managed. |
||
| 546 | * |
||
| 547 | * @param object $document |
||
| 548 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 549 | * @return void |
||
| 550 | */ |
||
| 551 | 12 | private function computeSingleDocumentChangeSet($document) |
|
| 552 | { |
||
| 553 | 12 | $state = $this->getDocumentState($document); |
|
| 554 | |||
| 555 | 12 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
|
| 556 | throw new \InvalidArgumentException('Document has to be managed or scheduled for removal for single computation ' . $this->objToStr($document)); |
||
| 557 | } |
||
| 558 | |||
| 559 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 560 | |||
| 561 | 12 | if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
|
| 562 | 10 | $this->persist($document); |
|
| 563 | } |
||
| 564 | |||
| 565 | // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case. |
||
| 566 | 12 | $this->computeScheduleInsertsChangeSets(); |
|
| 567 | 12 | $this->computeScheduleUpsertsChangeSets(); |
|
| 568 | |||
| 569 | // Ignore uninitialized proxy objects |
||
| 570 | 12 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 571 | return; |
||
| 572 | } |
||
| 573 | |||
| 574 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 575 | 12 | $oid = spl_object_hash($document); |
|
| 576 | |||
| 577 | 12 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 578 | 12 | && ! isset($this->documentUpserts[$oid]) |
|
| 579 | 12 | && ! isset($this->documentDeletions[$oid]) |
|
| 580 | 12 | && isset($this->documentStates[$oid]) |
|
| 581 | ) { |
||
| 582 | 7 | $this->computeChangeSet($class, $document); |
|
| 583 | } |
||
| 584 | 12 | } |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Gets the changeset for a document. |
||
| 588 | * |
||
| 589 | * @param object $document |
||
| 590 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 591 | */ |
||
| 592 | 601 | public function getDocumentChangeSet($document) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * INTERNAL: |
||
| 603 | * Sets the changeset for a document. |
||
| 604 | * |
||
| 605 | * @param object $document |
||
| 606 | * @param array $changeset |
||
| 607 | */ |
||
| 608 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 615 | * |
||
| 616 | * @param object $document |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | 608 | public function getDocumentActualData($document) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Computes the changes that happened to a single document. |
||
| 656 | * |
||
| 657 | * Modifies/populates the following properties: |
||
| 658 | * |
||
| 659 | * {@link originalDocumentData} |
||
| 660 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 661 | * then it was not fetched from the database and therefore we have no original |
||
| 662 | * document data yet. All of the current document data is stored as the original document data. |
||
| 663 | * |
||
| 664 | * {@link documentChangeSets} |
||
| 665 | * The changes detected on all properties of the document are stored there. |
||
| 666 | * A change is a tuple array where the first entry is the old value and the second |
||
| 667 | * entry is the new value of the property. Changesets are used by persisters |
||
| 668 | * to INSERT/UPDATE the persistent document state. |
||
| 669 | * |
||
| 670 | * {@link documentUpdates} |
||
| 671 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 672 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 673 | * there to mark it for an update. |
||
| 674 | * |
||
| 675 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 676 | * @param object $document The document for which to compute the changes. |
||
| 677 | */ |
||
| 678 | 605 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 694 | * |
||
| 695 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 696 | * @param object $document |
||
| 697 | * @param boolean $recompute |
||
| 698 | */ |
||
| 699 | 605 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Computes all the changes that have been done to documents and collections |
||
| 876 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 877 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 878 | */ |
||
| 879 | 604 | public function computeChangeSets() |
|
| 929 | |||
| 930 | /** |
||
| 931 | * Computes the changes of an association. |
||
| 932 | * |
||
| 933 | * @param object $parentDocument |
||
| 934 | * @param array $assoc |
||
| 935 | * @param mixed $value The value of the association. |
||
| 936 | * @throws \InvalidArgumentException |
||
| 937 | */ |
||
| 938 | 449 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1043 | |||
| 1044 | /** |
||
| 1045 | * INTERNAL: |
||
| 1046 | * Computes the changeset of an individual document, independently of the |
||
| 1047 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1048 | * |
||
| 1049 | * The passed document must be a managed document. If the document already has a change set |
||
| 1050 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1051 | * whereby changes detected in this method prevail. |
||
| 1052 | * |
||
| 1053 | * @ignore |
||
| 1054 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1055 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1056 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1057 | */ |
||
| 1058 | 18 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * @param ClassMetadata $class |
||
| 1080 | * @param object $document |
||
| 1081 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1082 | */ |
||
| 1083 | 635 | private function persistNew(ClassMetadata $class, $document) |
|
| 1126 | |||
| 1127 | /** |
||
| 1128 | * Executes all document insertions for documents of the specified type. |
||
| 1129 | * |
||
| 1130 | * @param ClassMetadata $class |
||
| 1131 | * @param array $documents Array of documents to insert |
||
| 1132 | * @param array $options Array of options to be used with batchInsert() |
||
| 1133 | */ |
||
| 1134 | 526 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1149 | |||
| 1150 | /** |
||
| 1151 | * Executes all document upserts for documents of the specified type. |
||
| 1152 | * |
||
| 1153 | * @param ClassMetadata $class |
||
| 1154 | * @param array $documents Array of documents to upsert |
||
| 1155 | * @param array $options Array of options to be used with batchInsert() |
||
| 1156 | */ |
||
| 1157 | 84 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Executes all document updates for documents of the specified type. |
||
| 1176 | * |
||
| 1177 | * @param Mapping\ClassMetadata $class |
||
| 1178 | * @param array $documents Array of documents to update |
||
| 1179 | * @param array $options Array of options to be used with update() |
||
| 1180 | */ |
||
| 1181 | 229 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Executes all document deletions for documents of the specified type. |
||
| 1205 | * |
||
| 1206 | * @param ClassMetadata $class |
||
| 1207 | * @param array $documents Array of documents to delete |
||
| 1208 | * @param array $options Array of options to be used with remove() |
||
| 1209 | */ |
||
| 1210 | 70 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Schedules a document for insertion into the database. |
||
| 1245 | * If the document already has an identifier, it will be added to the |
||
| 1246 | * identity map. |
||
| 1247 | * |
||
| 1248 | * @param ClassMetadata $class |
||
| 1249 | * @param object $document The document to schedule for insertion. |
||
| 1250 | * @throws \InvalidArgumentException |
||
| 1251 | */ |
||
| 1252 | 567 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1272 | |||
| 1273 | /** |
||
| 1274 | * Schedules a document for upsert into the database and adds it to the |
||
| 1275 | * identity map |
||
| 1276 | * |
||
| 1277 | * @param ClassMetadata $class |
||
| 1278 | * @param object $document The document to schedule for upsert. |
||
| 1279 | * @throws \InvalidArgumentException |
||
| 1280 | */ |
||
| 1281 | 91 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Checks whether a document is scheduled for insertion. |
||
| 1305 | * |
||
| 1306 | * @param object $document |
||
| 1307 | * @return boolean |
||
| 1308 | */ |
||
| 1309 | 107 | public function isScheduledForInsert($document) |
|
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Checks whether a document is scheduled for upsert. |
||
| 1316 | * |
||
| 1317 | * @param object $document |
||
| 1318 | * @return boolean |
||
| 1319 | */ |
||
| 1320 | 5 | public function isScheduledForUpsert($document) |
|
| 1324 | |||
| 1325 | /** |
||
| 1326 | * Schedules a document for being updated. |
||
| 1327 | * |
||
| 1328 | * @param object $document The document to schedule for being updated. |
||
| 1329 | * @throws \InvalidArgumentException |
||
| 1330 | */ |
||
| 1331 | 235 | public function scheduleForUpdate($document) |
|
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1351 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1352 | * at commit time. |
||
| 1353 | * |
||
| 1354 | * @param object $document |
||
| 1355 | * @return boolean |
||
| 1356 | */ |
||
| 1357 | 20 | public function isScheduledForUpdate($document) |
|
| 1361 | |||
| 1362 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1367 | |||
| 1368 | /** |
||
| 1369 | * INTERNAL: |
||
| 1370 | * Schedules a document for deletion. |
||
| 1371 | * |
||
| 1372 | * @param object $document |
||
| 1373 | */ |
||
| 1374 | 75 | public function scheduleForDelete($document) |
|
| 1400 | |||
| 1401 | /** |
||
| 1402 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1403 | * of work. |
||
| 1404 | * |
||
| 1405 | * @param object $document |
||
| 1406 | * @return boolean |
||
| 1407 | */ |
||
| 1408 | 8 | public function isScheduledForDelete($document) |
|
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1415 | * |
||
| 1416 | * @param $document |
||
| 1417 | * @return boolean |
||
| 1418 | */ |
||
| 1419 | 251 | public function isDocumentScheduled($document) |
|
| 1427 | |||
| 1428 | /** |
||
| 1429 | * INTERNAL: |
||
| 1430 | * Registers a document in the identity map. |
||
| 1431 | * |
||
| 1432 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1433 | * the root document. Identifiers are serialized before being used as array |
||
| 1434 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1435 | * |
||
| 1436 | * @ignore |
||
| 1437 | * @param object $document The document to register. |
||
| 1438 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1439 | * the document in question is already managed. |
||
| 1440 | */ |
||
| 1441 | 665 | public function addToIdentityMap($document) |
|
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Gets the state of a document with regard to the current unit of work. |
||
| 1462 | * |
||
| 1463 | * @param object $document |
||
| 1464 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1465 | * This parameter can be set to improve performance of document state detection |
||
| 1466 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1467 | * is either known or does not matter for the caller of the method. |
||
| 1468 | * @return int The document state. |
||
| 1469 | */ |
||
| 1470 | 637 | public function getDocumentState($document, $assume = null) |
|
| 1520 | |||
| 1521 | /** |
||
| 1522 | * INTERNAL: |
||
| 1523 | * Removes a document from the identity map. This effectively detaches the |
||
| 1524 | * document from the persistence management of Doctrine. |
||
| 1525 | * |
||
| 1526 | * @ignore |
||
| 1527 | * @param object $document |
||
| 1528 | * @throws \InvalidArgumentException |
||
| 1529 | * @return boolean |
||
| 1530 | */ |
||
| 1531 | 87 | public function removeFromIdentityMap($document) |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * INTERNAL: |
||
| 1554 | * Gets a document in the identity map by its identifier hash. |
||
| 1555 | * |
||
| 1556 | * @ignore |
||
| 1557 | * @param mixed $id Document identifier |
||
| 1558 | * @param ClassMetadata $class Document class |
||
| 1559 | * @return object |
||
| 1560 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1561 | */ |
||
| 1562 | 34 | public function getById($id, ClassMetadata $class) |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * INTERNAL: |
||
| 1575 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1576 | * for the given hash, FALSE is returned. |
||
| 1577 | * |
||
| 1578 | * @ignore |
||
| 1579 | * @param mixed $id Document identifier |
||
| 1580 | * @param ClassMetadata $class Document class |
||
| 1581 | * @return mixed The found document or FALSE. |
||
| 1582 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1583 | */ |
||
| 1584 | 312 | public function tryGetById($id, ClassMetadata $class) |
|
| 1595 | |||
| 1596 | /** |
||
| 1597 | * Schedules a document for dirty-checking at commit-time. |
||
| 1598 | * |
||
| 1599 | * @param object $document The document to schedule for dirty-checking. |
||
| 1600 | * @todo Rename: scheduleForSynchronization |
||
| 1601 | */ |
||
| 1602 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Checks whether a document is registered in the identity map. |
||
| 1610 | * |
||
| 1611 | * @param object $document |
||
| 1612 | * @return boolean |
||
| 1613 | */ |
||
| 1614 | 86 | public function isInIdentityMap($document) |
|
| 1627 | |||
| 1628 | /** |
||
| 1629 | * @param object $document |
||
| 1630 | * @return string |
||
| 1631 | */ |
||
| 1632 | 665 | private function getIdForIdentityMap($document) |
|
| 1645 | |||
| 1646 | /** |
||
| 1647 | * INTERNAL: |
||
| 1648 | * Checks whether an identifier exists in the identity map. |
||
| 1649 | * |
||
| 1650 | * @ignore |
||
| 1651 | * @param string $id |
||
| 1652 | * @param string $rootClassName |
||
| 1653 | * @return boolean |
||
| 1654 | */ |
||
| 1655 | public function containsId($id, $rootClassName) |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Persists a document as part of the current unit of work. |
||
| 1662 | * |
||
| 1663 | * @param object $document The document to persist. |
||
| 1664 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1665 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1666 | */ |
||
| 1667 | 636 | public function persist($document) |
|
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Saves a document as part of the current unit of work. |
||
| 1679 | * This method is internally called during save() cascades as it tracks |
||
| 1680 | * the already visited documents to prevent infinite recursions. |
||
| 1681 | * |
||
| 1682 | * NOTE: This method always considers documents that are not yet known to |
||
| 1683 | * this UnitOfWork as NEW. |
||
| 1684 | * |
||
| 1685 | * @param object $document The document to persist. |
||
| 1686 | * @param array $visited The already visited documents. |
||
| 1687 | * @throws \InvalidArgumentException |
||
| 1688 | * @throws MongoDBException |
||
| 1689 | */ |
||
| 1690 | 635 | private function doPersist($document, array &$visited) |
|
| 1730 | |||
| 1731 | /** |
||
| 1732 | * Deletes a document as part of the current unit of work. |
||
| 1733 | * |
||
| 1734 | * @param object $document The document to remove. |
||
| 1735 | */ |
||
| 1736 | 74 | public function remove($document) |
|
| 1741 | |||
| 1742 | /** |
||
| 1743 | * Deletes a document as part of the current unit of work. |
||
| 1744 | * |
||
| 1745 | * This method is internally called during delete() cascades as it tracks |
||
| 1746 | * the already visited documents to prevent infinite recursions. |
||
| 1747 | * |
||
| 1748 | * @param object $document The document to delete. |
||
| 1749 | * @param array $visited The map of the already visited documents. |
||
| 1750 | * @throws MongoDBException |
||
| 1751 | */ |
||
| 1752 | 74 | private function doRemove($document, array &$visited) |
|
| 1784 | |||
| 1785 | /** |
||
| 1786 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1787 | * |
||
| 1788 | * @param object $document |
||
| 1789 | * @return object The managed copy of the document. |
||
| 1790 | */ |
||
| 1791 | 12 | public function merge($document) |
|
| 1797 | |||
| 1798 | /** |
||
| 1799 | * Executes a merge operation on a document. |
||
| 1800 | * |
||
| 1801 | * @param object $document |
||
| 1802 | * @param array $visited |
||
| 1803 | * @param object|null $prevManagedCopy |
||
| 1804 | * @param array|null $assoc |
||
| 1805 | * |
||
| 1806 | * @return object The managed copy of the document. |
||
| 1807 | * |
||
| 1808 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1809 | * @throws LockException If the document uses optimistic locking through a |
||
| 1810 | * version attribute and the version check against the |
||
| 1811 | * managed copy fails. |
||
| 1812 | */ |
||
| 1813 | 12 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1987 | |||
| 1988 | /** |
||
| 1989 | * Detaches a document from the persistence management. It's persistence will |
||
| 1990 | * no longer be managed by Doctrine. |
||
| 1991 | * |
||
| 1992 | * @param object $document The document to detach. |
||
| 1993 | */ |
||
| 1994 | 11 | public function detach($document) |
|
| 1999 | |||
| 2000 | /** |
||
| 2001 | * Executes a detach operation on the given document. |
||
| 2002 | * |
||
| 2003 | * @param object $document |
||
| 2004 | * @param array $visited |
||
| 2005 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 2006 | */ |
||
| 2007 | 16 | private function doDetach($document, array &$visited) |
|
| 2032 | |||
| 2033 | /** |
||
| 2034 | * Refreshes the state of the given document from the database, overwriting |
||
| 2035 | * any local, unpersisted changes. |
||
| 2036 | * |
||
| 2037 | * @param object $document The document to refresh. |
||
| 2038 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2039 | */ |
||
| 2040 | 22 | public function refresh($document) |
|
| 2045 | |||
| 2046 | /** |
||
| 2047 | * Executes a refresh operation on a document. |
||
| 2048 | * |
||
| 2049 | * @param object $document The document to refresh. |
||
| 2050 | * @param array $visited The already visited documents during cascades. |
||
| 2051 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2052 | */ |
||
| 2053 | 22 | private function doRefresh($document, array &$visited) |
|
| 2075 | |||
| 2076 | /** |
||
| 2077 | * Cascades a refresh operation to associated documents. |
||
| 2078 | * |
||
| 2079 | * @param object $document |
||
| 2080 | * @param array $visited |
||
| 2081 | */ |
||
| 2082 | 21 | private function cascadeRefresh($document, array &$visited) |
|
| 2106 | |||
| 2107 | /** |
||
| 2108 | * Cascades a detach operation to associated documents. |
||
| 2109 | * |
||
| 2110 | * @param object $document |
||
| 2111 | * @param array $visited |
||
| 2112 | */ |
||
| 2113 | 16 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2134 | /** |
||
| 2135 | * Cascades a merge operation to associated documents. |
||
| 2136 | * |
||
| 2137 | * @param object $document |
||
| 2138 | * @param object $managedCopy |
||
| 2139 | * @param array $visited |
||
| 2140 | */ |
||
| 2141 | 12 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2167 | |||
| 2168 | /** |
||
| 2169 | * Cascades the save operation to associated documents. |
||
| 2170 | * |
||
| 2171 | * @param object $document |
||
| 2172 | * @param array $visited |
||
| 2173 | */ |
||
| 2174 | 633 | private function cascadePersist($document, array &$visited) |
|
| 2221 | |||
| 2222 | /** |
||
| 2223 | * Cascades the delete operation to associated documents. |
||
| 2224 | * |
||
| 2225 | * @param object $document |
||
| 2226 | * @param array $visited |
||
| 2227 | */ |
||
| 2228 | 74 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2250 | |||
| 2251 | /** |
||
| 2252 | * Acquire a lock on the given document. |
||
| 2253 | * |
||
| 2254 | * @param object $document |
||
| 2255 | * @param int $lockMode |
||
| 2256 | * @param int $lockVersion |
||
| 2257 | * @throws LockException |
||
| 2258 | * @throws \InvalidArgumentException |
||
| 2259 | */ |
||
| 2260 | 5 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2261 | { |
||
| 2262 | 5 | if ($this->getDocumentState($document) != self::STATE_MANAGED) { |
|
| 2263 | throw new \InvalidArgumentException('Document is not MANAGED.'); |
||
| 2264 | } |
||
| 2265 | |||
| 2266 | 5 | $documentName = get_class($document); |
|
| 2267 | 5 | $class = $this->dm->getClassMetadata($documentName); |
|
| 2268 | |||
| 2269 | 5 | if ($lockMode == LockMode::OPTIMISTIC) { |
|
| 2270 | if ( ! $class->isVersioned) { |
||
| 2271 | throw LockException::notVersioned($documentName); |
||
| 2272 | } |
||
| 2273 | |||
| 2274 | if ($lockVersion != null) { |
||
| 2275 | $documentVersion = $class->reflFields[$class->versionField]->getValue($document); |
||
| 2276 | if ($documentVersion != $lockVersion) { |
||
| 2277 | throw LockException::lockFailedVersionMissmatch($document, $lockVersion, $documentVersion); |
||
| 2278 | } |
||
| 2279 | } |
||
| 2280 | 5 | } elseif (in_array($lockMode, array(LockMode::PESSIMISTIC_READ, LockMode::PESSIMISTIC_WRITE))) { |
|
| 2281 | 5 | $this->getDocumentPersister($class->name)->lock($document, $lockMode); |
|
| 2282 | } |
||
| 2283 | 5 | } |
|
| 2284 | |||
| 2285 | /** |
||
| 2286 | * Releases a lock on the given document. |
||
| 2287 | * |
||
| 2288 | * @param object $document |
||
| 2289 | * @throws \InvalidArgumentException |
||
| 2290 | */ |
||
| 2291 | 1 | public function unlock($document) |
|
| 2299 | |||
| 2300 | /** |
||
| 2301 | * Clears the UnitOfWork. |
||
| 2302 | * |
||
| 2303 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2304 | */ |
||
| 2305 | 418 | public function clear($documentName = null) |
|
| 2339 | |||
| 2340 | /** |
||
| 2341 | * INTERNAL: |
||
| 2342 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2343 | * invoked on that document at the beginning of the next commit of this |
||
| 2344 | * UnitOfWork. |
||
| 2345 | * |
||
| 2346 | * @ignore |
||
| 2347 | * @param object $document |
||
| 2348 | */ |
||
| 2349 | 53 | public function scheduleOrphanRemoval($document) |
|
| 2353 | |||
| 2354 | /** |
||
| 2355 | * INTERNAL: |
||
| 2356 | * Unschedules an embedded or referenced object for removal. |
||
| 2357 | * |
||
| 2358 | * @ignore |
||
| 2359 | * @param object $document |
||
| 2360 | */ |
||
| 2361 | 111 | public function unscheduleOrphanRemoval($document) |
|
| 2368 | |||
| 2369 | /** |
||
| 2370 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2371 | * 1) sets owner if it was cloned |
||
| 2372 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2373 | * 3) NOP if state is OK |
||
| 2374 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2375 | * |
||
| 2376 | * @param PersistentCollectionInterface $coll |
||
| 2377 | * @param object $document |
||
| 2378 | * @param ClassMetadata $class |
||
| 2379 | * @param string $propName |
||
| 2380 | * @return PersistentCollectionInterface |
||
| 2381 | */ |
||
| 2382 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2402 | |||
| 2403 | /** |
||
| 2404 | * INTERNAL: |
||
| 2405 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2406 | * |
||
| 2407 | * @param PersistentCollectionInterface $coll |
||
| 2408 | */ |
||
| 2409 | 43 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2418 | |||
| 2419 | /** |
||
| 2420 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2421 | * |
||
| 2422 | * @param PersistentCollectionInterface $coll |
||
| 2423 | * @return boolean |
||
| 2424 | */ |
||
| 2425 | 210 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2429 | |||
| 2430 | /** |
||
| 2431 | * INTERNAL: |
||
| 2432 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2433 | * |
||
| 2434 | * @param PersistentCollectionInterface $coll |
||
| 2435 | */ |
||
| 2436 | 226 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2445 | |||
| 2446 | /** |
||
| 2447 | * INTERNAL: |
||
| 2448 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2449 | * |
||
| 2450 | * @param PersistentCollectionInterface $coll |
||
| 2451 | */ |
||
| 2452 | 248 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2467 | |||
| 2468 | /** |
||
| 2469 | * INTERNAL: |
||
| 2470 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2471 | * |
||
| 2472 | * @param PersistentCollectionInterface $coll |
||
| 2473 | */ |
||
| 2474 | 226 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2483 | |||
| 2484 | /** |
||
| 2485 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2486 | * |
||
| 2487 | * @param PersistentCollectionInterface $coll |
||
| 2488 | * @return boolean |
||
| 2489 | */ |
||
| 2490 | 132 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2494 | |||
| 2495 | /** |
||
| 2496 | * INTERNAL: |
||
| 2497 | * Gets PersistentCollections that have been visited during computing change |
||
| 2498 | * set of $document |
||
| 2499 | * |
||
| 2500 | * @param object $document |
||
| 2501 | * @return PersistentCollectionInterface[] |
||
| 2502 | */ |
||
| 2503 | 586 | public function getVisitedCollections($document) |
|
| 2510 | |||
| 2511 | /** |
||
| 2512 | * INTERNAL: |
||
| 2513 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2514 | * |
||
| 2515 | * @param object $document |
||
| 2516 | * @return array |
||
| 2517 | */ |
||
| 2518 | 586 | public function getScheduledCollections($document) |
|
| 2525 | |||
| 2526 | /** |
||
| 2527 | * Checks whether the document is related to a PersistentCollection |
||
| 2528 | * scheduled for update or deletion. |
||
| 2529 | * |
||
| 2530 | * @param object $document |
||
| 2531 | * @return boolean |
||
| 2532 | */ |
||
| 2533 | 51 | public function hasScheduledCollections($document) |
|
| 2537 | |||
| 2538 | /** |
||
| 2539 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2540 | * a collection scheduled for update or deletion. |
||
| 2541 | * |
||
| 2542 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2543 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2544 | * |
||
| 2545 | * If the collection is nested within atomic collection, it is immediately |
||
| 2546 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2547 | * calculating update data way easier. |
||
| 2548 | * |
||
| 2549 | * @param PersistentCollectionInterface $coll |
||
| 2550 | */ |
||
| 2551 | 250 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2574 | |||
| 2575 | /** |
||
| 2576 | * Get the top-most owning document of a given document |
||
| 2577 | * |
||
| 2578 | * If a top-level document is provided, that same document will be returned. |
||
| 2579 | * For an embedded document, we will walk through parent associations until |
||
| 2580 | * we find a top-level document. |
||
| 2581 | * |
||
| 2582 | * @param object $document |
||
| 2583 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2584 | * @return object |
||
| 2585 | */ |
||
| 2586 | 252 | public function getOwningDocument($document) |
|
| 2602 | |||
| 2603 | /** |
||
| 2604 | * Gets the class name for an association (embed or reference) with respect |
||
| 2605 | * to any discriminator value. |
||
| 2606 | * |
||
| 2607 | * @param array $mapping Field mapping for the association |
||
| 2608 | * @param array|null $data Data for the embedded document or reference |
||
| 2609 | * @return string Class name. |
||
| 2610 | */ |
||
| 2611 | 234 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2644 | |||
| 2645 | /** |
||
| 2646 | * INTERNAL: |
||
| 2647 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2648 | * |
||
| 2649 | * @ignore |
||
| 2650 | * @param string $className The name of the document class. |
||
| 2651 | * @param array $data The data for the document. |
||
| 2652 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2653 | * @param object $document The document to be hydrated into in case of creation |
||
| 2654 | * @return object The document instance. |
||
| 2655 | * @internal Highly performance-sensitive method. |
||
| 2656 | */ |
||
| 2657 | 423 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2729 | |||
| 2730 | /** |
||
| 2731 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2732 | * |
||
| 2733 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2734 | */ |
||
| 2735 | 171 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2740 | |||
| 2741 | /** |
||
| 2742 | * Gets the identity map of the UnitOfWork. |
||
| 2743 | * |
||
| 2744 | * @return array |
||
| 2745 | */ |
||
| 2746 | public function getIdentityMap() |
||
| 2750 | |||
| 2751 | /** |
||
| 2752 | * Gets the original data of a document. The original data is the data that was |
||
| 2753 | * present at the time the document was reconstituted from the database. |
||
| 2754 | * |
||
| 2755 | * @param object $document |
||
| 2756 | * @return array |
||
| 2757 | */ |
||
| 2758 | 1 | public function getOriginalDocumentData($document) |
|
| 2766 | |||
| 2767 | /** |
||
| 2768 | * @ignore |
||
| 2769 | */ |
||
| 2770 | 55 | public function setOriginalDocumentData($document, array $data) |
|
| 2776 | |||
| 2777 | /** |
||
| 2778 | * INTERNAL: |
||
| 2779 | * Sets a property value of the original data array of a document. |
||
| 2780 | * |
||
| 2781 | * @ignore |
||
| 2782 | * @param string $oid |
||
| 2783 | * @param string $property |
||
| 2784 | * @param mixed $value |
||
| 2785 | */ |
||
| 2786 | 6 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2790 | |||
| 2791 | /** |
||
| 2792 | * Gets the identifier of a document. |
||
| 2793 | * |
||
| 2794 | * @param object $document |
||
| 2795 | * @return mixed The identifier value |
||
| 2796 | */ |
||
| 2797 | 439 | public function getDocumentIdentifier($document) |
|
| 2802 | |||
| 2803 | /** |
||
| 2804 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2805 | * |
||
| 2806 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2807 | */ |
||
| 2808 | public function hasPendingInsertions() |
||
| 2812 | |||
| 2813 | /** |
||
| 2814 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2815 | * number of documents in the identity map. |
||
| 2816 | * |
||
| 2817 | * @return integer |
||
| 2818 | */ |
||
| 2819 | 2 | public function size() |
|
| 2827 | |||
| 2828 | /** |
||
| 2829 | * INTERNAL: |
||
| 2830 | * Registers a document as managed. |
||
| 2831 | * |
||
| 2832 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2833 | * document class. If the class expects its database identifier to be a |
||
| 2834 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2835 | * document identifiers map will become inconsistent with the identity map. |
||
| 2836 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2837 | * conversion and throw an exception if it's inconsistent. |
||
| 2838 | * |
||
| 2839 | * @param object $document The document. |
||
| 2840 | * @param array $id The identifier values. |
||
| 2841 | * @param array $data The original document data. |
||
| 2842 | */ |
||
| 2843 | 404 | public function registerManaged($document, $id, array $data) |
|
| 2858 | |||
| 2859 | /** |
||
| 2860 | * INTERNAL: |
||
| 2861 | * Clears the property changeset of the document with the given OID. |
||
| 2862 | * |
||
| 2863 | * @param string $oid The document's OID. |
||
| 2864 | */ |
||
| 2865 | public function clearDocumentChangeSet($oid) |
||
| 2866 | { |
||
| 2867 | $this->documentChangeSets[$oid] = array(); |
||
| 2868 | } |
||
| 2869 | |||
| 2870 | /* PropertyChangedListener implementation */ |
||
| 2871 | |||
| 2872 | /** |
||
| 2873 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2874 | * |
||
| 2875 | * @param object $document The document that owns the property. |
||
| 2876 | * @param string $propertyName The name of the property that changed. |
||
| 2877 | * @param mixed $oldValue The old value of the property. |
||
| 2878 | * @param mixed $newValue The new value of the property. |
||
| 2879 | */ |
||
| 2880 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2895 | |||
| 2896 | /** |
||
| 2897 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2898 | * |
||
| 2899 | * @return array |
||
| 2900 | */ |
||
| 2901 | 2 | public function getScheduledDocumentInsertions() |
|
| 2905 | |||
| 2906 | /** |
||
| 2907 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2908 | * |
||
| 2909 | * @return array |
||
| 2910 | */ |
||
| 2911 | 1 | public function getScheduledDocumentUpserts() |
|
| 2915 | |||
| 2916 | /** |
||
| 2917 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2918 | * |
||
| 2919 | * @return array |
||
| 2920 | */ |
||
| 2921 | 2 | public function getScheduledDocumentUpdates() |
|
| 2925 | |||
| 2926 | /** |
||
| 2927 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2928 | * |
||
| 2929 | * @return array |
||
| 2930 | */ |
||
| 2931 | public function getScheduledDocumentDeletions() |
||
| 2935 | |||
| 2936 | /** |
||
| 2937 | * Get the currently scheduled complete collection deletions |
||
| 2938 | * |
||
| 2939 | * @return array |
||
| 2940 | */ |
||
| 2941 | public function getScheduledCollectionDeletions() |
||
| 2945 | |||
| 2946 | /** |
||
| 2947 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2948 | * |
||
| 2949 | * @return array |
||
| 2950 | */ |
||
| 2951 | public function getScheduledCollectionUpdates() |
||
| 2955 | |||
| 2956 | /** |
||
| 2957 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2958 | * |
||
| 2959 | * @param object |
||
| 2960 | * @return void |
||
| 2961 | */ |
||
| 2962 | public function initializeObject($obj) |
||
| 2970 | |||
| 2971 | private function objToStr($obj) |
||
| 2972 | { |
||
| 2973 | return method_exists($obj, '__toString') ? (string)$obj : get_class($obj) . '@' . spl_object_hash($obj); |
||
| 2975 | } |
||
| 2976 |
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.