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 |
||
| 47 | class UnitOfWork implements PropertyChangedListener |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 51 | */ |
||
| 52 | const STATE_MANAGED = 1; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 56 | * and is not (yet) managed by a DocumentManager. |
||
| 57 | */ |
||
| 58 | const STATE_NEW = 2; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A detached document is an instance with a persistent identity that is not |
||
| 62 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 63 | */ |
||
| 64 | const STATE_DETACHED = 3; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * A removed document instance is an instance with a persistent identity, |
||
| 68 | * associated with a DocumentManager, whose persistent state has been |
||
| 69 | * deleted (or is scheduled for deletion). |
||
| 70 | */ |
||
| 71 | const STATE_REMOVED = 4; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The identity map holds references to all managed documents. |
||
| 75 | * |
||
| 76 | * Documents are grouped by their class name, and then indexed by the |
||
| 77 | * serialized string of their database identifier field or, if the class |
||
| 78 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 79 | * differentiation of values that may be equal (via type juggling) but not |
||
| 80 | * identical. |
||
| 81 | * |
||
| 82 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 83 | * we always take the root class name of the hierarchy. |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $identityMap = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Map of all identifiers of managed documents. |
||
| 91 | * Keys are object ids (spl_object_hash). |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | private $documentIdentifiers = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Map of the original document data of managed documents. |
||
| 99 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 100 | * at commit time. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 104 | * A value will only really be copied if the value in the document is modified |
||
| 105 | * by the user. |
||
| 106 | */ |
||
| 107 | private $originalDocumentData = array(); |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 111 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | private $documentChangeSets = array(); |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The (cached) states of any known documents. |
||
| 119 | * Keys are object ids (spl_object_hash). |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | private $documentStates = array(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 127 | * |
||
| 128 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 129 | * object hash. This is only used for documents with a change tracking |
||
| 130 | * policy of DEFERRED_EXPLICIT. |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | * @todo rename: scheduledForSynchronization |
||
| 134 | */ |
||
| 135 | private $scheduledForDirtyCheck = array(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * A list of all pending document insertions. |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | private $documentInsertions = array(); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * A list of all pending document updates. |
||
| 146 | * |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | private $documentUpdates = array(); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * A list of all pending document upserts. |
||
| 153 | * |
||
| 154 | * @var array |
||
| 155 | */ |
||
| 156 | private $documentUpserts = array(); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * A list of all pending document deletions. |
||
| 160 | * |
||
| 161 | * @var array |
||
| 162 | */ |
||
| 163 | private $documentDeletions = array(); |
||
| 164 | |||
| 165 | /** |
||
| 166 | * All pending collection deletions. |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | */ |
||
| 170 | private $collectionDeletions = array(); |
||
| 171 | |||
| 172 | /** |
||
| 173 | * All pending collection updates. |
||
| 174 | * |
||
| 175 | * @var array |
||
| 176 | */ |
||
| 177 | private $collectionUpdates = array(); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * A list of documents related to collections scheduled for update or deletion |
||
| 181 | * |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | private $hasScheduledCollections = array(); |
||
| 185 | |||
| 186 | /** |
||
| 187 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 188 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 189 | * of their data. |
||
| 190 | * |
||
| 191 | * @var array |
||
| 192 | */ |
||
| 193 | private $visitedCollections = array(); |
||
| 194 | |||
| 195 | /** |
||
| 196 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 197 | * |
||
| 198 | * @var DocumentManager |
||
| 199 | */ |
||
| 200 | private $dm; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * The EventManager used for dispatching events. |
||
| 204 | * |
||
| 205 | * @var EventManager |
||
| 206 | */ |
||
| 207 | private $evm; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Additional documents that are scheduled for removal. |
||
| 211 | * |
||
| 212 | * @var array |
||
| 213 | */ |
||
| 214 | private $orphanRemovals = array(); |
||
| 215 | |||
| 216 | /** |
||
| 217 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 218 | * |
||
| 219 | * @var HydratorFactory |
||
| 220 | */ |
||
| 221 | private $hydratorFactory; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * The document persister instances used to persist document instances. |
||
| 225 | * |
||
| 226 | * @var array |
||
| 227 | */ |
||
| 228 | private $persisters = array(); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * The collection persister instance used to persist changes to collections. |
||
| 232 | * |
||
| 233 | * @var Persisters\CollectionPersister |
||
| 234 | */ |
||
| 235 | private $collectionPersister; |
||
| 236 | |||
| 237 | /** |
||
| 238 | * The persistence builder instance used in DocumentPersisters. |
||
| 239 | * |
||
| 240 | * @var PersistenceBuilder |
||
| 241 | */ |
||
| 242 | private $persistenceBuilder; |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Array of parent associations between embedded documents |
||
| 246 | * |
||
| 247 | * @todo We might need to clean up this array in clear(), doDetach(), etc. |
||
| 248 | * @var array |
||
| 249 | */ |
||
| 250 | private $parentAssociations = array(); |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 254 | * |
||
| 255 | * @param DocumentManager $dm |
||
| 256 | * @param EventManager $evm |
||
| 257 | * @param HydratorFactory $hydratorFactory |
||
| 258 | */ |
||
| 259 | 924 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 268 | * queries for insert persistence. |
||
| 269 | * |
||
| 270 | * @return PersistenceBuilder $pb |
||
| 271 | */ |
||
| 272 | 670 | public function getPersistenceBuilder() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Sets the parent association for a given embedded document. |
||
| 282 | * |
||
| 283 | * @param object $document |
||
| 284 | * @param array $mapping |
||
| 285 | * @param object $parent |
||
| 286 | * @param string $propertyPath |
||
| 287 | */ |
||
| 288 | 180 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets the parent association for a given embedded document. |
||
| 296 | * |
||
| 297 | * <code> |
||
| 298 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 299 | * </code> |
||
| 300 | * |
||
| 301 | * @param object $document |
||
| 302 | * @return array $association |
||
| 303 | */ |
||
| 304 | 207 | public function getParentAssociation($document) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Get the document persister instance for the given document name |
||
| 315 | * |
||
| 316 | * @param string $documentName |
||
| 317 | * @return Persisters\DocumentPersister |
||
| 318 | */ |
||
| 319 | 668 | public function getDocumentPersister($documentName) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Get the collection persister instance. |
||
| 331 | * |
||
| 332 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 333 | */ |
||
| 334 | 668 | public function getCollectionPersister() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Set the document persister instance to use for the given document name |
||
| 345 | * |
||
| 346 | * @param string $documentName |
||
| 347 | * @param Persisters\DocumentPersister $persister |
||
| 348 | */ |
||
| 349 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 356 | * up to this point. The state of all managed documents will be synchronized with |
||
| 357 | * the database. |
||
| 358 | * |
||
| 359 | * The operations are executed in the following order: |
||
| 360 | * |
||
| 361 | * 1) All document insertions |
||
| 362 | * 2) All document updates |
||
| 363 | * 3) All document deletions |
||
| 364 | * |
||
| 365 | * @param object $document |
||
| 366 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 367 | */ |
||
| 368 | 557 | public function commit($document = null, array $options = array()) |
|
| 369 | { |
||
| 370 | // Raise preFlush |
||
| 371 | 557 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 372 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 373 | } |
||
| 374 | |||
| 375 | 557 | $defaultOptions = $this->dm->getConfiguration()->getDefaultCommitOptions(); |
|
| 376 | 557 | if ($options) { |
|
|
|
|||
| 377 | $options = array_merge($defaultOptions, $options); |
||
| 378 | } else { |
||
| 379 | 557 | $options = $defaultOptions; |
|
| 380 | } |
||
| 381 | // Compute changes done since last commit. |
||
| 382 | 557 | if ($document === null) { |
|
| 383 | 551 | $this->computeChangeSets(); |
|
| 384 | 556 | } elseif (is_object($document)) { |
|
| 385 | 12 | $this->computeSingleDocumentChangeSet($document); |
|
| 386 | 12 | } elseif (is_array($document)) { |
|
| 387 | 1 | foreach ($document as $object) { |
|
| 388 | 1 | $this->computeSingleDocumentChangeSet($object); |
|
| 389 | 1 | } |
|
| 390 | 1 | } |
|
| 391 | |||
| 392 | 555 | if ( ! ($this->documentInsertions || |
|
| 393 | 238 | $this->documentUpserts || |
|
| 394 | 201 | $this->documentDeletions || |
|
| 395 | 191 | $this->documentUpdates || |
|
| 396 | 23 | $this->collectionUpdates || |
|
| 397 | 23 | $this->collectionDeletions || |
|
| 398 | 23 | $this->orphanRemovals) |
|
| 399 | 555 | ) { |
|
| 400 | 23 | return; // Nothing to do. |
|
| 401 | } |
||
| 402 | |||
| 403 | 552 | if ($this->orphanRemovals) { |
|
| 404 | 45 | foreach ($this->orphanRemovals as $removal) { |
|
| 405 | 45 | $this->remove($removal); |
|
| 406 | 45 | } |
|
| 407 | 45 | } |
|
| 408 | |||
| 409 | // Raise onFlush |
||
| 410 | 552 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 411 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 412 | 7 | } |
|
| 413 | |||
| 414 | 552 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 415 | 78 | list($class, $documents) = $classAndDocuments; |
|
| 416 | 78 | $this->executeUpserts($class, $documents, $options); |
|
| 417 | 552 | } |
|
| 418 | |||
| 419 | 552 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 420 | 485 | list($class, $documents) = $classAndDocuments; |
|
| 421 | 485 | $this->executeInserts($class, $documents, $options); |
|
| 422 | 551 | } |
|
| 423 | |||
| 424 | 551 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 425 | 217 | list($class, $documents) = $classAndDocuments; |
|
| 426 | 217 | $this->executeUpdates($class, $documents, $options); |
|
| 427 | 551 | } |
|
| 428 | |||
| 429 | 551 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 430 | 62 | list($class, $documents) = $classAndDocuments; |
|
| 431 | 62 | $this->executeDeletions($class, $documents, $options); |
|
| 432 | 551 | } |
|
| 433 | |||
| 434 | // Raise postFlush |
||
| 435 | 551 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 436 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 437 | } |
||
| 438 | |||
| 439 | // Clear up |
||
| 440 | 551 | $this->documentInsertions = |
|
| 441 | 551 | $this->documentUpserts = |
|
| 442 | 551 | $this->documentUpdates = |
|
| 443 | 551 | $this->documentDeletions = |
|
| 444 | 551 | $this->documentChangeSets = |
|
| 445 | 551 | $this->collectionUpdates = |
|
| 446 | 551 | $this->collectionDeletions = |
|
| 447 | 551 | $this->visitedCollections = |
|
| 448 | 551 | $this->scheduledForDirtyCheck = |
|
| 449 | 551 | $this->orphanRemovals = |
|
| 450 | 551 | $this->hasScheduledCollections = array(); |
|
| 451 | 551 | } |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Groups a list of scheduled documents by their class. |
||
| 455 | * |
||
| 456 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 457 | * @param bool $includeEmbedded |
||
| 458 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 459 | */ |
||
| 460 | 552 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 461 | { |
||
| 462 | 552 | if (empty($documents)) { |
|
| 463 | 552 | return array(); |
|
| 464 | } |
||
| 465 | 551 | $divided = array(); |
|
| 466 | 551 | $embeds = array(); |
|
| 467 | 551 | foreach ($documents as $oid => $d) { |
|
| 468 | 551 | $className = get_class($d); |
|
| 469 | 551 | if (isset($embeds[$className])) { |
|
| 470 | 68 | continue; |
|
| 471 | } |
||
| 472 | 551 | if (isset($divided[$className])) { |
|
| 473 | 135 | $divided[$className][1][$oid] = $d; |
|
| 474 | 135 | continue; |
|
| 475 | } |
||
| 476 | 551 | $class = $this->dm->getClassMetadata($className); |
|
| 477 | 551 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 478 | 165 | $embeds[$className] = true; |
|
| 479 | 165 | continue; |
|
| 480 | } |
||
| 481 | 551 | if (empty($divided[$class->name])) { |
|
| 482 | 551 | $divided[$class->name] = array($class, array($oid => $d)); |
|
| 483 | 551 | } else { |
|
| 484 | 4 | $divided[$class->name][1][$oid] = $d; |
|
| 485 | } |
||
| 486 | 551 | } |
|
| 487 | 551 | return $divided; |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Compute changesets of all documents scheduled for insertion. |
||
| 492 | * |
||
| 493 | * Embedded documents will not be processed. |
||
| 494 | */ |
||
| 495 | 559 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 496 | { |
||
| 497 | 559 | foreach ($this->documentInsertions as $document) { |
|
| 498 | 493 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 499 | 493 | if ( ! $class->isEmbeddedDocument) { |
|
| 500 | 490 | $this->computeChangeSet($class, $document); |
|
| 501 | 489 | } |
|
| 502 | 558 | } |
|
| 503 | 558 | } |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Compute changesets of all documents scheduled for upsert. |
||
| 507 | * |
||
| 508 | * Embedded documents will not be processed. |
||
| 509 | */ |
||
| 510 | 558 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 511 | { |
||
| 512 | 558 | foreach ($this->documentUpserts as $document) { |
|
| 513 | 77 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 514 | 77 | if ( ! $class->isEmbeddedDocument) { |
|
| 515 | 77 | $this->computeChangeSet($class, $document); |
|
| 516 | 77 | } |
|
| 517 | 558 | } |
|
| 518 | 558 | } |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 522 | * |
||
| 523 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 524 | * 2. Proxies are skipped. |
||
| 525 | * 3. Only if document is properly managed. |
||
| 526 | * |
||
| 527 | * @param object $document |
||
| 528 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | 13 | private function computeSingleDocumentChangeSet($document) |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Gets the changeset for a document. |
||
| 568 | * |
||
| 569 | * @param object $document |
||
| 570 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 571 | */ |
||
| 572 | 542 | public function getDocumentChangeSet($document) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 583 | * |
||
| 584 | * @param object $document |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | 556 | public function getDocumentActualData($document) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Computes the changes that happened to a single document. |
||
| 624 | * |
||
| 625 | * Modifies/populates the following properties: |
||
| 626 | * |
||
| 627 | * {@link originalDocumentData} |
||
| 628 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 629 | * then it was not fetched from the database and therefore we have no original |
||
| 630 | * document data yet. All of the current document data is stored as the original document data. |
||
| 631 | * |
||
| 632 | * {@link documentChangeSets} |
||
| 633 | * The changes detected on all properties of the document are stored there. |
||
| 634 | * A change is a tuple array where the first entry is the old value and the second |
||
| 635 | * entry is the new value of the property. Changesets are used by persisters |
||
| 636 | * to INSERT/UPDATE the persistent document state. |
||
| 637 | * |
||
| 638 | * {@link documentUpdates} |
||
| 639 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 640 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 641 | * there to mark it for an update. |
||
| 642 | * |
||
| 643 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 644 | * @param object $document The document for which to compute the changes. |
||
| 645 | */ |
||
| 646 | 556 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 662 | * |
||
| 663 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 664 | * @param object $document |
||
| 665 | * @param boolean $recompute |
||
| 666 | */ |
||
| 667 | 556 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Computes all the changes that have been done to documents and collections |
||
| 833 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 834 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 835 | */ |
||
| 836 | 554 | public function computeChangeSets() |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Computes the changes of an association. |
||
| 889 | * |
||
| 890 | * @param object $parentDocument |
||
| 891 | * @param array $assoc |
||
| 892 | * @param mixed $value The value of the association. |
||
| 893 | * @throws \InvalidArgumentException |
||
| 894 | */ |
||
| 895 | 419 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 997 | |||
| 998 | /** |
||
| 999 | * INTERNAL: |
||
| 1000 | * Computes the changeset of an individual document, independently of the |
||
| 1001 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1002 | * |
||
| 1003 | * The passed document must be a managed document. If the document already has a change set |
||
| 1004 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1005 | * whereby changes detected in this method prevail. |
||
| 1006 | * |
||
| 1007 | * @ignore |
||
| 1008 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1009 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1010 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1011 | */ |
||
| 1012 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * @param ClassMetadata $class |
||
| 1034 | * @param object $document |
||
| 1035 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1036 | */ |
||
| 1037 | 574 | private function persistNew(ClassMetadata $class, $document) |
|
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Cascades the postPersist events to embedded documents. |
||
| 1089 | * |
||
| 1090 | * @param ClassMetadata $class |
||
| 1091 | * @param object $document |
||
| 1092 | */ |
||
| 1093 | 550 | private function cascadePostPersist(ClassMetadata $class, $document) |
|
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Executes all document insertions for documents of the specified type. |
||
| 1133 | * |
||
| 1134 | * @param ClassMetadata $class |
||
| 1135 | * @param array $documents Array of documents to insert |
||
| 1136 | * @param array $options Array of options to be used with batchInsert() |
||
| 1137 | */ |
||
| 1138 | 485 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1162 | |||
| 1163 | /** |
||
| 1164 | * Executes all document upserts for documents of the specified type. |
||
| 1165 | * |
||
| 1166 | * @param ClassMetadata $class |
||
| 1167 | * @param array $documents Array of documents to upsert |
||
| 1168 | * @param array $options Array of options to be used with batchInsert() |
||
| 1169 | */ |
||
| 1170 | 78 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1195 | |||
| 1196 | /** |
||
| 1197 | * Executes all document updates for documents of the specified type. |
||
| 1198 | * |
||
| 1199 | * @param Mapping\ClassMetadata $class |
||
| 1200 | * @param array $documents Array of documents to update |
||
| 1201 | * @param array $options Array of options to be used with update() |
||
| 1202 | */ |
||
| 1203 | 217 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Cascades the preUpdate event to embedded documents. |
||
| 1250 | * |
||
| 1251 | * @param ClassMetadata $class |
||
| 1252 | * @param object $document |
||
| 1253 | */ |
||
| 1254 | 217 | private function cascadePreUpdate(ClassMetadata $class, $document) |
|
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Cascades the postUpdate and postPersist events to embedded documents. |
||
| 1303 | * |
||
| 1304 | * @param ClassMetadata $class |
||
| 1305 | * @param object $document |
||
| 1306 | */ |
||
| 1307 | 213 | private function cascadePostUpdate(ClassMetadata $class, $document) |
|
| 1358 | |||
| 1359 | /** |
||
| 1360 | * Executes all document deletions for documents of the specified type. |
||
| 1361 | * |
||
| 1362 | * @param ClassMetadata $class |
||
| 1363 | * @param array $documents Array of documents to delete |
||
| 1364 | * @param array $options Array of options to be used with remove() |
||
| 1365 | */ |
||
| 1366 | 62 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1406 | |||
| 1407 | /** |
||
| 1408 | * Schedules a document for insertion into the database. |
||
| 1409 | * If the document already has an identifier, it will be added to the |
||
| 1410 | * identity map. |
||
| 1411 | * |
||
| 1412 | * @param ClassMetadata $class |
||
| 1413 | * @param object $document The document to schedule for insertion. |
||
| 1414 | * @throws \InvalidArgumentException |
||
| 1415 | */ |
||
| 1416 | 509 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Schedules a document for upsert into the database and adds it to the |
||
| 1439 | * identity map |
||
| 1440 | * |
||
| 1441 | * @param ClassMetadata $class |
||
| 1442 | * @param object $document The document to schedule for upsert. |
||
| 1443 | * @throws \InvalidArgumentException |
||
| 1444 | */ |
||
| 1445 | 84 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1466 | |||
| 1467 | /** |
||
| 1468 | * Checks whether a document is scheduled for insertion. |
||
| 1469 | * |
||
| 1470 | * @param object $document |
||
| 1471 | * @return boolean |
||
| 1472 | */ |
||
| 1473 | 70 | public function isScheduledForInsert($document) |
|
| 1477 | |||
| 1478 | /** |
||
| 1479 | * Checks whether a document is scheduled for upsert. |
||
| 1480 | * |
||
| 1481 | * @param object $document |
||
| 1482 | * @return boolean |
||
| 1483 | */ |
||
| 1484 | 5 | public function isScheduledForUpsert($document) |
|
| 1488 | |||
| 1489 | /** |
||
| 1490 | * Schedules a document for being updated. |
||
| 1491 | * |
||
| 1492 | * @param object $document The document to schedule for being updated. |
||
| 1493 | * @throws \InvalidArgumentException |
||
| 1494 | */ |
||
| 1495 | 226 | public function scheduleForUpdate($document) |
|
| 1512 | |||
| 1513 | /** |
||
| 1514 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1515 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1516 | * at commit time. |
||
| 1517 | * |
||
| 1518 | * @param object $document |
||
| 1519 | * @return boolean |
||
| 1520 | */ |
||
| 1521 | 13 | public function isScheduledForUpdate($document) |
|
| 1525 | |||
| 1526 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1531 | |||
| 1532 | /** |
||
| 1533 | * INTERNAL: |
||
| 1534 | * Schedules a document for deletion. |
||
| 1535 | * |
||
| 1536 | * @param object $document |
||
| 1537 | */ |
||
| 1538 | 67 | public function scheduleForDelete($document) |
|
| 1564 | |||
| 1565 | /** |
||
| 1566 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1567 | * of work. |
||
| 1568 | * |
||
| 1569 | * @param object $document |
||
| 1570 | * @return boolean |
||
| 1571 | */ |
||
| 1572 | 8 | public function isScheduledForDelete($document) |
|
| 1576 | |||
| 1577 | /** |
||
| 1578 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1579 | * |
||
| 1580 | * @param $document |
||
| 1581 | * @return boolean |
||
| 1582 | */ |
||
| 1583 | 224 | public function isDocumentScheduled($document) |
|
| 1591 | |||
| 1592 | /** |
||
| 1593 | * INTERNAL: |
||
| 1594 | * Registers a document in the identity map. |
||
| 1595 | * |
||
| 1596 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1597 | * the root document. Identifiers are serialized before being used as array |
||
| 1598 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1599 | * |
||
| 1600 | * @ignore |
||
| 1601 | * @param object $document The document to register. |
||
| 1602 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1603 | * the document in question is already managed. |
||
| 1604 | */ |
||
| 1605 | 601 | public function addToIdentityMap($document) |
|
| 1623 | |||
| 1624 | /** |
||
| 1625 | * Gets the state of a document with regard to the current unit of work. |
||
| 1626 | * |
||
| 1627 | * @param object $document |
||
| 1628 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1629 | * This parameter can be set to improve performance of document state detection |
||
| 1630 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1631 | * is either known or does not matter for the caller of the method. |
||
| 1632 | * @return int The document state. |
||
| 1633 | */ |
||
| 1634 | 577 | public function getDocumentState($document, $assume = null) |
|
| 1684 | |||
| 1685 | /** |
||
| 1686 | * INTERNAL: |
||
| 1687 | * Removes a document from the identity map. This effectively detaches the |
||
| 1688 | * document from the persistence management of Doctrine. |
||
| 1689 | * |
||
| 1690 | * @ignore |
||
| 1691 | * @param object $document |
||
| 1692 | * @throws \InvalidArgumentException |
||
| 1693 | * @return boolean |
||
| 1694 | */ |
||
| 1695 | 76 | public function removeFromIdentityMap($document) |
|
| 1715 | |||
| 1716 | /** |
||
| 1717 | * INTERNAL: |
||
| 1718 | * Gets a document in the identity map by its identifier hash. |
||
| 1719 | * |
||
| 1720 | * @ignore |
||
| 1721 | * @param mixed $id Document identifier |
||
| 1722 | * @param ClassMetadata $class Document class |
||
| 1723 | * @return object |
||
| 1724 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1725 | */ |
||
| 1726 | 31 | public function getById($id, ClassMetadata $class) |
|
| 1736 | |||
| 1737 | /** |
||
| 1738 | * INTERNAL: |
||
| 1739 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1740 | * for the given hash, FALSE is returned. |
||
| 1741 | * |
||
| 1742 | * @ignore |
||
| 1743 | * @param mixed $id Document identifier |
||
| 1744 | * @param ClassMetadata $class Document class |
||
| 1745 | * @return mixed The found document or FALSE. |
||
| 1746 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1747 | */ |
||
| 1748 | 290 | public function tryGetById($id, ClassMetadata $class) |
|
| 1759 | |||
| 1760 | /** |
||
| 1761 | * Schedules a document for dirty-checking at commit-time. |
||
| 1762 | * |
||
| 1763 | * @param object $document The document to schedule for dirty-checking. |
||
| 1764 | * @todo Rename: scheduleForSynchronization |
||
| 1765 | */ |
||
| 1766 | 2 | public function scheduleForDirtyCheck($document) |
|
| 1771 | |||
| 1772 | /** |
||
| 1773 | * Checks whether a document is registered in the identity map. |
||
| 1774 | * |
||
| 1775 | * @param object $document |
||
| 1776 | * @return boolean |
||
| 1777 | */ |
||
| 1778 | 76 | public function isInIdentityMap($document) |
|
| 1791 | |||
| 1792 | /** |
||
| 1793 | * @param object $document |
||
| 1794 | * @return string |
||
| 1795 | */ |
||
| 1796 | 601 | private function getIdForIdentityMap($document) |
|
| 1809 | |||
| 1810 | /** |
||
| 1811 | * INTERNAL: |
||
| 1812 | * Checks whether an identifier exists in the identity map. |
||
| 1813 | * |
||
| 1814 | * @ignore |
||
| 1815 | * @param string $id |
||
| 1816 | * @param string $rootClassName |
||
| 1817 | * @return boolean |
||
| 1818 | */ |
||
| 1819 | public function containsId($id, $rootClassName) |
||
| 1823 | |||
| 1824 | /** |
||
| 1825 | * Persists a document as part of the current unit of work. |
||
| 1826 | * |
||
| 1827 | * @param object $document The document to persist. |
||
| 1828 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1829 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1830 | */ |
||
| 1831 | 572 | public function persist($document) |
|
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Saves a document as part of the current unit of work. |
||
| 1843 | * This method is internally called during save() cascades as it tracks |
||
| 1844 | * the already visited documents to prevent infinite recursions. |
||
| 1845 | * |
||
| 1846 | * NOTE: This method always considers documents that are not yet known to |
||
| 1847 | * this UnitOfWork as NEW. |
||
| 1848 | * |
||
| 1849 | * @param object $document The document to persist. |
||
| 1850 | * @param array $visited The already visited documents. |
||
| 1851 | * @throws \InvalidArgumentException |
||
| 1852 | * @throws MongoDBException |
||
| 1853 | */ |
||
| 1854 | 571 | private function doPersist($document, array &$visited) |
|
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Deletes a document as part of the current unit of work. |
||
| 1898 | * |
||
| 1899 | * @param object $document The document to remove. |
||
| 1900 | */ |
||
| 1901 | 66 | public function remove($document) |
|
| 1906 | |||
| 1907 | /** |
||
| 1908 | * Deletes a document as part of the current unit of work. |
||
| 1909 | * |
||
| 1910 | * This method is internally called during delete() cascades as it tracks |
||
| 1911 | * the already visited documents to prevent infinite recursions. |
||
| 1912 | * |
||
| 1913 | * @param object $document The document to delete. |
||
| 1914 | * @param array $visited The map of the already visited documents. |
||
| 1915 | * @throws MongoDBException |
||
| 1916 | */ |
||
| 1917 | 66 | private function doRemove($document, array &$visited) |
|
| 1954 | |||
| 1955 | /** |
||
| 1956 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1957 | * |
||
| 1958 | * @param object $document |
||
| 1959 | * @return object The managed copy of the document. |
||
| 1960 | */ |
||
| 1961 | 13 | public function merge($document) |
|
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Executes a merge operation on a document. |
||
| 1970 | * |
||
| 1971 | * @param object $document |
||
| 1972 | * @param array $visited |
||
| 1973 | * @param object|null $prevManagedCopy |
||
| 1974 | * @param array|null $assoc |
||
| 1975 | * |
||
| 1976 | * @return object The managed copy of the document. |
||
| 1977 | * |
||
| 1978 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1979 | * @throws LockException If the document uses optimistic locking through a |
||
| 1980 | * version attribute and the version check against the |
||
| 1981 | * managed copy fails. |
||
| 1982 | */ |
||
| 1983 | 13 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 2161 | |||
| 2162 | /** |
||
| 2163 | * Detaches a document from the persistence management. It's persistence will |
||
| 2164 | * no longer be managed by Doctrine. |
||
| 2165 | * |
||
| 2166 | * @param object $document The document to detach. |
||
| 2167 | */ |
||
| 2168 | 9 | public function detach($document) |
|
| 2173 | |||
| 2174 | /** |
||
| 2175 | * Executes a detach operation on the given document. |
||
| 2176 | * |
||
| 2177 | * @param object $document |
||
| 2178 | * @param array $visited |
||
| 2179 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 2180 | */ |
||
| 2181 | 12 | private function doDetach($document, array &$visited) |
|
| 2206 | |||
| 2207 | /** |
||
| 2208 | * Refreshes the state of the given document from the database, overwriting |
||
| 2209 | * any local, unpersisted changes. |
||
| 2210 | * |
||
| 2211 | * @param object $document The document to refresh. |
||
| 2212 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2213 | */ |
||
| 2214 | 21 | public function refresh($document) |
|
| 2219 | |||
| 2220 | /** |
||
| 2221 | * Executes a refresh operation on a document. |
||
| 2222 | * |
||
| 2223 | * @param object $document The document to refresh. |
||
| 2224 | * @param array $visited The already visited documents during cascades. |
||
| 2225 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2226 | */ |
||
| 2227 | 21 | private function doRefresh($document, array &$visited) |
|
| 2249 | |||
| 2250 | /** |
||
| 2251 | * Cascades a refresh operation to associated documents. |
||
| 2252 | * |
||
| 2253 | * @param object $document |
||
| 2254 | * @param array $visited |
||
| 2255 | */ |
||
| 2256 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2280 | |||
| 2281 | /** |
||
| 2282 | * Cascades a detach operation to associated documents. |
||
| 2283 | * |
||
| 2284 | * @param object $document |
||
| 2285 | * @param array $visited |
||
| 2286 | */ |
||
| 2287 | 12 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2308 | /** |
||
| 2309 | * Cascades a merge operation to associated documents. |
||
| 2310 | * |
||
| 2311 | * @param object $document |
||
| 2312 | * @param object $managedCopy |
||
| 2313 | * @param array $visited |
||
| 2314 | */ |
||
| 2315 | 13 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2346 | |||
| 2347 | /** |
||
| 2348 | * Cascades the save operation to associated documents. |
||
| 2349 | * |
||
| 2350 | * @param object $document |
||
| 2351 | * @param array $visited |
||
| 2352 | */ |
||
| 2353 | 569 | private function cascadePersist($document, array &$visited) |
|
| 2400 | |||
| 2401 | /** |
||
| 2402 | * Cascades the delete operation to associated documents. |
||
| 2403 | * |
||
| 2404 | * @param object $document |
||
| 2405 | * @param array $visited |
||
| 2406 | */ |
||
| 2407 | 66 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2429 | |||
| 2430 | /** |
||
| 2431 | * Acquire a lock on the given document. |
||
| 2432 | * |
||
| 2433 | * @param object $document |
||
| 2434 | * @param int $lockMode |
||
| 2435 | * @param int $lockVersion |
||
| 2436 | * @throws LockException |
||
| 2437 | * @throws \InvalidArgumentException |
||
| 2438 | */ |
||
| 2439 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2463 | |||
| 2464 | /** |
||
| 2465 | * Releases a lock on the given document. |
||
| 2466 | * |
||
| 2467 | * @param object $document |
||
| 2468 | * @throws \InvalidArgumentException |
||
| 2469 | */ |
||
| 2470 | 1 | public function unlock($document) |
|
| 2478 | |||
| 2479 | /** |
||
| 2480 | * Clears the UnitOfWork. |
||
| 2481 | * |
||
| 2482 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2483 | */ |
||
| 2484 | 388 | public function clear($documentName = null) |
|
| 2517 | |||
| 2518 | /** |
||
| 2519 | * INTERNAL: |
||
| 2520 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2521 | * invoked on that document at the beginning of the next commit of this |
||
| 2522 | * UnitOfWork. |
||
| 2523 | * |
||
| 2524 | * @ignore |
||
| 2525 | * @param object $document |
||
| 2526 | */ |
||
| 2527 | 47 | public function scheduleOrphanRemoval($document) |
|
| 2531 | |||
| 2532 | /** |
||
| 2533 | * INTERNAL: |
||
| 2534 | * Unschedules an embedded or referenced object for removal. |
||
| 2535 | * |
||
| 2536 | * @ignore |
||
| 2537 | * @param object $document |
||
| 2538 | */ |
||
| 2539 | 103 | public function unscheduleOrphanRemoval($document) |
|
| 2546 | |||
| 2547 | /** |
||
| 2548 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2549 | * 1) sets owner if it was cloned |
||
| 2550 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2551 | * 3) NOP if state is OK |
||
| 2552 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2553 | * |
||
| 2554 | * @param PersistentCollection $coll |
||
| 2555 | * @param object $document |
||
| 2556 | * @param ClassMetadata $class |
||
| 2557 | * @param string $propName |
||
| 2558 | * @return PersistentCollection |
||
| 2559 | */ |
||
| 2560 | 8 | private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, ClassMetadata $class, $propName) |
|
| 2580 | |||
| 2581 | /** |
||
| 2582 | * INTERNAL: |
||
| 2583 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2584 | * |
||
| 2585 | * @param PersistentCollection $coll |
||
| 2586 | */ |
||
| 2587 | 41 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2596 | |||
| 2597 | /** |
||
| 2598 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2599 | * |
||
| 2600 | * @param PersistentCollection $coll |
||
| 2601 | * @return boolean |
||
| 2602 | */ |
||
| 2603 | 106 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
|
| 2607 | |||
| 2608 | /** |
||
| 2609 | * INTERNAL: |
||
| 2610 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2611 | * |
||
| 2612 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2613 | */ |
||
| 2614 | 205 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollection $coll) |
| 2623 | |||
| 2624 | /** |
||
| 2625 | * INTERNAL: |
||
| 2626 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2627 | * |
||
| 2628 | * @param PersistentCollection $coll |
||
| 2629 | */ |
||
| 2630 | 221 | public function scheduleCollectionUpdate(PersistentCollection $coll) |
|
| 2645 | |||
| 2646 | /** |
||
| 2647 | * INTERNAL: |
||
| 2648 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2649 | * |
||
| 2650 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2651 | */ |
||
| 2652 | 205 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollection $coll) |
| 2661 | |||
| 2662 | /** |
||
| 2663 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2664 | * |
||
| 2665 | * @param PersistentCollection $coll |
||
| 2666 | * @return boolean |
||
| 2667 | */ |
||
| 2668 | 122 | public function isCollectionScheduledForUpdate(PersistentCollection $coll) |
|
| 2672 | |||
| 2673 | /** |
||
| 2674 | * INTERNAL: |
||
| 2675 | * Gets PersistentCollections that have been visited during computing change |
||
| 2676 | * set of $document |
||
| 2677 | * |
||
| 2678 | * @param object $document |
||
| 2679 | * @return PersistentCollection[] |
||
| 2680 | */ |
||
| 2681 | 537 | public function getVisitedCollections($document) |
|
| 2688 | |||
| 2689 | /** |
||
| 2690 | * INTERNAL: |
||
| 2691 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2692 | * |
||
| 2693 | * @param object $document |
||
| 2694 | * @return array |
||
| 2695 | */ |
||
| 2696 | 537 | public function getScheduledCollections($document) |
|
| 2703 | |||
| 2704 | /** |
||
| 2705 | * Checks whether the document is related to a PersistentCollection |
||
| 2706 | * scheduled for update or deletion. |
||
| 2707 | * |
||
| 2708 | * @param object $document |
||
| 2709 | * @return boolean |
||
| 2710 | */ |
||
| 2711 | 60 | public function hasScheduledCollections($document) |
|
| 2715 | |||
| 2716 | /** |
||
| 2717 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2718 | * a collection scheduled for update or deletion. |
||
| 2719 | * |
||
| 2720 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2721 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2722 | * |
||
| 2723 | * If the collection is nested within atomic collection, it is immediately |
||
| 2724 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2725 | * calculating update data way easier. |
||
| 2726 | * |
||
| 2727 | * @param PersistentCollection $coll |
||
| 2728 | */ |
||
| 2729 | 223 | private function scheduleCollectionOwner(PersistentCollection $coll) |
|
| 2752 | |||
| 2753 | /** |
||
| 2754 | * Get the top-most owning document of a given document |
||
| 2755 | * |
||
| 2756 | * If a top-level document is provided, that same document will be returned. |
||
| 2757 | * For an embedded document, we will walk through parent associations until |
||
| 2758 | * we find a top-level document. |
||
| 2759 | * |
||
| 2760 | * @param object $document |
||
| 2761 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2762 | * @return object |
||
| 2763 | */ |
||
| 2764 | 225 | public function getOwningDocument($document) |
|
| 2780 | |||
| 2781 | /** |
||
| 2782 | * Gets the class name for an association (embed or reference) with respect |
||
| 2783 | * to any discriminator value. |
||
| 2784 | * |
||
| 2785 | * @param array $mapping Field mapping for the association |
||
| 2786 | * @param array|null $data Data for the embedded document or reference |
||
| 2787 | */ |
||
| 2788 | 207 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2821 | |||
| 2822 | /** |
||
| 2823 | * INTERNAL: |
||
| 2824 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2825 | * |
||
| 2826 | * @ignore |
||
| 2827 | * @param string $className The name of the document class. |
||
| 2828 | * @param array $data The data for the document. |
||
| 2829 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2830 | * @param object The document to be hydrated into in case of creation |
||
| 2831 | * @return object The document instance. |
||
| 2832 | * @internal Highly performance-sensitive method. |
||
| 2833 | */ |
||
| 2834 | 382 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2888 | |||
| 2889 | /** |
||
| 2890 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2891 | * |
||
| 2892 | * @param PersistentCollection $collection The collection to initialize. |
||
| 2893 | */ |
||
| 2894 | 157 | public function loadCollection(PersistentCollection $collection) |
|
| 2898 | |||
| 2899 | /** |
||
| 2900 | * Gets the identity map of the UnitOfWork. |
||
| 2901 | * |
||
| 2902 | * @return array |
||
| 2903 | */ |
||
| 2904 | public function getIdentityMap() |
||
| 2908 | |||
| 2909 | /** |
||
| 2910 | * Gets the original data of a document. The original data is the data that was |
||
| 2911 | * present at the time the document was reconstituted from the database. |
||
| 2912 | * |
||
| 2913 | * @param object $document |
||
| 2914 | * @return array |
||
| 2915 | */ |
||
| 2916 | 1 | public function getOriginalDocumentData($document) |
|
| 2924 | |||
| 2925 | /** |
||
| 2926 | * @ignore |
||
| 2927 | */ |
||
| 2928 | 51 | public function setOriginalDocumentData($document, array $data) |
|
| 2934 | |||
| 2935 | /** |
||
| 2936 | * INTERNAL: |
||
| 2937 | * Sets a property value of the original data array of a document. |
||
| 2938 | * |
||
| 2939 | * @ignore |
||
| 2940 | * @param string $oid |
||
| 2941 | * @param string $property |
||
| 2942 | * @param mixed $value |
||
| 2943 | */ |
||
| 2944 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2948 | |||
| 2949 | /** |
||
| 2950 | * Gets the identifier of a document. |
||
| 2951 | * |
||
| 2952 | * @param object $document |
||
| 2953 | * @return mixed The identifier value |
||
| 2954 | */ |
||
| 2955 | 355 | public function getDocumentIdentifier($document) |
|
| 2960 | |||
| 2961 | /** |
||
| 2962 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2963 | * |
||
| 2964 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2965 | */ |
||
| 2966 | public function hasPendingInsertions() |
||
| 2970 | |||
| 2971 | /** |
||
| 2972 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2973 | * number of documents in the identity map. |
||
| 2974 | * |
||
| 2975 | * @return integer |
||
| 2976 | */ |
||
| 2977 | 2 | public function size() |
|
| 2985 | |||
| 2986 | /** |
||
| 2987 | * INTERNAL: |
||
| 2988 | * Registers a document as managed. |
||
| 2989 | * |
||
| 2990 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2991 | * document class. If the class expects its database identifier to be a |
||
| 2992 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2993 | * document identifiers map will become inconsistent with the identity map. |
||
| 2994 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2995 | * conversion and throw an exception if it's inconsistent. |
||
| 2996 | * |
||
| 2997 | * @param object $document The document. |
||
| 2998 | * @param array $id The identifier values. |
||
| 2999 | * @param array $data The original document data. |
||
| 3000 | */ |
||
| 3001 | 376 | public function registerManaged($document, $id, array $data) |
|
| 3016 | |||
| 3017 | /** |
||
| 3018 | * INTERNAL: |
||
| 3019 | * Clears the property changeset of the document with the given OID. |
||
| 3020 | * |
||
| 3021 | * @param string $oid The document's OID. |
||
| 3022 | */ |
||
| 3023 | 1 | public function clearDocumentChangeSet($oid) |
|
| 3027 | |||
| 3028 | /* PropertyChangedListener implementation */ |
||
| 3029 | |||
| 3030 | /** |
||
| 3031 | * Notifies this UnitOfWork of a property change in a document. |
||
| 3032 | * |
||
| 3033 | * @param object $document The document that owns the property. |
||
| 3034 | * @param string $propertyName The name of the property that changed. |
||
| 3035 | * @param mixed $oldValue The old value of the property. |
||
| 3036 | * @param mixed $newValue The new value of the property. |
||
| 3037 | */ |
||
| 3038 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 3053 | |||
| 3054 | /** |
||
| 3055 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 3056 | * |
||
| 3057 | * @return array |
||
| 3058 | */ |
||
| 3059 | 5 | public function getScheduledDocumentInsertions() |
|
| 3063 | |||
| 3064 | /** |
||
| 3065 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 3066 | * |
||
| 3067 | * @return array |
||
| 3068 | */ |
||
| 3069 | 3 | public function getScheduledDocumentUpserts() |
|
| 3073 | |||
| 3074 | /** |
||
| 3075 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 3076 | * |
||
| 3077 | * @return array |
||
| 3078 | */ |
||
| 3079 | 3 | public function getScheduledDocumentUpdates() |
|
| 3083 | |||
| 3084 | /** |
||
| 3085 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 3086 | * |
||
| 3087 | * @return array |
||
| 3088 | */ |
||
| 3089 | public function getScheduledDocumentDeletions() |
||
| 3093 | |||
| 3094 | /** |
||
| 3095 | * Get the currently scheduled complete collection deletions |
||
| 3096 | * |
||
| 3097 | * @return array |
||
| 3098 | */ |
||
| 3099 | public function getScheduledCollectionDeletions() |
||
| 3103 | |||
| 3104 | /** |
||
| 3105 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 3106 | * |
||
| 3107 | * @return array |
||
| 3108 | */ |
||
| 3109 | public function getScheduledCollectionUpdates() |
||
| 3113 | |||
| 3114 | /** |
||
| 3115 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 3116 | * |
||
| 3117 | * @param object |
||
| 3118 | * @return void |
||
| 3119 | */ |
||
| 3120 | public function initializeObject($obj) |
||
| 3128 | |||
| 3129 | 1 | private static function objToStr($obj) |
|
| 3133 | } |
||
| 3134 |
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.