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 | 921 | 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 | 667 | 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 | 665 | public function getDocumentPersister($documentName) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Get the collection persister instance. |
||
| 331 | * |
||
| 332 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 333 | */ |
||
| 334 | 665 | 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 | 554 | public function commit($document = null, array $options = array()) |
|
| 369 | { |
||
| 370 | // Raise preFlush |
||
| 371 | 554 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 372 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 373 | } |
||
| 374 | |||
| 375 | 554 | $defaultOptions = $this->dm->getConfiguration()->getDefaultCommitOptions(); |
|
| 376 | 554 | if ($options) { |
|
|
|
|||
| 377 | $options = array_merge($defaultOptions, $options); |
||
| 378 | } else { |
||
| 379 | 554 | $options = $defaultOptions; |
|
| 380 | } |
||
| 381 | // Compute changes done since last commit. |
||
| 382 | 554 | if ($document === null) { |
|
| 383 | 548 | $this->computeChangeSets(); |
|
| 384 | 553 | } 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 | 552 | if ( ! ($this->documentInsertions || |
|
| 393 | 235 | $this->documentUpserts || |
|
| 394 | 198 | $this->documentDeletions || |
|
| 395 | 188 | $this->documentUpdates || |
|
| 396 | 23 | $this->collectionUpdates || |
|
| 397 | 23 | $this->collectionDeletions || |
|
| 398 | 23 | $this->orphanRemovals) |
|
| 399 | 552 | ) { |
|
| 400 | 23 | return; // Nothing to do. |
|
| 401 | } |
||
| 402 | |||
| 403 | 549 | if ($this->orphanRemovals) { |
|
| 404 | 45 | foreach ($this->orphanRemovals as $removal) { |
|
| 405 | 45 | $this->remove($removal); |
|
| 406 | 45 | } |
|
| 407 | 45 | } |
|
| 408 | |||
| 409 | // Raise onFlush |
||
| 410 | 549 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 411 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 412 | 7 | } |
|
| 413 | |||
| 414 | 549 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 415 | 78 | list($class, $documents) = $classAndDocuments; |
|
| 416 | 78 | $this->executeUpserts($class, $documents, $options); |
|
| 417 | 549 | } |
|
| 418 | |||
| 419 | 549 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 420 | 482 | list($class, $documents) = $classAndDocuments; |
|
| 421 | 482 | $this->executeInserts($class, $documents, $options); |
|
| 422 | 548 | } |
|
| 423 | |||
| 424 | 548 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 425 | 214 | list($class, $documents) = $classAndDocuments; |
|
| 426 | 214 | $this->executeUpdates($class, $documents, $options); |
|
| 427 | 548 | } |
|
| 428 | |||
| 429 | 548 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 430 | 62 | list($class, $documents) = $classAndDocuments; |
|
| 431 | 62 | $this->executeDeletions($class, $documents, $options); |
|
| 432 | 548 | } |
|
| 433 | |||
| 434 | // Raise postFlush |
||
| 435 | 548 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 436 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 437 | } |
||
| 438 | |||
| 439 | // Clear up |
||
| 440 | 548 | $this->documentInsertions = |
|
| 441 | 548 | $this->documentUpserts = |
|
| 442 | 548 | $this->documentUpdates = |
|
| 443 | 548 | $this->documentDeletions = |
|
| 444 | 548 | $this->documentChangeSets = |
|
| 445 | 548 | $this->collectionUpdates = |
|
| 446 | 548 | $this->collectionDeletions = |
|
| 447 | 548 | $this->visitedCollections = |
|
| 448 | 548 | $this->scheduledForDirtyCheck = |
|
| 449 | 548 | $this->orphanRemovals = |
|
| 450 | 548 | $this->hasScheduledCollections = array(); |
|
| 451 | 548 | } |
|
| 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 | 549 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 461 | { |
||
| 462 | 549 | if (empty($documents)) { |
|
| 463 | 549 | return array(); |
|
| 464 | } |
||
| 465 | 548 | $divided = array(); |
|
| 466 | 548 | $embeds = array(); |
|
| 467 | 548 | foreach ($documents as $oid => $d) { |
|
| 468 | 548 | $className = get_class($d); |
|
| 469 | 548 | if (isset($embeds[$className])) { |
|
| 470 | 68 | continue; |
|
| 471 | } |
||
| 472 | 548 | if (isset($divided[$className])) { |
|
| 473 | 135 | $divided[$className][1][$oid] = $d; |
|
| 474 | 135 | continue; |
|
| 475 | } |
||
| 476 | 548 | $class = $this->dm->getClassMetadata($className); |
|
| 477 | 548 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 478 | 165 | $embeds[$className] = true; |
|
| 479 | 165 | continue; |
|
| 480 | } |
||
| 481 | 548 | if (empty($divided[$class->name])) { |
|
| 482 | 548 | $divided[$class->name] = array($class, array($oid => $d)); |
|
| 483 | 548 | } else { |
|
| 484 | 4 | $divided[$class->name][1][$oid] = $d; |
|
| 485 | } |
||
| 486 | 548 | } |
|
| 487 | 548 | return $divided; |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Compute changesets of all documents scheduled for insertion. |
||
| 492 | * |
||
| 493 | * Embedded documents will not be processed. |
||
| 494 | */ |
||
| 495 | 556 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 496 | { |
||
| 497 | 556 | foreach ($this->documentInsertions as $document) { |
|
| 498 | 490 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 499 | 490 | if ( ! $class->isEmbeddedDocument) { |
|
| 500 | 487 | $this->computeChangeSet($class, $document); |
|
| 501 | 486 | } |
|
| 502 | 555 | } |
|
| 503 | 555 | } |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Compute changesets of all documents scheduled for upsert. |
||
| 507 | * |
||
| 508 | * Embedded documents will not be processed. |
||
| 509 | */ |
||
| 510 | 555 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 511 | { |
||
| 512 | 555 | 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 | 555 | } |
|
| 518 | 555 | } |
|
| 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 | 539 | 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 | 553 | public function getDocumentActualData($document) |
|
| 588 | { |
||
| 589 | 553 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 590 | 553 | $actualData = array(); |
|
| 591 | 553 | foreach ($class->reflFields as $name => $refProp) { |
|
| 592 | 553 | $mapping = $class->fieldMappings[$name]; |
|
| 593 | // skip not saved fields |
||
| 594 | 553 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 595 | 49 | continue; |
|
| 596 | } |
||
| 597 | 553 | $value = $refProp->getValue($document); |
|
| 598 | 553 | if (isset($mapping['file']) && ! $value instanceof GridFSFile) { |
|
| 599 | 5 | $value = new GridFSFile($value); |
|
| 600 | 5 | $class->reflFields[$name]->setValue($document, $value); |
|
| 601 | 5 | $actualData[$name] = $value; |
|
| 602 | 553 | } elseif ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 603 | 553 | && $value !== null && ! ($value instanceof PersistentCollection)) { |
|
| 604 | // If $actualData[$name] is not a Collection then use an ArrayCollection. |
||
| 605 | 365 | if ( ! $value instanceof Collection) { |
|
| 606 | 118 | $value = new ArrayCollection($value); |
|
| 607 | 118 | } |
|
| 608 | |||
| 609 | // Inject PersistentCollection |
||
| 610 | 365 | $coll = new PersistentCollection($value, $this->dm, $this); |
|
| 611 | 365 | $coll->setOwner($document, $mapping); |
|
| 612 | 365 | $coll->setDirty( ! $value->isEmpty()); |
|
| 613 | 365 | $class->reflFields[$name]->setValue($document, $coll); |
|
| 614 | 365 | $actualData[$name] = $coll; |
|
| 615 | 365 | } else { |
|
| 616 | 553 | $actualData[$name] = $value; |
|
| 617 | } |
||
| 618 | 553 | } |
|
| 619 | 553 | return $actualData; |
|
| 620 | } |
||
| 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 | 553 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 647 | { |
||
| 648 | 553 | if ( ! $class->isInheritanceTypeNone()) { |
|
| 649 | 171 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 650 | 171 | } |
|
| 651 | |||
| 652 | // Fire PreFlush lifecycle callbacks |
||
| 653 | 553 | View Code Duplication | if ( ! empty($class->lifecycleCallbacks[Events::preFlush])) { |
| 654 | 10 | $class->invokeLifecycleCallbacks(Events::preFlush, $document, array(new Event\PreFlushEventArgs($this->dm))); |
|
| 655 | 10 | } |
|
| 656 | |||
| 657 | 553 | $this->computeOrRecomputeChangeSet($class, $document); |
|
| 658 | 552 | } |
|
| 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 | 553 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 668 | { |
||
| 669 | 553 | $oid = spl_object_hash($document); |
|
| 670 | 553 | $actualData = $this->getDocumentActualData($document); |
|
| 671 | 553 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 672 | 553 | if ($isNewDocument) { |
|
| 673 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 674 | // These result in an INSERT. |
||
| 675 | 553 | $this->originalDocumentData[$oid] = $actualData; |
|
| 676 | 553 | $changeSet = array(); |
|
| 677 | 553 | foreach ($actualData as $propName => $actualValue) { |
|
| 678 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 679 | * was cloned and its ownership must be fixed |
||
| 680 | */ |
||
| 681 | 553 | if ($actualValue instanceof PersistentCollection && $actualValue->getOwner() !== $document) { |
|
| 682 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 683 | $actualValue = $actualData[$propName]; |
||
| 684 | } |
||
| 685 | 553 | $changeSet[$propName] = array(null, $actualValue); |
|
| 686 | 553 | } |
|
| 687 | 553 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 688 | 553 | } else { |
|
| 689 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 690 | // and we have a copy of the original data |
||
| 691 | 274 | $originalData = $this->originalDocumentData[$oid]; |
|
| 692 | 274 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 693 | 274 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 694 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 695 | 2 | } else { |
|
| 696 | 274 | $changeSet = array(); |
|
| 697 | } |
||
| 698 | |||
| 699 | 274 | foreach ($actualData as $propName => $actualValue) { |
|
| 700 | // skip not saved fields |
||
| 701 | 274 | if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) { |
|
| 702 | continue; |
||
| 703 | } |
||
| 704 | |||
| 705 | 274 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; |
|
| 706 | |||
| 707 | // skip if value has not changed |
||
| 708 | 274 | if ($orgValue === $actualValue) { |
|
| 709 | // but consider dirty GridFSFile instances as changed |
||
| 710 | 273 | if ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) { |
|
| 711 | 273 | continue; |
|
| 712 | } |
||
| 713 | 1 | } |
|
| 714 | |||
| 715 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 716 | 175 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 717 | 10 | if ($orgValue !== null) { |
|
| 718 | 5 | $this->scheduleOrphanRemoval($orgValue); |
|
| 719 | 5 | } |
|
| 720 | |||
| 721 | 10 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 722 | 10 | continue; |
|
| 723 | } |
||
| 724 | |||
| 725 | // if owning side of reference-one relationship |
||
| 726 | 168 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 727 | 10 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 728 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 729 | 1 | } |
|
| 730 | |||
| 731 | 10 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 732 | 10 | continue; |
|
| 733 | } |
||
| 734 | |||
| 735 | 160 | if ($isChangeTrackingNotify) { |
|
| 736 | 2 | continue; |
|
| 737 | } |
||
| 738 | |||
| 739 | // ignore inverse side of reference-many relationship |
||
| 740 | 159 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'many' && $class->fieldMappings[$propName]['isInverseSide']) { |
|
| 741 | continue; |
||
| 742 | } |
||
| 743 | |||
| 744 | // Persistent collection was exchanged with the "originally" |
||
| 745 | // created one. This can only mean it was cloned and replaced |
||
| 746 | // on another document. |
||
| 747 | 159 | if ($actualValue instanceof PersistentCollection && $actualValue->getOwner() !== $document) { |
|
| 748 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 749 | 6 | } |
|
| 750 | |||
| 751 | // if embed-many or reference-many relationship |
||
| 752 | 159 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 753 | 25 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 754 | /* If original collection was exchanged with a non-empty value |
||
| 755 | * and $set will be issued, there is no need to $unset it first |
||
| 756 | */ |
||
| 757 | 25 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 758 | 7 | continue; |
|
| 759 | } |
||
| 760 | 19 | if ($orgValue instanceof PersistentCollection) { |
|
| 761 | 17 | $this->scheduleCollectionDeletion($orgValue); |
|
| 762 | 17 | } |
|
| 763 | 19 | continue; |
|
| 764 | } |
||
| 765 | |||
| 766 | // skip equivalent date values |
||
| 767 | 145 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 768 | 35 | $dateType = Type::getType('date'); |
|
| 769 | 35 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 770 | 35 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 771 | |||
| 772 | 35 | if ($dbOrgValue instanceof \MongoDate && $dbActualValue instanceof \MongoDate && $dbOrgValue == $dbActualValue) { |
|
| 773 | 29 | continue; |
|
| 774 | } |
||
| 775 | 9 | } |
|
| 776 | |||
| 777 | // regular field |
||
| 778 | 129 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 779 | 274 | } |
|
| 780 | 274 | if ($changeSet) { |
|
| 781 | 161 | $this->documentChangeSets[$oid] = ($recompute && isset($this->documentChangeSets[$oid])) |
|
| 782 | 161 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 783 | 13 | : $changeSet; |
|
| 784 | |||
| 785 | 161 | $this->originalDocumentData[$oid] = $actualData; |
|
| 786 | 161 | $this->scheduleForUpdate($document); |
|
| 787 | 161 | } |
|
| 788 | } |
||
| 789 | |||
| 790 | // Look for changes in associations of the document |
||
| 791 | 553 | $associationMappings = array_filter( |
|
| 792 | 553 | $class->associationMappings, |
|
| 793 | function ($assoc) { return empty($assoc['notSaved']); } |
||
| 794 | 553 | ); |
|
| 795 | |||
| 796 | 553 | foreach ($associationMappings as $mapping) { |
|
| 797 | 427 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 798 | |||
| 799 | 427 | if ($value === null) { |
|
| 800 | 287 | continue; |
|
| 801 | } |
||
| 802 | |||
| 803 | 418 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 804 | |||
| 805 | 417 | if (isset($mapping['reference'])) { |
|
| 806 | 314 | continue; |
|
| 807 | } |
||
| 808 | |||
| 809 | 326 | $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value->unwrap(); |
|
| 810 | |||
| 811 | 326 | foreach ($values as $obj) { |
|
| 812 | 169 | $oid2 = spl_object_hash($obj); |
|
| 813 | |||
| 814 | 169 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 815 | 167 | $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value); |
|
| 816 | |||
| 817 | 167 | if ( ! $isNewDocument) { |
|
| 818 | 71 | $this->scheduleForUpdate($document); |
|
| 819 | 71 | } |
|
| 820 | |||
| 821 | 167 | break; |
|
| 822 | } |
||
| 823 | 326 | } |
|
| 824 | 552 | } |
|
| 825 | 552 | } |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Computes all the changes that have been done to documents and collections |
||
| 829 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 830 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 831 | */ |
||
| 832 | 551 | public function computeChangeSets() |
|
| 833 | { |
||
| 834 | 551 | $this->computeScheduleInsertsChangeSets(); |
|
| 835 | 550 | $this->computeScheduleUpsertsChangeSets(); |
|
| 836 | |||
| 837 | // Compute changes for other MANAGED documents. Change tracking policies take effect here. |
||
| 838 | 550 | foreach ($this->identityMap as $className => $documents) { |
|
| 839 | 550 | $class = $this->dm->getClassMetadata($className); |
|
| 840 | 550 | if ($class->isEmbeddedDocument) { |
|
| 841 | /* we do not want to compute changes to embedded documents up front |
||
| 842 | * in case embedded document was replaced and its changeset |
||
| 843 | * would corrupt data. Embedded documents' change set will |
||
| 844 | * be calculated by reachability from owning document. |
||
| 845 | */ |
||
| 846 | 158 | continue; |
|
| 847 | } |
||
| 848 | |||
| 849 | // If change tracking is explicit or happens through notification, then only compute |
||
| 850 | // changes on document of that type that are explicitly marked for synchronization. |
||
| 851 | 550 | switch (true) { |
|
| 852 | 550 | case ($class->isChangeTrackingDeferredImplicit()): |
|
| 853 | 549 | $documentsToProcess = $documents; |
|
| 854 | 549 | break; |
|
| 855 | |||
| 856 | 3 | case (isset($this->scheduledForDirtyCheck[$className])): |
|
| 857 | 2 | $documentsToProcess = $this->scheduledForDirtyCheck[$className]; |
|
| 858 | 2 | break; |
|
| 859 | |||
| 860 | 3 | default: |
|
| 861 | 3 | $documentsToProcess = array(); |
|
| 862 | |||
| 863 | 3 | } |
|
| 864 | |||
| 865 | 550 | foreach ($documentsToProcess as $document) { |
|
| 866 | // Ignore uninitialized proxy objects |
||
| 867 | 546 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 868 | 10 | continue; |
|
| 869 | } |
||
| 870 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 871 | 546 | $oid = spl_object_hash($document); |
|
| 872 | 546 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 873 | 546 | && ! isset($this->documentUpserts[$oid]) |
|
| 874 | 546 | && ! isset($this->documentDeletions[$oid]) |
|
| 875 | 546 | && isset($this->documentStates[$oid]) |
|
| 876 | 546 | ) { |
|
| 877 | 259 | $this->computeChangeSet($class, $document); |
|
| 878 | 259 | } |
|
| 879 | 550 | } |
|
| 880 | 550 | } |
|
| 881 | 550 | } |
|
| 882 | |||
| 883 | /** |
||
| 884 | * Computes the changes of an association. |
||
| 885 | * |
||
| 886 | * @param object $parentDocument |
||
| 887 | * @param array $assoc |
||
| 888 | * @param mixed $value The value of the association. |
||
| 889 | * @throws \InvalidArgumentException |
||
| 890 | */ |
||
| 891 | 418 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 892 | { |
||
| 893 | 418 | $isNewParentDocument = isset($this->documentInsertions[spl_object_hash($parentDocument)]); |
|
| 894 | 418 | $class = $this->dm->getClassMetadata(get_class($parentDocument)); |
|
| 895 | 418 | $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); |
|
| 896 | |||
| 897 | 418 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
|
| 898 | 8 | return; |
|
| 899 | } |
||
| 900 | |||
| 901 | 417 | if ($value instanceof PersistentCollection && $value->isDirty() && ($assoc['isOwningSide'] || isset($assoc['embedded']))) { |
|
| 902 | 225 | if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) { |
|
| 903 | 221 | $this->scheduleCollectionUpdate($value); |
|
| 904 | 221 | } |
|
| 905 | 225 | $topmostOwner = $this->getOwningDocument($value->getOwner()); |
|
| 906 | 225 | $this->visitedCollections[spl_object_hash($topmostOwner)][] = $value; |
|
| 907 | 225 | if ( ! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) { |
|
| 908 | 132 | $value->initialize(); |
|
| 909 | 132 | foreach ($value->getDeletedDocuments() as $orphan) { |
|
| 910 | 21 | $this->scheduleOrphanRemoval($orphan); |
|
| 911 | 132 | } |
|
| 912 | 132 | } |
|
| 913 | 225 | } |
|
| 914 | |||
| 915 | // Look through the documents, and in any of their associations, |
||
| 916 | // for transient (new) documents, recursively. ("Persistence by reachability") |
||
| 917 | // Unwrap. Uninitialized collections will simply be empty. |
||
| 918 | 417 | $unwrappedValue = ($assoc['type'] === ClassMetadata::ONE) ? array($value) : $value->unwrap(); |
|
| 919 | |||
| 920 | 417 | $count = 0; |
|
| 921 | 417 | foreach ($unwrappedValue as $key => $entry) { |
|
| 922 | 322 | if ( ! is_object($entry)) { |
|
| 923 | 1 | throw new \InvalidArgumentException( |
|
| 924 | 1 | sprintf('Expected object, found "%s" in %s::%s', $entry, get_class($parentDocument), $assoc['name']) |
|
| 925 | 1 | ); |
|
| 926 | } |
||
| 927 | |||
| 928 | 321 | $targetClass = $this->dm->getClassMetadata(get_class($entry)); |
|
| 929 | |||
| 930 | 321 | $state = $this->getDocumentState($entry, self::STATE_NEW); |
|
| 931 | |||
| 932 | // Handle "set" strategy for multi-level hierarchy |
||
| 933 | 321 | $pathKey = ! isset($assoc['strategy']) || CollectionHelper::isList($assoc['strategy']) ? $count : $key; |
|
| 934 | 321 | $path = $assoc['type'] === 'many' ? $assoc['name'] . '.' . $pathKey : $assoc['name']; |
|
| 935 | |||
| 936 | 321 | $count++; |
|
| 937 | |||
| 938 | switch ($state) { |
||
| 939 | 321 | case self::STATE_NEW: |
|
| 940 | 56 | if ( ! $assoc['isCascadePersist']) { |
|
| 941 | throw new \InvalidArgumentException("A new document was found through a relationship that was not" |
||
| 942 | . " configured to cascade persist operations: " . self::objToStr($entry) . "." |
||
| 943 | . " Explicitly persist the new document or configure cascading persist operations" |
||
| 944 | . " on the relationship."); |
||
| 945 | } |
||
| 946 | |||
| 947 | 56 | $this->persistNew($targetClass, $entry); |
|
| 948 | 56 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 949 | 56 | $this->computeChangeSet($targetClass, $entry); |
|
| 950 | 56 | break; |
|
| 951 | |||
| 952 | 317 | case self::STATE_MANAGED: |
|
| 953 | 317 | if ($targetClass->isEmbeddedDocument) { |
|
| 954 | 161 | list(, $knownParent, ) = $this->getParentAssociation($entry); |
|
| 955 | 161 | if ($knownParent && $knownParent !== $parentDocument) { |
|
| 956 | 6 | $entry = clone $entry; |
|
| 957 | 6 | if ($assoc['type'] === ClassMetadata::ONE) { |
|
| 958 | 3 | $class->setFieldValue($parentDocument, $assoc['name'], $entry); |
|
| 959 | 3 | $this->setOriginalDocumentProperty(spl_object_hash($parentDocument), $assoc['name'], $entry); |
|
| 960 | 3 | } else { |
|
| 961 | // must use unwrapped value to not trigger orphan removal |
||
| 962 | 6 | $unwrappedValue[$key] = $entry; |
|
| 963 | } |
||
| 964 | 6 | $this->persistNew($targetClass, $entry); |
|
| 965 | 6 | } |
|
| 966 | 161 | $this->setParentAssociation($entry, $assoc, $parentDocument, $path); |
|
| 967 | 161 | $this->computeChangeSet($targetClass, $entry); |
|
| 968 | 161 | } |
|
| 969 | 317 | break; |
|
| 970 | |||
| 971 | 1 | case self::STATE_REMOVED: |
|
| 972 | // Consume the $value as array (it's either an array or an ArrayAccess) |
||
| 973 | // and remove the element from Collection. |
||
| 974 | 1 | if ($assoc['type'] === ClassMetadata::MANY) { |
|
| 975 | unset($value[$key]); |
||
| 976 | } |
||
| 977 | 1 | break; |
|
| 978 | |||
| 979 | case self::STATE_DETACHED: |
||
| 980 | // Can actually not happen right now as we assume STATE_NEW, |
||
| 981 | // so the exception will be raised from the DBAL layer (constraint violation). |
||
| 982 | throw new \InvalidArgumentException("A detached document was found through a " |
||
| 983 | . "relationship during cascading a persist operation."); |
||
| 984 | break; |
||
| 985 | |||
| 986 | default: |
||
| 987 | // MANAGED associated documents are already taken into account |
||
| 988 | // during changeset calculation anyway, since they are in the identity map. |
||
| 989 | |||
| 990 | } |
||
| 991 | 416 | } |
|
| 992 | 416 | } |
|
| 993 | |||
| 994 | /** |
||
| 995 | * INTERNAL: |
||
| 996 | * Computes the changeset of an individual document, independently of the |
||
| 997 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 998 | * |
||
| 999 | * The passed document must be a managed document. If the document already has a change set |
||
| 1000 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1001 | * whereby changes detected in this method prevail. |
||
| 1002 | * |
||
| 1003 | * @ignore |
||
| 1004 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1005 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1006 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1007 | */ |
||
| 1008 | 19 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1027 | |||
| 1028 | /** |
||
| 1029 | * @param ClassMetadata $class |
||
| 1030 | * @param object $document |
||
| 1031 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1032 | */ |
||
| 1033 | 571 | private function persistNew(ClassMetadata $class, $document) |
|
| 1034 | { |
||
| 1035 | 571 | $oid = spl_object_hash($document); |
|
| 1036 | 571 | View Code Duplication | if ( ! empty($class->lifecycleCallbacks[Events::prePersist])) { |
| 1037 | 155 | $class->invokeLifecycleCallbacks(Events::prePersist, $document, array(new LifecycleEventArgs($document, $this->dm))); |
|
| 1038 | 155 | } |
|
| 1039 | 571 | View Code Duplication | if ($this->evm->hasListeners(Events::prePersist)) { |
| 1040 | 6 | $this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($document, $this->dm)); |
|
| 1041 | 6 | } |
|
| 1042 | |||
| 1043 | 571 | $upsert = false; |
|
| 1044 | 571 | if ($class->identifier) { |
|
| 1045 | 571 | $idValue = $class->getIdentifierValue($document); |
|
| 1046 | 571 | $upsert = ! $class->isEmbeddedDocument && $idValue !== null; |
|
| 1047 | |||
| 1048 | 571 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
|
| 1049 | 3 | throw new \InvalidArgumentException(sprintf( |
|
| 1050 | 3 | "%s uses NONE identifier generation strategy but no identifier was provided when persisting.", |
|
| 1051 | 3 | get_class($document) |
|
| 1052 | 3 | )); |
|
| 1053 | } |
||
| 1054 | |||
| 1055 | 570 | if ($class->generatorType === ClassMetadata::GENERATOR_TYPE_AUTO && $idValue !== null && ! \MongoId::isValid($idValue)) { |
|
| 1056 | 1 | throw new \InvalidArgumentException(sprintf( |
|
| 1057 | 1 | "%s uses AUTO identifier generation strategy but provided identifier is not valid MongoId.", |
|
| 1058 | 1 | get_class($document) |
|
| 1059 | 1 | )); |
|
| 1060 | } |
||
| 1061 | |||
| 1062 | 569 | if ($class->generatorType !== ClassMetadata::GENERATOR_TYPE_NONE && $idValue === null) { |
|
| 1063 | 498 | $idValue = $class->idGenerator->generate($this->dm, $document); |
|
| 1064 | 498 | $idValue = $class->getPHPIdentifierValue($class->getDatabaseIdentifierValue($idValue)); |
|
| 1065 | 498 | $class->setIdentifierValue($document, $idValue); |
|
| 1066 | 498 | } |
|
| 1067 | |||
| 1068 | 569 | $this->documentIdentifiers[$oid] = $idValue; |
|
| 1069 | 569 | } else { |
|
| 1070 | // this is for embedded documents without identifiers |
||
| 1071 | 142 | $this->documentIdentifiers[$oid] = $oid; |
|
| 1072 | } |
||
| 1073 | |||
| 1074 | 569 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 1075 | |||
| 1076 | 569 | if ($upsert) { |
|
| 1077 | 81 | $this->scheduleForUpsert($class, $document); |
|
| 1078 | 81 | } else { |
|
| 1079 | 503 | $this->scheduleForInsert($class, $document); |
|
| 1080 | } |
||
| 1081 | 569 | } |
|
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Cascades the postPersist events to embedded documents. |
||
| 1085 | * |
||
| 1086 | * @param ClassMetadata $class |
||
| 1087 | * @param object $document |
||
| 1088 | */ |
||
| 1089 | 547 | private function cascadePostPersist(ClassMetadata $class, $document) |
|
| 1090 | { |
||
| 1091 | 547 | $hasPostPersistListeners = $this->evm->hasListeners(Events::postPersist); |
|
| 1092 | |||
| 1093 | 547 | $embeddedMappings = array_filter( |
|
| 1094 | 547 | $class->associationMappings, |
|
| 1095 | function($assoc) { return ! empty($assoc['embedded']); } |
||
| 1096 | 547 | ); |
|
| 1097 | |||
| 1098 | 547 | foreach ($embeddedMappings as $mapping) { |
|
| 1099 | 334 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 1100 | |||
| 1101 | 334 | if ($value === null) { |
|
| 1102 | 213 | continue; |
|
| 1103 | } |
||
| 1104 | |||
| 1105 | 316 | $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value; |
|
| 1106 | |||
| 1107 | 316 | if (isset($mapping['targetDocument'])) { |
|
| 1108 | 305 | $embeddedClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1109 | 305 | } |
|
| 1110 | |||
| 1111 | 316 | foreach ($values as $embeddedDocument) { |
|
| 1112 | 159 | if ( ! isset($mapping['targetDocument'])) { |
|
| 1113 | 13 | $embeddedClass = $this->dm->getClassMetadata(get_class($embeddedDocument)); |
|
| 1114 | 13 | } |
|
| 1115 | |||
| 1116 | 159 | View Code Duplication | if ( ! empty($embeddedClass->lifecycleCallbacks[Events::postPersist])) { |
| 1117 | 9 | $embeddedClass->invokeLifecycleCallbacks(Events::postPersist, $embeddedDocument, array(new LifecycleEventArgs($document, $this->dm))); |
|
| 1118 | 9 | } |
|
| 1119 | 159 | if ($hasPostPersistListeners) { |
|
| 1120 | 4 | $this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($embeddedDocument, $this->dm)); |
|
| 1121 | 4 | } |
|
| 1122 | 159 | $this->cascadePostPersist($embeddedClass, $embeddedDocument); |
|
| 1123 | 316 | } |
|
| 1124 | 547 | } |
|
| 1125 | 547 | } |
|
| 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 | 482 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Executes all document upserts for documents of the specified type. |
||
| 1161 | * |
||
| 1162 | * @param ClassMetadata $class |
||
| 1163 | * @param array $documents Array of documents to upsert |
||
| 1164 | * @param array $options Array of options to be used with batchInsert() |
||
| 1165 | */ |
||
| 1166 | 78 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Executes all document updates for documents of the specified type. |
||
| 1194 | * |
||
| 1195 | * @param Mapping\ClassMetadata $class |
||
| 1196 | * @param array $documents Array of documents to update |
||
| 1197 | * @param array $options Array of options to be used with update() |
||
| 1198 | */ |
||
| 1199 | 214 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Cascades the preUpdate event to embedded documents. |
||
| 1246 | * |
||
| 1247 | * @param ClassMetadata $class |
||
| 1248 | * @param object $document |
||
| 1249 | */ |
||
| 1250 | 214 | private function cascadePreUpdate(ClassMetadata $class, $document) |
|
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Cascades the postUpdate and postPersist events to embedded documents. |
||
| 1299 | * |
||
| 1300 | * @param ClassMetadata $class |
||
| 1301 | * @param object $document |
||
| 1302 | */ |
||
| 1303 | 210 | private function cascadePostUpdate(ClassMetadata $class, $document) |
|
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Executes all document deletions for documents of the specified type. |
||
| 1357 | * |
||
| 1358 | * @param ClassMetadata $class |
||
| 1359 | * @param array $documents Array of documents to delete |
||
| 1360 | * @param array $options Array of options to be used with remove() |
||
| 1361 | */ |
||
| 1362 | 62 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1402 | |||
| 1403 | /** |
||
| 1404 | * Schedules a document for insertion into the database. |
||
| 1405 | * If the document already has an identifier, it will be added to the |
||
| 1406 | * identity map. |
||
| 1407 | * |
||
| 1408 | * @param ClassMetadata $class |
||
| 1409 | * @param object $document The document to schedule for insertion. |
||
| 1410 | * @throws \InvalidArgumentException |
||
| 1411 | */ |
||
| 1412 | 506 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1432 | |||
| 1433 | /** |
||
| 1434 | * Schedules a document for upsert into the database and adds it to the |
||
| 1435 | * identity map |
||
| 1436 | * |
||
| 1437 | * @param ClassMetadata $class |
||
| 1438 | * @param object $document The document to schedule for upsert. |
||
| 1439 | * @throws \InvalidArgumentException |
||
| 1440 | */ |
||
| 1441 | 84 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Checks whether a document is scheduled for insertion. |
||
| 1465 | * |
||
| 1466 | * @param object $document |
||
| 1467 | * @return boolean |
||
| 1468 | */ |
||
| 1469 | 70 | public function isScheduledForInsert($document) |
|
| 1473 | |||
| 1474 | /** |
||
| 1475 | * Checks whether a document is scheduled for upsert. |
||
| 1476 | * |
||
| 1477 | * @param object $document |
||
| 1478 | * @return boolean |
||
| 1479 | */ |
||
| 1480 | 5 | public function isScheduledForUpsert($document) |
|
| 1484 | |||
| 1485 | /** |
||
| 1486 | * Schedules a document for being updated. |
||
| 1487 | * |
||
| 1488 | * @param object $document The document to schedule for being updated. |
||
| 1489 | * @throws \InvalidArgumentException |
||
| 1490 | */ |
||
| 1491 | 223 | public function scheduleForUpdate($document) |
|
| 1508 | |||
| 1509 | /** |
||
| 1510 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1511 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1512 | * at commit time. |
||
| 1513 | * |
||
| 1514 | * @param object $document |
||
| 1515 | * @return boolean |
||
| 1516 | */ |
||
| 1517 | 13 | public function isScheduledForUpdate($document) |
|
| 1521 | |||
| 1522 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1527 | |||
| 1528 | /** |
||
| 1529 | * INTERNAL: |
||
| 1530 | * Schedules a document for deletion. |
||
| 1531 | * |
||
| 1532 | * @param object $document |
||
| 1533 | */ |
||
| 1534 | 67 | public function scheduleForDelete($document) |
|
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1563 | * of work. |
||
| 1564 | * |
||
| 1565 | * @param object $document |
||
| 1566 | * @return boolean |
||
| 1567 | */ |
||
| 1568 | 8 | public function isScheduledForDelete($document) |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1575 | * |
||
| 1576 | * @param $document |
||
| 1577 | * @return boolean |
||
| 1578 | */ |
||
| 1579 | 224 | public function isDocumentScheduled($document) |
|
| 1587 | |||
| 1588 | /** |
||
| 1589 | * INTERNAL: |
||
| 1590 | * Registers a document in the identity map. |
||
| 1591 | * |
||
| 1592 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1593 | * the root document. Identifiers are serialized before being used as array |
||
| 1594 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1595 | * |
||
| 1596 | * @ignore |
||
| 1597 | * @param object $document The document to register. |
||
| 1598 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1599 | * the document in question is already managed. |
||
| 1600 | */ |
||
| 1601 | 598 | public function addToIdentityMap($document) |
|
| 1619 | |||
| 1620 | /** |
||
| 1621 | * Gets the state of a document with regard to the current unit of work. |
||
| 1622 | * |
||
| 1623 | * @param object $document |
||
| 1624 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1625 | * This parameter can be set to improve performance of document state detection |
||
| 1626 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1627 | * is either known or does not matter for the caller of the method. |
||
| 1628 | * @return int The document state. |
||
| 1629 | */ |
||
| 1630 | 574 | public function getDocumentState($document, $assume = null) |
|
| 1680 | |||
| 1681 | /** |
||
| 1682 | * INTERNAL: |
||
| 1683 | * Removes a document from the identity map. This effectively detaches the |
||
| 1684 | * document from the persistence management of Doctrine. |
||
| 1685 | * |
||
| 1686 | * @ignore |
||
| 1687 | * @param object $document |
||
| 1688 | * @throws \InvalidArgumentException |
||
| 1689 | * @return boolean |
||
| 1690 | */ |
||
| 1691 | 76 | public function removeFromIdentityMap($document) |
|
| 1711 | |||
| 1712 | /** |
||
| 1713 | * INTERNAL: |
||
| 1714 | * Gets a document in the identity map by its identifier hash. |
||
| 1715 | * |
||
| 1716 | * @ignore |
||
| 1717 | * @param mixed $id Document identifier |
||
| 1718 | * @param ClassMetadata $class Document class |
||
| 1719 | * @return object |
||
| 1720 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1721 | */ |
||
| 1722 | 31 | public function getById($id, ClassMetadata $class) |
|
| 1732 | |||
| 1733 | /** |
||
| 1734 | * INTERNAL: |
||
| 1735 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1736 | * for the given hash, FALSE is returned. |
||
| 1737 | * |
||
| 1738 | * @ignore |
||
| 1739 | * @param mixed $id Document identifier |
||
| 1740 | * @param ClassMetadata $class Document class |
||
| 1741 | * @return mixed The found document or FALSE. |
||
| 1742 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1743 | */ |
||
| 1744 | 290 | public function tryGetById($id, ClassMetadata $class) |
|
| 1755 | |||
| 1756 | /** |
||
| 1757 | * Schedules a document for dirty-checking at commit-time. |
||
| 1758 | * |
||
| 1759 | * @param object $document The document to schedule for dirty-checking. |
||
| 1760 | * @todo Rename: scheduleForSynchronization |
||
| 1761 | */ |
||
| 1762 | 2 | public function scheduleForDirtyCheck($document) |
|
| 1767 | |||
| 1768 | /** |
||
| 1769 | * Checks whether a document is registered in the identity map. |
||
| 1770 | * |
||
| 1771 | * @param object $document |
||
| 1772 | * @return boolean |
||
| 1773 | */ |
||
| 1774 | 76 | public function isInIdentityMap($document) |
|
| 1787 | |||
| 1788 | /** |
||
| 1789 | * @param object $document |
||
| 1790 | * @return string |
||
| 1791 | */ |
||
| 1792 | 598 | private function getIdForIdentityMap($document) |
|
| 1805 | |||
| 1806 | /** |
||
| 1807 | * INTERNAL: |
||
| 1808 | * Checks whether an identifier exists in the identity map. |
||
| 1809 | * |
||
| 1810 | * @ignore |
||
| 1811 | * @param string $id |
||
| 1812 | * @param string $rootClassName |
||
| 1813 | * @return boolean |
||
| 1814 | */ |
||
| 1815 | public function containsId($id, $rootClassName) |
||
| 1819 | |||
| 1820 | /** |
||
| 1821 | * Persists a document as part of the current unit of work. |
||
| 1822 | * |
||
| 1823 | * @param object $document The document to persist. |
||
| 1824 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1825 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1826 | */ |
||
| 1827 | 569 | public function persist($document) |
|
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Saves a document as part of the current unit of work. |
||
| 1839 | * This method is internally called during save() cascades as it tracks |
||
| 1840 | * the already visited documents to prevent infinite recursions. |
||
| 1841 | * |
||
| 1842 | * NOTE: This method always considers documents that are not yet known to |
||
| 1843 | * this UnitOfWork as NEW. |
||
| 1844 | * |
||
| 1845 | * @param object $document The document to persist. |
||
| 1846 | * @param array $visited The already visited documents. |
||
| 1847 | * @throws \InvalidArgumentException |
||
| 1848 | * @throws MongoDBException |
||
| 1849 | */ |
||
| 1850 | 568 | private function doPersist($document, array &$visited) |
|
| 1891 | |||
| 1892 | /** |
||
| 1893 | * Deletes a document as part of the current unit of work. |
||
| 1894 | * |
||
| 1895 | * @param object $document The document to remove. |
||
| 1896 | */ |
||
| 1897 | 66 | public function remove($document) |
|
| 1902 | |||
| 1903 | /** |
||
| 1904 | * Deletes a document as part of the current unit of work. |
||
| 1905 | * |
||
| 1906 | * This method is internally called during delete() cascades as it tracks |
||
| 1907 | * the already visited documents to prevent infinite recursions. |
||
| 1908 | * |
||
| 1909 | * @param object $document The document to delete. |
||
| 1910 | * @param array $visited The map of the already visited documents. |
||
| 1911 | * @throws MongoDBException |
||
| 1912 | */ |
||
| 1913 | 66 | private function doRemove($document, array &$visited) |
|
| 1950 | |||
| 1951 | /** |
||
| 1952 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1953 | * |
||
| 1954 | * @param object $document |
||
| 1955 | * @return object The managed copy of the document. |
||
| 1956 | */ |
||
| 1957 | 13 | public function merge($document) |
|
| 1963 | |||
| 1964 | /** |
||
| 1965 | * Executes a merge operation on a document. |
||
| 1966 | * |
||
| 1967 | * @param object $document |
||
| 1968 | * @param array $visited |
||
| 1969 | * @param object|null $prevManagedCopy |
||
| 1970 | * @param array|null $assoc |
||
| 1971 | * |
||
| 1972 | * @return object The managed copy of the document. |
||
| 1973 | * |
||
| 1974 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1975 | * @throws LockException If the document uses optimistic locking through a |
||
| 1976 | * version attribute and the version check against the |
||
| 1977 | * managed copy fails. |
||
| 1978 | */ |
||
| 1979 | 13 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 2157 | |||
| 2158 | /** |
||
| 2159 | * Detaches a document from the persistence management. It's persistence will |
||
| 2160 | * no longer be managed by Doctrine. |
||
| 2161 | * |
||
| 2162 | * @param object $document The document to detach. |
||
| 2163 | */ |
||
| 2164 | 9 | public function detach($document) |
|
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Executes a detach operation on the given document. |
||
| 2172 | * |
||
| 2173 | * @param object $document |
||
| 2174 | * @param array $visited |
||
| 2175 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 2176 | */ |
||
| 2177 | 12 | private function doDetach($document, array &$visited) |
|
| 2202 | |||
| 2203 | /** |
||
| 2204 | * Refreshes the state of the given document from the database, overwriting |
||
| 2205 | * any local, unpersisted changes. |
||
| 2206 | * |
||
| 2207 | * @param object $document The document to refresh. |
||
| 2208 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2209 | */ |
||
| 2210 | 20 | public function refresh($document) |
|
| 2215 | |||
| 2216 | /** |
||
| 2217 | * Executes a refresh operation on a document. |
||
| 2218 | * |
||
| 2219 | * @param object $document The document to refresh. |
||
| 2220 | * @param array $visited The already visited documents during cascades. |
||
| 2221 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2222 | */ |
||
| 2223 | 20 | private function doRefresh($document, array &$visited) |
|
| 2245 | |||
| 2246 | /** |
||
| 2247 | * Cascades a refresh operation to associated documents. |
||
| 2248 | * |
||
| 2249 | * @param object $document |
||
| 2250 | * @param array $visited |
||
| 2251 | */ |
||
| 2252 | 19 | private function cascadeRefresh($document, array &$visited) |
|
| 2276 | |||
| 2277 | /** |
||
| 2278 | * Cascades a detach operation to associated documents. |
||
| 2279 | * |
||
| 2280 | * @param object $document |
||
| 2281 | * @param array $visited |
||
| 2282 | */ |
||
| 2283 | 12 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2304 | /** |
||
| 2305 | * Cascades a merge operation to associated documents. |
||
| 2306 | * |
||
| 2307 | * @param object $document |
||
| 2308 | * @param object $managedCopy |
||
| 2309 | * @param array $visited |
||
| 2310 | */ |
||
| 2311 | 13 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Cascades the save operation to associated documents. |
||
| 2345 | * |
||
| 2346 | * @param object $document |
||
| 2347 | * @param array $visited |
||
| 2348 | */ |
||
| 2349 | 566 | private function cascadePersist($document, array &$visited) |
|
| 2396 | |||
| 2397 | /** |
||
| 2398 | * Cascades the delete operation to associated documents. |
||
| 2399 | * |
||
| 2400 | * @param object $document |
||
| 2401 | * @param array $visited |
||
| 2402 | */ |
||
| 2403 | 66 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2425 | |||
| 2426 | /** |
||
| 2427 | * Acquire a lock on the given document. |
||
| 2428 | * |
||
| 2429 | * @param object $document |
||
| 2430 | * @param int $lockMode |
||
| 2431 | * @param int $lockVersion |
||
| 2432 | * @throws LockException |
||
| 2433 | * @throws \InvalidArgumentException |
||
| 2434 | */ |
||
| 2435 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2459 | |||
| 2460 | /** |
||
| 2461 | * Releases a lock on the given document. |
||
| 2462 | * |
||
| 2463 | * @param object $document |
||
| 2464 | * @throws \InvalidArgumentException |
||
| 2465 | */ |
||
| 2466 | 1 | public function unlock($document) |
|
| 2474 | |||
| 2475 | /** |
||
| 2476 | * Clears the UnitOfWork. |
||
| 2477 | * |
||
| 2478 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2479 | */ |
||
| 2480 | 386 | public function clear($documentName = null) |
|
| 2513 | |||
| 2514 | /** |
||
| 2515 | * INTERNAL: |
||
| 2516 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2517 | * invoked on that document at the beginning of the next commit of this |
||
| 2518 | * UnitOfWork. |
||
| 2519 | * |
||
| 2520 | * @ignore |
||
| 2521 | * @param object $document |
||
| 2522 | */ |
||
| 2523 | 47 | public function scheduleOrphanRemoval($document) |
|
| 2527 | |||
| 2528 | /** |
||
| 2529 | * INTERNAL: |
||
| 2530 | * Unschedules an embedded or referenced object for removal. |
||
| 2531 | * |
||
| 2532 | * @ignore |
||
| 2533 | * @param object $document |
||
| 2534 | */ |
||
| 2535 | 103 | public function unscheduleOrphanRemoval($document) |
|
| 2542 | |||
| 2543 | /** |
||
| 2544 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2545 | * 1) sets owner if it was cloned |
||
| 2546 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2547 | * 3) NOP if state is OK |
||
| 2548 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2549 | * |
||
| 2550 | * @param PersistentCollection $coll |
||
| 2551 | * @param object $document |
||
| 2552 | * @param ClassMetadata $class |
||
| 2553 | * @param string $propName |
||
| 2554 | * @return PersistentCollection |
||
| 2555 | */ |
||
| 2556 | 8 | private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, ClassMetadata $class, $propName) |
|
| 2576 | |||
| 2577 | /** |
||
| 2578 | * INTERNAL: |
||
| 2579 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2580 | * |
||
| 2581 | * @param PersistentCollection $coll |
||
| 2582 | */ |
||
| 2583 | 41 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2592 | |||
| 2593 | /** |
||
| 2594 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2595 | * |
||
| 2596 | * @param PersistentCollection $coll |
||
| 2597 | * @return boolean |
||
| 2598 | */ |
||
| 2599 | 106 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
|
| 2603 | |||
| 2604 | /** |
||
| 2605 | * INTERNAL: |
||
| 2606 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2607 | * |
||
| 2608 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2609 | */ |
||
| 2610 | 205 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollection $coll) |
| 2619 | |||
| 2620 | /** |
||
| 2621 | * INTERNAL: |
||
| 2622 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2623 | * |
||
| 2624 | * @param PersistentCollection $coll |
||
| 2625 | */ |
||
| 2626 | 221 | public function scheduleCollectionUpdate(PersistentCollection $coll) |
|
| 2641 | |||
| 2642 | /** |
||
| 2643 | * INTERNAL: |
||
| 2644 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2645 | * |
||
| 2646 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2647 | */ |
||
| 2648 | 205 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollection $coll) |
| 2657 | |||
| 2658 | /** |
||
| 2659 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2660 | * |
||
| 2661 | * @param PersistentCollection $coll |
||
| 2662 | * @return boolean |
||
| 2663 | */ |
||
| 2664 | 122 | public function isCollectionScheduledForUpdate(PersistentCollection $coll) |
|
| 2668 | |||
| 2669 | /** |
||
| 2670 | * INTERNAL: |
||
| 2671 | * Gets PersistentCollections that have been visited during computing change |
||
| 2672 | * set of $document |
||
| 2673 | * |
||
| 2674 | * @param object $document |
||
| 2675 | * @return PersistentCollection[] |
||
| 2676 | */ |
||
| 2677 | 534 | public function getVisitedCollections($document) |
|
| 2684 | |||
| 2685 | /** |
||
| 2686 | * INTERNAL: |
||
| 2687 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2688 | * |
||
| 2689 | * @param object $document |
||
| 2690 | * @return array |
||
| 2691 | */ |
||
| 2692 | 534 | public function getScheduledCollections($document) |
|
| 2699 | |||
| 2700 | /** |
||
| 2701 | * Checks whether the document is related to a PersistentCollection |
||
| 2702 | * scheduled for update or deletion. |
||
| 2703 | * |
||
| 2704 | * @param object $document |
||
| 2705 | * @return boolean |
||
| 2706 | */ |
||
| 2707 | 59 | public function hasScheduledCollections($document) |
|
| 2711 | |||
| 2712 | /** |
||
| 2713 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2714 | * a collection scheduled for update or deletion. |
||
| 2715 | * |
||
| 2716 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2717 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2718 | * |
||
| 2719 | * If the collection is nested within atomic collection, it is immediately |
||
| 2720 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2721 | * calculating update data way easier. |
||
| 2722 | * |
||
| 2723 | * @param PersistentCollection $coll |
||
| 2724 | */ |
||
| 2725 | 223 | private function scheduleCollectionOwner(PersistentCollection $coll) |
|
| 2748 | |||
| 2749 | /** |
||
| 2750 | * Get the top-most owning document of a given document |
||
| 2751 | * |
||
| 2752 | * If a top-level document is provided, that same document will be returned. |
||
| 2753 | * For an embedded document, we will walk through parent associations until |
||
| 2754 | * we find a top-level document. |
||
| 2755 | * |
||
| 2756 | * @param object $document |
||
| 2757 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2758 | * @return object |
||
| 2759 | */ |
||
| 2760 | 225 | public function getOwningDocument($document) |
|
| 2776 | |||
| 2777 | /** |
||
| 2778 | * Gets the class name for an association (embed or reference) with respect |
||
| 2779 | * to any discriminator value. |
||
| 2780 | * |
||
| 2781 | * @param array $mapping Field mapping for the association |
||
| 2782 | * @param array|null $data Data for the embedded document or reference |
||
| 2783 | */ |
||
| 2784 | 207 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2817 | |||
| 2818 | /** |
||
| 2819 | * INTERNAL: |
||
| 2820 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2821 | * |
||
| 2822 | * @ignore |
||
| 2823 | * @param string $className The name of the document class. |
||
| 2824 | * @param array $data The data for the document. |
||
| 2825 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2826 | * @param object The document to be hydrated into in case of creation |
||
| 2827 | * @return object The document instance. |
||
| 2828 | * @internal Highly performance-sensitive method. |
||
| 2829 | */ |
||
| 2830 | 380 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2884 | |||
| 2885 | /** |
||
| 2886 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2887 | * |
||
| 2888 | * @param PersistentCollection $collection The collection to initialize. |
||
| 2889 | */ |
||
| 2890 | 157 | public function loadCollection(PersistentCollection $collection) |
|
| 2894 | |||
| 2895 | /** |
||
| 2896 | * Gets the identity map of the UnitOfWork. |
||
| 2897 | * |
||
| 2898 | * @return array |
||
| 2899 | */ |
||
| 2900 | public function getIdentityMap() |
||
| 2904 | |||
| 2905 | /** |
||
| 2906 | * Gets the original data of a document. The original data is the data that was |
||
| 2907 | * present at the time the document was reconstituted from the database. |
||
| 2908 | * |
||
| 2909 | * @param object $document |
||
| 2910 | * @return array |
||
| 2911 | */ |
||
| 2912 | 1 | public function getOriginalDocumentData($document) |
|
| 2920 | |||
| 2921 | /** |
||
| 2922 | * @ignore |
||
| 2923 | */ |
||
| 2924 | 50 | public function setOriginalDocumentData($document, array $data) |
|
| 2928 | |||
| 2929 | /** |
||
| 2930 | * INTERNAL: |
||
| 2931 | * Sets a property value of the original data array of a document. |
||
| 2932 | * |
||
| 2933 | * @ignore |
||
| 2934 | * @param string $oid |
||
| 2935 | * @param string $property |
||
| 2936 | * @param mixed $value |
||
| 2937 | */ |
||
| 2938 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2942 | |||
| 2943 | /** |
||
| 2944 | * Gets the identifier of a document. |
||
| 2945 | * |
||
| 2946 | * @param object $document |
||
| 2947 | * @return mixed The identifier value |
||
| 2948 | */ |
||
| 2949 | 353 | public function getDocumentIdentifier($document) |
|
| 2954 | |||
| 2955 | /** |
||
| 2956 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2957 | * |
||
| 2958 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2959 | */ |
||
| 2960 | public function hasPendingInsertions() |
||
| 2964 | |||
| 2965 | /** |
||
| 2966 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2967 | * number of documents in the identity map. |
||
| 2968 | * |
||
| 2969 | * @return integer |
||
| 2970 | */ |
||
| 2971 | 2 | public function size() |
|
| 2979 | |||
| 2980 | /** |
||
| 2981 | * INTERNAL: |
||
| 2982 | * Registers a document as managed. |
||
| 2983 | * |
||
| 2984 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2985 | * document class. If the class expects its database identifier to be a |
||
| 2986 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2987 | * document identifiers map will become inconsistent with the identity map. |
||
| 2988 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2989 | * conversion and throw an exception if it's inconsistent. |
||
| 2990 | * |
||
| 2991 | * @param object $document The document. |
||
| 2992 | * @param array $id The identifier values. |
||
| 2993 | * @param array $data The original document data. |
||
| 2994 | */ |
||
| 2995 | 374 | public function registerManaged($document, $id, array $data) |
|
| 3010 | |||
| 3011 | /** |
||
| 3012 | * INTERNAL: |
||
| 3013 | * Clears the property changeset of the document with the given OID. |
||
| 3014 | * |
||
| 3015 | * @param string $oid The document's OID. |
||
| 3016 | */ |
||
| 3017 | 1 | public function clearDocumentChangeSet($oid) |
|
| 3021 | |||
| 3022 | /* PropertyChangedListener implementation */ |
||
| 3023 | |||
| 3024 | /** |
||
| 3025 | * Notifies this UnitOfWork of a property change in a document. |
||
| 3026 | * |
||
| 3027 | * @param object $document The document that owns the property. |
||
| 3028 | * @param string $propertyName The name of the property that changed. |
||
| 3029 | * @param mixed $oldValue The old value of the property. |
||
| 3030 | * @param mixed $newValue The new value of the property. |
||
| 3031 | */ |
||
| 3032 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 3047 | |||
| 3048 | /** |
||
| 3049 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 3050 | * |
||
| 3051 | * @return array |
||
| 3052 | */ |
||
| 3053 | 5 | public function getScheduledDocumentInsertions() |
|
| 3057 | |||
| 3058 | /** |
||
| 3059 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 3060 | * |
||
| 3061 | * @return array |
||
| 3062 | */ |
||
| 3063 | 3 | public function getScheduledDocumentUpserts() |
|
| 3067 | |||
| 3068 | /** |
||
| 3069 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 3070 | * |
||
| 3071 | * @return array |
||
| 3072 | */ |
||
| 3073 | 3 | public function getScheduledDocumentUpdates() |
|
| 3077 | |||
| 3078 | /** |
||
| 3079 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 3080 | * |
||
| 3081 | * @return array |
||
| 3082 | */ |
||
| 3083 | public function getScheduledDocumentDeletions() |
||
| 3087 | |||
| 3088 | /** |
||
| 3089 | * Get the currently scheduled complete collection deletions |
||
| 3090 | * |
||
| 3091 | * @return array |
||
| 3092 | */ |
||
| 3093 | public function getScheduledCollectionDeletions() |
||
| 3097 | |||
| 3098 | /** |
||
| 3099 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 3100 | * |
||
| 3101 | * @return array |
||
| 3102 | */ |
||
| 3103 | public function getScheduledCollectionUpdates() |
||
| 3107 | |||
| 3108 | /** |
||
| 3109 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 3110 | * |
||
| 3111 | * @param object |
||
| 3112 | * @return void |
||
| 3113 | */ |
||
| 3114 | public function initializeObject($obj) |
||
| 3122 | |||
| 3123 | 1 | private static function objToStr($obj) |
|
| 3127 | } |
||
| 3128 |
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.