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 |
||
| 46 | class UnitOfWork implements PropertyChangedListener |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 50 | */ |
||
| 51 | const STATE_MANAGED = 1; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 55 | * and is not (yet) managed by a DocumentManager. |
||
| 56 | */ |
||
| 57 | const STATE_NEW = 2; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * A detached document is an instance with a persistent identity that is not |
||
| 61 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 62 | */ |
||
| 63 | const STATE_DETACHED = 3; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * A removed document instance is an instance with a persistent identity, |
||
| 67 | * associated with a DocumentManager, whose persistent state has been |
||
| 68 | * deleted (or is scheduled for deletion). |
||
| 69 | */ |
||
| 70 | const STATE_REMOVED = 4; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The identity map holds references to all managed documents. |
||
| 74 | * |
||
| 75 | * Documents are grouped by their class name, and then indexed by the |
||
| 76 | * serialized string of their database identifier field or, if the class |
||
| 77 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 78 | * differentiation of values that may be equal (via type juggling) but not |
||
| 79 | * identical. |
||
| 80 | * |
||
| 81 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 82 | * we always take the root class name of the hierarchy. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $identityMap = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Map of all identifiers of managed documents. |
||
| 90 | * Keys are object ids (spl_object_hash). |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $documentIdentifiers = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Map of the original document data of managed documents. |
||
| 98 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 99 | * at commit time. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 103 | * A value will only really be copied if the value in the document is modified |
||
| 104 | * by the user. |
||
| 105 | */ |
||
| 106 | private $originalDocumentData = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 110 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private $documentChangeSets = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The (cached) states of any known documents. |
||
| 118 | * Keys are object ids (spl_object_hash). |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $documentStates = array(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 126 | * |
||
| 127 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 128 | * object hash. This is only used for documents with a change tracking |
||
| 129 | * policy of DEFERRED_EXPLICIT. |
||
| 130 | * |
||
| 131 | * @var array |
||
| 132 | * @todo rename: scheduledForSynchronization |
||
| 133 | */ |
||
| 134 | private $scheduledForDirtyCheck = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * A list of all pending document insertions. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private $documentInsertions = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * A list of all pending document updates. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | private $documentUpdates = array(); |
||
| 149 | |||
| 150 | /** |
||
| 151 | * A list of all pending document upserts. |
||
| 152 | * |
||
| 153 | * @var array |
||
| 154 | */ |
||
| 155 | private $documentUpserts = array(); |
||
| 156 | |||
| 157 | /** |
||
| 158 | * A list of all pending document deletions. |
||
| 159 | * |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private $documentDeletions = array(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * All pending collection deletions. |
||
| 166 | * |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | private $collectionDeletions = array(); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * All pending collection updates. |
||
| 173 | * |
||
| 174 | * @var array |
||
| 175 | */ |
||
| 176 | private $collectionUpdates = array(); |
||
| 177 | |||
| 178 | /** |
||
| 179 | * A list of documents related to collections scheduled for update or deletion |
||
| 180 | * |
||
| 181 | * @var array |
||
| 182 | */ |
||
| 183 | private $hasScheduledCollections = array(); |
||
| 184 | |||
| 185 | /** |
||
| 186 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 187 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 188 | * of their data. |
||
| 189 | * |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | private $visitedCollections = array(); |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 196 | * |
||
| 197 | * @var DocumentManager |
||
| 198 | */ |
||
| 199 | private $dm; |
||
| 200 | |||
| 201 | /** |
||
| 202 | * The EventManager used for dispatching events. |
||
| 203 | * |
||
| 204 | * @var EventManager |
||
| 205 | */ |
||
| 206 | private $evm; |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Additional documents that are scheduled for removal. |
||
| 210 | * |
||
| 211 | * @var array |
||
| 212 | */ |
||
| 213 | private $orphanRemovals = array(); |
||
| 214 | |||
| 215 | /** |
||
| 216 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 217 | * |
||
| 218 | * @var HydratorFactory |
||
| 219 | */ |
||
| 220 | private $hydratorFactory; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * The document persister instances used to persist document instances. |
||
| 224 | * |
||
| 225 | * @var array |
||
| 226 | */ |
||
| 227 | private $persisters = array(); |
||
| 228 | |||
| 229 | /** |
||
| 230 | * The collection persister instance used to persist changes to collections. |
||
| 231 | * |
||
| 232 | * @var Persisters\CollectionPersister |
||
| 233 | */ |
||
| 234 | private $collectionPersister; |
||
| 235 | |||
| 236 | /** |
||
| 237 | * The persistence builder instance used in DocumentPersisters. |
||
| 238 | * |
||
| 239 | * @var PersistenceBuilder |
||
| 240 | */ |
||
| 241 | private $persistenceBuilder; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Array of parent associations between embedded documents |
||
| 245 | * |
||
| 246 | * @todo We might need to clean up this array in clear(), doDetach(), etc. |
||
| 247 | * @var array |
||
| 248 | */ |
||
| 249 | private $parentAssociations = array(); |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @var LifecycleEventManager |
||
| 253 | */ |
||
| 254 | private $lifecycleEventManager; |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 258 | * |
||
| 259 | * @param DocumentManager $dm |
||
| 260 | * @param EventManager $evm |
||
| 261 | * @param HydratorFactory $hydratorFactory |
||
| 262 | */ |
||
| 263 | 949 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 264 | { |
||
| 265 | 949 | $this->dm = $dm; |
|
| 266 | 949 | $this->evm = $evm; |
|
| 267 | 949 | $this->hydratorFactory = $hydratorFactory; |
|
| 268 | 949 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 269 | 949 | } |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 273 | * queries for insert persistence. |
||
| 274 | * |
||
| 275 | * @return PersistenceBuilder $pb |
||
| 276 | */ |
||
| 277 | 689 | public function getPersistenceBuilder() |
|
| 278 | { |
||
| 279 | 689 | if ( ! $this->persistenceBuilder) { |
|
| 280 | 689 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 281 | 689 | } |
|
| 282 | 689 | return $this->persistenceBuilder; |
|
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Sets the parent association for a given embedded document. |
||
| 287 | * |
||
| 288 | * @param object $document |
||
| 289 | * @param array $mapping |
||
| 290 | * @param object $parent |
||
| 291 | * @param string $propertyPath |
||
| 292 | */ |
||
| 293 | 184 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 294 | { |
||
| 295 | 184 | $oid = spl_object_hash($document); |
|
| 296 | 184 | $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath); |
|
| 297 | 184 | } |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Gets the parent association for a given embedded document. |
||
| 301 | * |
||
| 302 | * <code> |
||
| 303 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 304 | * </code> |
||
| 305 | * |
||
| 306 | * @param object $document |
||
| 307 | * @return array $association |
||
| 308 | */ |
||
| 309 | 211 | public function getParentAssociation($document) |
|
| 310 | { |
||
| 311 | 211 | $oid = spl_object_hash($document); |
|
| 312 | 211 | if ( ! isset($this->parentAssociations[$oid])) { |
|
| 313 | 207 | return null; |
|
| 314 | } |
||
| 315 | 168 | return $this->parentAssociations[$oid]; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the document persister instance for the given document name |
||
| 320 | * |
||
| 321 | * @param string $documentName |
||
| 322 | * @return Persisters\DocumentPersister |
||
| 323 | */ |
||
| 324 | 687 | public function getDocumentPersister($documentName) |
|
| 325 | { |
||
| 326 | 687 | if ( ! isset($this->persisters[$documentName])) { |
|
| 327 | 673 | $class = $this->dm->getClassMetadata($documentName); |
|
| 328 | 673 | $pb = $this->getPersistenceBuilder(); |
|
| 329 | 673 | $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this->evm, $this, $this->hydratorFactory, $class); |
|
| 330 | 673 | } |
|
| 331 | 687 | return $this->persisters[$documentName]; |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get the collection persister instance. |
||
| 336 | * |
||
| 337 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 338 | */ |
||
| 339 | 687 | public function getCollectionPersister() |
|
| 340 | { |
||
| 341 | 687 | if ( ! isset($this->collectionPersister)) { |
|
| 342 | 687 | $pb = $this->getPersistenceBuilder(); |
|
| 343 | 687 | $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this); |
|
| 344 | 687 | } |
|
| 345 | 687 | return $this->collectionPersister; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the document persister instance to use for the given document name |
||
| 350 | * |
||
| 351 | * @param string $documentName |
||
| 352 | * @param Persisters\DocumentPersister $persister |
||
| 353 | */ |
||
| 354 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 361 | * up to this point. The state of all managed documents will be synchronized with |
||
| 362 | * the database. |
||
| 363 | * |
||
| 364 | * The operations are executed in the following order: |
||
| 365 | * |
||
| 366 | * 1) All document insertions |
||
| 367 | * 2) All document updates |
||
| 368 | * 3) All document deletions |
||
| 369 | * |
||
| 370 | * @param object $document |
||
| 371 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 372 | */ |
||
| 373 | 571 | public function commit($document = null, array $options = array()) |
|
| 374 | { |
||
| 375 | // Raise preFlush |
||
| 376 | 571 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 377 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 378 | } |
||
| 379 | |||
| 380 | 571 | $defaultOptions = $this->dm->getConfiguration()->getDefaultCommitOptions(); |
|
| 381 | 571 | if ($options) { |
|
|
|
|||
| 382 | $options = array_merge($defaultOptions, $options); |
||
| 383 | } else { |
||
| 384 | 571 | $options = $defaultOptions; |
|
| 385 | } |
||
| 386 | // Compute changes done since last commit. |
||
| 387 | 571 | if ($document === null) { |
|
| 388 | 565 | $this->computeChangeSets(); |
|
| 389 | 570 | } elseif (is_object($document)) { |
|
| 390 | 12 | $this->computeSingleDocumentChangeSet($document); |
|
| 391 | 12 | } elseif (is_array($document)) { |
|
| 392 | 1 | foreach ($document as $object) { |
|
| 393 | 1 | $this->computeSingleDocumentChangeSet($object); |
|
| 394 | 1 | } |
|
| 395 | 1 | } |
|
| 396 | |||
| 397 | 569 | if ( ! ($this->documentInsertions || |
|
| 398 | 243 | $this->documentUpserts || |
|
| 399 | 204 | $this->documentDeletions || |
|
| 400 | 194 | $this->documentUpdates || |
|
| 401 | 24 | $this->collectionUpdates || |
|
| 402 | 25 | $this->collectionDeletions || |
|
| 403 | 24 | $this->orphanRemovals) |
|
| 404 | 569 | ) { |
|
| 405 | 24 | return; // Nothing to do. |
|
| 406 | } |
||
| 407 | |||
| 408 | 566 | if ($this->orphanRemovals) { |
|
| 409 | 47 | foreach ($this->orphanRemovals as $removal) { |
|
| 410 | 47 | $this->remove($removal); |
|
| 411 | 47 | } |
|
| 412 | 47 | } |
|
| 413 | |||
| 414 | // Raise onFlush |
||
| 415 | 566 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 416 | 7 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 417 | 7 | } |
|
| 418 | |||
| 419 | 566 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 420 | 80 | list($class, $documents) = $classAndDocuments; |
|
| 421 | 80 | $this->executeUpserts($class, $documents, $options); |
|
| 422 | 566 | } |
|
| 423 | |||
| 424 | 566 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 425 | 497 | list($class, $documents) = $classAndDocuments; |
|
| 426 | 497 | $this->executeInserts($class, $documents, $options); |
|
| 427 | 565 | } |
|
| 428 | |||
| 429 | 565 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 430 | 221 | list($class, $documents) = $classAndDocuments; |
|
| 431 | 221 | $this->executeUpdates($class, $documents, $options); |
|
| 432 | 565 | } |
|
| 433 | |||
| 434 | 565 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 435 | 64 | list($class, $documents) = $classAndDocuments; |
|
| 436 | 64 | $this->executeDeletions($class, $documents, $options); |
|
| 437 | 565 | } |
|
| 438 | |||
| 439 | // Raise postFlush |
||
| 440 | 565 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 441 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 442 | } |
||
| 443 | |||
| 444 | // Clear up |
||
| 445 | 565 | $this->documentInsertions = |
|
| 446 | 565 | $this->documentUpserts = |
|
| 447 | 565 | $this->documentUpdates = |
|
| 448 | 565 | $this->documentDeletions = |
|
| 449 | 565 | $this->documentChangeSets = |
|
| 450 | 565 | $this->collectionUpdates = |
|
| 451 | 565 | $this->collectionDeletions = |
|
| 452 | 565 | $this->visitedCollections = |
|
| 453 | 565 | $this->scheduledForDirtyCheck = |
|
| 454 | 565 | $this->orphanRemovals = |
|
| 455 | 565 | $this->hasScheduledCollections = array(); |
|
| 456 | 565 | } |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Groups a list of scheduled documents by their class. |
||
| 460 | * |
||
| 461 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 462 | * @param bool $includeEmbedded |
||
| 463 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 464 | */ |
||
| 465 | 566 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 466 | { |
||
| 467 | 566 | if (empty($documents)) { |
|
| 468 | 566 | return array(); |
|
| 469 | } |
||
| 470 | 565 | $divided = array(); |
|
| 471 | 565 | $embeds = array(); |
|
| 472 | 565 | foreach ($documents as $oid => $d) { |
|
| 473 | 565 | $className = get_class($d); |
|
| 474 | 565 | if (isset($embeds[$className])) { |
|
| 475 | 69 | continue; |
|
| 476 | } |
||
| 477 | 565 | if (isset($divided[$className])) { |
|
| 478 | 137 | $divided[$className][1][$oid] = $d; |
|
| 479 | 137 | continue; |
|
| 480 | } |
||
| 481 | 565 | $class = $this->dm->getClassMetadata($className); |
|
| 482 | 565 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 483 | 169 | $embeds[$className] = true; |
|
| 484 | 169 | continue; |
|
| 485 | } |
||
| 486 | 565 | if (empty($divided[$class->name])) { |
|
| 487 | 565 | $divided[$class->name] = array($class, array($oid => $d)); |
|
| 488 | 565 | } else { |
|
| 489 | 4 | $divided[$class->name][1][$oid] = $d; |
|
| 490 | } |
||
| 491 | 565 | } |
|
| 492 | 565 | return $divided; |
|
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Compute changesets of all documents scheduled for insertion. |
||
| 497 | * |
||
| 498 | * Embedded documents will not be processed. |
||
| 499 | */ |
||
| 500 | 573 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 501 | { |
||
| 502 | 573 | foreach ($this->documentInsertions as $document) { |
|
| 503 | 505 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 504 | 505 | if ( ! $class->isEmbeddedDocument) { |
|
| 505 | 502 | $this->computeChangeSet($class, $document); |
|
| 506 | 501 | } |
|
| 507 | 572 | } |
|
| 508 | 572 | } |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Compute changesets of all documents scheduled for upsert. |
||
| 512 | * |
||
| 513 | * Embedded documents will not be processed. |
||
| 514 | */ |
||
| 515 | 572 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 516 | { |
||
| 517 | 572 | foreach ($this->documentUpserts as $document) { |
|
| 518 | 79 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 519 | 79 | if ( ! $class->isEmbeddedDocument) { |
|
| 520 | 79 | $this->computeChangeSet($class, $document); |
|
| 521 | 79 | } |
|
| 522 | 572 | } |
|
| 523 | 572 | } |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 527 | * |
||
| 528 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 529 | * 2. Proxies are skipped. |
||
| 530 | * 3. Only if document is properly managed. |
||
| 531 | * |
||
| 532 | * @param object $document |
||
| 533 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 534 | * @return void |
||
| 535 | */ |
||
| 536 | 13 | private function computeSingleDocumentChangeSet($document) |
|
| 537 | { |
||
| 538 | 13 | $state = $this->getDocumentState($document); |
|
| 539 | |||
| 540 | 13 | if ($state !== self::STATE_MANAGED && $state !== self::STATE_REMOVED) { |
|
| 541 | 1 | throw new \InvalidArgumentException("Document has to be managed or scheduled for removal for single computation " . $this->objToStr($document)); |
|
| 542 | } |
||
| 543 | |||
| 544 | 12 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 545 | |||
| 546 | 12 | if ($state === self::STATE_MANAGED && $class->isChangeTrackingDeferredImplicit()) { |
|
| 547 | 9 | $this->persist($document); |
|
| 548 | 9 | } |
|
| 549 | |||
| 550 | // Compute changes for INSERTed and UPSERTed documents first. This must always happen even in this case. |
||
| 551 | 12 | $this->computeScheduleInsertsChangeSets(); |
|
| 552 | 12 | $this->computeScheduleUpsertsChangeSets(); |
|
| 553 | |||
| 554 | // Ignore uninitialized proxy objects |
||
| 555 | 12 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 556 | return; |
||
| 557 | } |
||
| 558 | |||
| 559 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 560 | 12 | $oid = spl_object_hash($document); |
|
| 561 | |||
| 562 | 12 | View Code Duplication | if ( ! isset($this->documentInsertions[$oid]) |
| 563 | 12 | && ! isset($this->documentUpserts[$oid]) |
|
| 564 | 12 | && ! isset($this->documentDeletions[$oid]) |
|
| 565 | 12 | && isset($this->documentStates[$oid]) |
|
| 566 | 12 | ) { |
|
| 567 | 8 | $this->computeChangeSet($class, $document); |
|
| 568 | 8 | } |
|
| 569 | 12 | } |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Gets the changeset for a document. |
||
| 573 | * |
||
| 574 | * @param object $document |
||
| 575 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 576 | */ |
||
| 577 | 563 | public function getDocumentChangeSet($document) |
|
| 578 | { |
||
| 579 | 563 | $oid = spl_object_hash($document); |
|
| 580 | 563 | if (isset($this->documentChangeSets[$oid])) { |
|
| 581 | 563 | return $this->documentChangeSets[$oid]; |
|
| 582 | } |
||
| 583 | 54 | return array(); |
|
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * INTERNAL: |
||
| 588 | * Sets the changeset for a document. |
||
| 589 | * |
||
| 590 | * @param object $document |
||
| 591 | * @param array $changeset |
||
| 592 | */ |
||
| 593 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 600 | * |
||
| 601 | * @param object $document |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | 570 | public function getDocumentActualData($document) |
|
| 605 | { |
||
| 606 | 570 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 607 | 570 | $actualData = array(); |
|
| 608 | 570 | foreach ($class->reflFields as $name => $refProp) { |
|
| 609 | 570 | $mapping = $class->fieldMappings[$name]; |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Computes the changes that happened to a single document. |
||
| 641 | * |
||
| 642 | * Modifies/populates the following properties: |
||
| 643 | * |
||
| 644 | * {@link originalDocumentData} |
||
| 645 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 646 | * then it was not fetched from the database and therefore we have no original |
||
| 647 | * document data yet. All of the current document data is stored as the original document data. |
||
| 648 | * |
||
| 649 | * {@link documentChangeSets} |
||
| 650 | * The changes detected on all properties of the document are stored there. |
||
| 651 | * A change is a tuple array where the first entry is the old value and the second |
||
| 652 | * entry is the new value of the property. Changesets are used by persisters |
||
| 653 | * to INSERT/UPDATE the persistent document state. |
||
| 654 | * |
||
| 655 | * {@link documentUpdates} |
||
| 656 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 657 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 658 | * there to mark it for an update. |
||
| 659 | * |
||
| 660 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 661 | * @param object $document The document for which to compute the changes. |
||
| 662 | */ |
||
| 663 | 570 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 679 | * |
||
| 680 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 681 | * @param object $document |
||
| 682 | * @param boolean $recompute |
||
| 683 | */ |
||
| 684 | 570 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Computes all the changes that have been done to documents and collections |
||
| 855 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 856 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 857 | */ |
||
| 858 | 568 | public function computeChangeSets() |
|
| 908 | |||
| 909 | /** |
||
| 910 | * Computes the changes of an association. |
||
| 911 | * |
||
| 912 | * @param object $parentDocument |
||
| 913 | * @param array $assoc |
||
| 914 | * @param mixed $value The value of the association. |
||
| 915 | * @throws \InvalidArgumentException |
||
| 916 | */ |
||
| 917 | 430 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1018 | |||
| 1019 | /** |
||
| 1020 | * INTERNAL: |
||
| 1021 | * Computes the changeset of an individual document, independently of the |
||
| 1022 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1023 | * |
||
| 1024 | * The passed document must be a managed document. If the document already has a change set |
||
| 1025 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1026 | * whereby changes detected in this method prevail. |
||
| 1027 | * |
||
| 1028 | * @ignore |
||
| 1029 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1030 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1031 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1032 | */ |
||
| 1033 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1034 | { |
||
| 1035 | // Ignore uninitialized proxy objects |
||
| 1036 | 20 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 1037 | 1 | return; |
|
| 1038 | } |
||
| 1039 | |||
| 1040 | 19 | $oid = spl_object_hash($document); |
|
| 1041 | |||
| 1042 | 19 | if ( ! isset($this->documentStates[$oid]) || $this->documentStates[$oid] != self::STATE_MANAGED) { |
|
| 1043 | throw new \InvalidArgumentException('Document must be managed.'); |
||
| 1044 | } |
||
| 1045 | |||
| 1046 | 19 | if ( ! $class->isInheritanceTypeNone()) { |
|
| 1047 | 2 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1048 | 2 | } |
|
| 1049 | |||
| 1050 | 19 | $this->computeOrRecomputeChangeSet($class, $document, true); |
|
| 1051 | 19 | } |
|
| 1052 | |||
| 1053 | /** |
||
| 1054 | * @param ClassMetadata $class |
||
| 1055 | * @param object $document |
||
| 1056 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1057 | */ |
||
| 1058 | 594 | private function persistNew(ClassMetadata $class, $document) |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Executes all document insertions for documents of the specified type. |
||
| 1104 | * |
||
| 1105 | * @param ClassMetadata $class |
||
| 1106 | * @param array $documents Array of documents to insert |
||
| 1107 | * @param array $options Array of options to be used with batchInsert() |
||
| 1108 | */ |
||
| 1109 | 497 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Executes all document upserts for documents of the specified type. |
||
| 1127 | * |
||
| 1128 | * @param ClassMetadata $class |
||
| 1129 | * @param array $documents Array of documents to upsert |
||
| 1130 | * @param array $options Array of options to be used with batchInsert() |
||
| 1131 | */ |
||
| 1132 | 80 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Executes all document updates for documents of the specified type. |
||
| 1151 | * |
||
| 1152 | * @param Mapping\ClassMetadata $class |
||
| 1153 | * @param array $documents Array of documents to update |
||
| 1154 | * @param array $options Array of options to be used with update() |
||
| 1155 | */ |
||
| 1156 | 221 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Executes all document deletions for documents of the specified type. |
||
| 1176 | * |
||
| 1177 | * @param ClassMetadata $class |
||
| 1178 | * @param array $documents Array of documents to delete |
||
| 1179 | * @param array $options Array of options to be used with remove() |
||
| 1180 | */ |
||
| 1181 | 64 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Schedules a document for insertion into the database. |
||
| 1216 | * If the document already has an identifier, it will be added to the |
||
| 1217 | * identity map. |
||
| 1218 | * |
||
| 1219 | * @param ClassMetadata $class |
||
| 1220 | * @param object $document The document to schedule for insertion. |
||
| 1221 | * @throws \InvalidArgumentException |
||
| 1222 | */ |
||
| 1223 | 527 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Schedules a document for upsert into the database and adds it to the |
||
| 1246 | * identity map |
||
| 1247 | * |
||
| 1248 | * @param ClassMetadata $class |
||
| 1249 | * @param object $document The document to schedule for upsert. |
||
| 1250 | * @throws \InvalidArgumentException |
||
| 1251 | */ |
||
| 1252 | 86 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Checks whether a document is scheduled for insertion. |
||
| 1276 | * |
||
| 1277 | * @param object $document |
||
| 1278 | * @return boolean |
||
| 1279 | */ |
||
| 1280 | 101 | public function isScheduledForInsert($document) |
|
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Checks whether a document is scheduled for upsert. |
||
| 1287 | * |
||
| 1288 | * @param object $document |
||
| 1289 | * @return boolean |
||
| 1290 | */ |
||
| 1291 | 5 | public function isScheduledForUpsert($document) |
|
| 1295 | |||
| 1296 | /** |
||
| 1297 | * Schedules a document for being updated. |
||
| 1298 | * |
||
| 1299 | * @param object $document The document to schedule for being updated. |
||
| 1300 | * @throws \InvalidArgumentException |
||
| 1301 | */ |
||
| 1302 | 230 | public function scheduleForUpdate($document) |
|
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1322 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1323 | * at commit time. |
||
| 1324 | * |
||
| 1325 | * @param object $document |
||
| 1326 | * @return boolean |
||
| 1327 | */ |
||
| 1328 | 13 | public function isScheduledForUpdate($document) |
|
| 1332 | |||
| 1333 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1338 | |||
| 1339 | /** |
||
| 1340 | * INTERNAL: |
||
| 1341 | * Schedules a document for deletion. |
||
| 1342 | * |
||
| 1343 | * @param object $document |
||
| 1344 | */ |
||
| 1345 | 69 | public function scheduleForDelete($document) |
|
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1374 | * of work. |
||
| 1375 | * |
||
| 1376 | * @param object $document |
||
| 1377 | * @return boolean |
||
| 1378 | */ |
||
| 1379 | 8 | public function isScheduledForDelete($document) |
|
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1386 | * |
||
| 1387 | * @param $document |
||
| 1388 | * @return boolean |
||
| 1389 | */ |
||
| 1390 | 230 | public function isDocumentScheduled($document) |
|
| 1398 | |||
| 1399 | /** |
||
| 1400 | * INTERNAL: |
||
| 1401 | * Registers a document in the identity map. |
||
| 1402 | * |
||
| 1403 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1404 | * the root document. Identifiers are serialized before being used as array |
||
| 1405 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1406 | * |
||
| 1407 | * @ignore |
||
| 1408 | * @param object $document The document to register. |
||
| 1409 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1410 | * the document in question is already managed. |
||
| 1411 | */ |
||
| 1412 | 623 | public function addToIdentityMap($document) |
|
| 1435 | |||
| 1436 | /** |
||
| 1437 | * Gets the state of a document with regard to the current unit of work. |
||
| 1438 | * |
||
| 1439 | * @param object $document |
||
| 1440 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1441 | * This parameter can be set to improve performance of document state detection |
||
| 1442 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1443 | * is either known or does not matter for the caller of the method. |
||
| 1444 | * @return int The document state. |
||
| 1445 | */ |
||
| 1446 | 597 | public function getDocumentState($document, $assume = null) |
|
| 1496 | |||
| 1497 | /** |
||
| 1498 | * INTERNAL: |
||
| 1499 | * Removes a document from the identity map. This effectively detaches the |
||
| 1500 | * document from the persistence management of Doctrine. |
||
| 1501 | * |
||
| 1502 | * @ignore |
||
| 1503 | * @param object $document |
||
| 1504 | * @throws \InvalidArgumentException |
||
| 1505 | * @return boolean |
||
| 1506 | */ |
||
| 1507 | 78 | public function removeFromIdentityMap($document) |
|
| 1527 | |||
| 1528 | /** |
||
| 1529 | * INTERNAL: |
||
| 1530 | * Gets a document in the identity map by its identifier hash. |
||
| 1531 | * |
||
| 1532 | * @ignore |
||
| 1533 | * @param mixed $id Document identifier |
||
| 1534 | * @param ClassMetadata $class Document class |
||
| 1535 | * @return object |
||
| 1536 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1537 | */ |
||
| 1538 | 32 | public function getById($id, ClassMetadata $class) |
|
| 1548 | |||
| 1549 | /** |
||
| 1550 | * INTERNAL: |
||
| 1551 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1552 | * for the given hash, FALSE is returned. |
||
| 1553 | * |
||
| 1554 | * @ignore |
||
| 1555 | * @param mixed $id Document identifier |
||
| 1556 | * @param ClassMetadata $class Document class |
||
| 1557 | * @return mixed The found document or FALSE. |
||
| 1558 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1559 | */ |
||
| 1560 | 295 | public function tryGetById($id, ClassMetadata $class) |
|
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Schedules a document for dirty-checking at commit-time. |
||
| 1574 | * |
||
| 1575 | * @param object $document The document to schedule for dirty-checking. |
||
| 1576 | * @todo Rename: scheduleForSynchronization |
||
| 1577 | */ |
||
| 1578 | 2 | public function scheduleForDirtyCheck($document) |
|
| 1583 | |||
| 1584 | /** |
||
| 1585 | * Checks whether a document is registered in the identity map. |
||
| 1586 | * |
||
| 1587 | * @param object $document |
||
| 1588 | * @return boolean |
||
| 1589 | */ |
||
| 1590 | 78 | public function isInIdentityMap($document) |
|
| 1603 | |||
| 1604 | /** |
||
| 1605 | * @param object $document |
||
| 1606 | * @return string |
||
| 1607 | */ |
||
| 1608 | 623 | private function getIdForIdentityMap($document) |
|
| 1621 | |||
| 1622 | /** |
||
| 1623 | * INTERNAL: |
||
| 1624 | * Checks whether an identifier exists in the identity map. |
||
| 1625 | * |
||
| 1626 | * @ignore |
||
| 1627 | * @param string $id |
||
| 1628 | * @param string $rootClassName |
||
| 1629 | * @return boolean |
||
| 1630 | */ |
||
| 1631 | public function containsId($id, $rootClassName) |
||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Persists a document as part of the current unit of work. |
||
| 1638 | * |
||
| 1639 | * @param object $document The document to persist. |
||
| 1640 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1641 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1642 | */ |
||
| 1643 | 592 | public function persist($document) |
|
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Saves a document as part of the current unit of work. |
||
| 1655 | * This method is internally called during save() cascades as it tracks |
||
| 1656 | * the already visited documents to prevent infinite recursions. |
||
| 1657 | * |
||
| 1658 | * NOTE: This method always considers documents that are not yet known to |
||
| 1659 | * this UnitOfWork as NEW. |
||
| 1660 | * |
||
| 1661 | * @param object $document The document to persist. |
||
| 1662 | * @param array $visited The already visited documents. |
||
| 1663 | * @throws \InvalidArgumentException |
||
| 1664 | * @throws MongoDBException |
||
| 1665 | */ |
||
| 1666 | 591 | private function doPersist($document, array &$visited) |
|
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Deletes a document as part of the current unit of work. |
||
| 1709 | * |
||
| 1710 | * @param object $document The document to remove. |
||
| 1711 | */ |
||
| 1712 | 68 | public function remove($document) |
|
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Deletes a document as part of the current unit of work. |
||
| 1720 | * |
||
| 1721 | * This method is internally called during delete() cascades as it tracks |
||
| 1722 | * the already visited documents to prevent infinite recursions. |
||
| 1723 | * |
||
| 1724 | * @param object $document The document to delete. |
||
| 1725 | * @param array $visited The map of the already visited documents. |
||
| 1726 | * @throws MongoDBException |
||
| 1727 | */ |
||
| 1728 | 68 | private function doRemove($document, array &$visited) |
|
| 1760 | |||
| 1761 | /** |
||
| 1762 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1763 | * |
||
| 1764 | * @param object $document |
||
| 1765 | * @return object The managed copy of the document. |
||
| 1766 | */ |
||
| 1767 | 13 | public function merge($document) |
|
| 1773 | |||
| 1774 | /** |
||
| 1775 | * Executes a merge operation on a document. |
||
| 1776 | * |
||
| 1777 | * @param object $document |
||
| 1778 | * @param array $visited |
||
| 1779 | * @param object|null $prevManagedCopy |
||
| 1780 | * @param array|null $assoc |
||
| 1781 | * |
||
| 1782 | * @return object The managed copy of the document. |
||
| 1783 | * |
||
| 1784 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1785 | * @throws LockException If the document uses optimistic locking through a |
||
| 1786 | * version attribute and the version check against the |
||
| 1787 | * managed copy fails. |
||
| 1788 | */ |
||
| 1789 | 13 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Detaches a document from the persistence management. It's persistence will |
||
| 1970 | * no longer be managed by Doctrine. |
||
| 1971 | * |
||
| 1972 | * @param object $document The document to detach. |
||
| 1973 | */ |
||
| 1974 | 9 | public function detach($document) |
|
| 1979 | |||
| 1980 | /** |
||
| 1981 | * Executes a detach operation on the given document. |
||
| 1982 | * |
||
| 1983 | * @param object $document |
||
| 1984 | * @param array $visited |
||
| 1985 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1986 | */ |
||
| 1987 | 12 | private function doDetach($document, array &$visited) |
|
| 2012 | |||
| 2013 | /** |
||
| 2014 | * Refreshes the state of the given document from the database, overwriting |
||
| 2015 | * any local, unpersisted changes. |
||
| 2016 | * |
||
| 2017 | * @param object $document The document to refresh. |
||
| 2018 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2019 | */ |
||
| 2020 | 21 | public function refresh($document) |
|
| 2025 | |||
| 2026 | /** |
||
| 2027 | * Executes a refresh operation on a document. |
||
| 2028 | * |
||
| 2029 | * @param object $document The document to refresh. |
||
| 2030 | * @param array $visited The already visited documents during cascades. |
||
| 2031 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2032 | */ |
||
| 2033 | 21 | private function doRefresh($document, array &$visited) |
|
| 2055 | |||
| 2056 | /** |
||
| 2057 | * Cascades a refresh operation to associated documents. |
||
| 2058 | * |
||
| 2059 | * @param object $document |
||
| 2060 | * @param array $visited |
||
| 2061 | */ |
||
| 2062 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2086 | |||
| 2087 | /** |
||
| 2088 | * Cascades a detach operation to associated documents. |
||
| 2089 | * |
||
| 2090 | * @param object $document |
||
| 2091 | * @param array $visited |
||
| 2092 | */ |
||
| 2093 | 12 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2114 | /** |
||
| 2115 | * Cascades a merge operation to associated documents. |
||
| 2116 | * |
||
| 2117 | * @param object $document |
||
| 2118 | * @param object $managedCopy |
||
| 2119 | * @param array $visited |
||
| 2120 | */ |
||
| 2121 | 13 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2152 | |||
| 2153 | /** |
||
| 2154 | * Cascades the save operation to associated documents. |
||
| 2155 | * |
||
| 2156 | * @param object $document |
||
| 2157 | * @param array $visited |
||
| 2158 | */ |
||
| 2159 | 589 | private function cascadePersist($document, array &$visited) |
|
| 2206 | |||
| 2207 | /** |
||
| 2208 | * Cascades the delete operation to associated documents. |
||
| 2209 | * |
||
| 2210 | * @param object $document |
||
| 2211 | * @param array $visited |
||
| 2212 | */ |
||
| 2213 | 68 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2235 | |||
| 2236 | /** |
||
| 2237 | * Acquire a lock on the given document. |
||
| 2238 | * |
||
| 2239 | * @param object $document |
||
| 2240 | * @param int $lockMode |
||
| 2241 | * @param int $lockVersion |
||
| 2242 | * @throws LockException |
||
| 2243 | * @throws \InvalidArgumentException |
||
| 2244 | */ |
||
| 2245 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2269 | |||
| 2270 | /** |
||
| 2271 | * Releases a lock on the given document. |
||
| 2272 | * |
||
| 2273 | * @param object $document |
||
| 2274 | * @throws \InvalidArgumentException |
||
| 2275 | */ |
||
| 2276 | 1 | public function unlock($document) |
|
| 2284 | |||
| 2285 | /** |
||
| 2286 | * Clears the UnitOfWork. |
||
| 2287 | * |
||
| 2288 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2289 | */ |
||
| 2290 | 394 | public function clear($documentName = null) |
|
| 2323 | |||
| 2324 | /** |
||
| 2325 | * INTERNAL: |
||
| 2326 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2327 | * invoked on that document at the beginning of the next commit of this |
||
| 2328 | * UnitOfWork. |
||
| 2329 | * |
||
| 2330 | * @ignore |
||
| 2331 | * @param object $document |
||
| 2332 | */ |
||
| 2333 | 49 | public function scheduleOrphanRemoval($document) |
|
| 2337 | |||
| 2338 | /** |
||
| 2339 | * INTERNAL: |
||
| 2340 | * Unschedules an embedded or referenced object for removal. |
||
| 2341 | * |
||
| 2342 | * @ignore |
||
| 2343 | * @param object $document |
||
| 2344 | */ |
||
| 2345 | 104 | public function unscheduleOrphanRemoval($document) |
|
| 2352 | |||
| 2353 | /** |
||
| 2354 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2355 | * 1) sets owner if it was cloned |
||
| 2356 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2357 | * 3) NOP if state is OK |
||
| 2358 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2359 | * |
||
| 2360 | * @param PersistentCollection $coll |
||
| 2361 | * @param object $document |
||
| 2362 | * @param ClassMetadata $class |
||
| 2363 | * @param string $propName |
||
| 2364 | * @return PersistentCollection |
||
| 2365 | */ |
||
| 2366 | 8 | private function fixPersistentCollectionOwnership(PersistentCollection $coll, $document, ClassMetadata $class, $propName) |
|
| 2386 | |||
| 2387 | /** |
||
| 2388 | * INTERNAL: |
||
| 2389 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2390 | * |
||
| 2391 | * @param PersistentCollection $coll |
||
| 2392 | */ |
||
| 2393 | 42 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
|
| 2402 | |||
| 2403 | /** |
||
| 2404 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2405 | * |
||
| 2406 | * @param PersistentCollection $coll |
||
| 2407 | * @return boolean |
||
| 2408 | */ |
||
| 2409 | 208 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
|
| 2413 | |||
| 2414 | /** |
||
| 2415 | * INTERNAL: |
||
| 2416 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2417 | * |
||
| 2418 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2419 | */ |
||
| 2420 | 211 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollection $coll) |
| 2429 | |||
| 2430 | /** |
||
| 2431 | * INTERNAL: |
||
| 2432 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2433 | * |
||
| 2434 | * @param PersistentCollection $coll |
||
| 2435 | */ |
||
| 2436 | 227 | public function scheduleCollectionUpdate(PersistentCollection $coll) |
|
| 2451 | |||
| 2452 | /** |
||
| 2453 | * INTERNAL: |
||
| 2454 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2455 | * |
||
| 2456 | * @param \Doctrine\ODM\MongoDB\PersistentCollection $coll |
||
| 2457 | */ |
||
| 2458 | 211 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollection $coll) |
| 2467 | |||
| 2468 | /** |
||
| 2469 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2470 | * |
||
| 2471 | * @param PersistentCollection $coll |
||
| 2472 | * @return boolean |
||
| 2473 | */ |
||
| 2474 | 124 | public function isCollectionScheduledForUpdate(PersistentCollection $coll) |
|
| 2478 | |||
| 2479 | /** |
||
| 2480 | * INTERNAL: |
||
| 2481 | * Gets PersistentCollections that have been visited during computing change |
||
| 2482 | * set of $document |
||
| 2483 | * |
||
| 2484 | * @param object $document |
||
| 2485 | * @return PersistentCollection[] |
||
| 2486 | */ |
||
| 2487 | 551 | public function getVisitedCollections($document) |
|
| 2494 | |||
| 2495 | /** |
||
| 2496 | * INTERNAL: |
||
| 2497 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2498 | * |
||
| 2499 | * @param object $document |
||
| 2500 | * @return array |
||
| 2501 | */ |
||
| 2502 | 551 | public function getScheduledCollections($document) |
|
| 2509 | |||
| 2510 | /** |
||
| 2511 | * Checks whether the document is related to a PersistentCollection |
||
| 2512 | * scheduled for update or deletion. |
||
| 2513 | * |
||
| 2514 | * @param object $document |
||
| 2515 | * @return boolean |
||
| 2516 | */ |
||
| 2517 | 49 | public function hasScheduledCollections($document) |
|
| 2521 | |||
| 2522 | /** |
||
| 2523 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2524 | * a collection scheduled for update or deletion. |
||
| 2525 | * |
||
| 2526 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2527 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2528 | * |
||
| 2529 | * If the collection is nested within atomic collection, it is immediately |
||
| 2530 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2531 | * calculating update data way easier. |
||
| 2532 | * |
||
| 2533 | * @param PersistentCollection $coll |
||
| 2534 | */ |
||
| 2535 | 229 | private function scheduleCollectionOwner(PersistentCollection $coll) |
|
| 2558 | |||
| 2559 | /** |
||
| 2560 | * Get the top-most owning document of a given document |
||
| 2561 | * |
||
| 2562 | * If a top-level document is provided, that same document will be returned. |
||
| 2563 | * For an embedded document, we will walk through parent associations until |
||
| 2564 | * we find a top-level document. |
||
| 2565 | * |
||
| 2566 | * @param object $document |
||
| 2567 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2568 | * @return object |
||
| 2569 | */ |
||
| 2570 | 231 | public function getOwningDocument($document) |
|
| 2586 | |||
| 2587 | /** |
||
| 2588 | * Gets the class name for an association (embed or reference) with respect |
||
| 2589 | * to any discriminator value. |
||
| 2590 | * |
||
| 2591 | * @param array $mapping Field mapping for the association |
||
| 2592 | * @param array|null $data Data for the embedded document or reference |
||
| 2593 | */ |
||
| 2594 | 209 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2627 | |||
| 2628 | /** |
||
| 2629 | * INTERNAL: |
||
| 2630 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2631 | * |
||
| 2632 | * @ignore |
||
| 2633 | * @param string $className The name of the document class. |
||
| 2634 | * @param array $data The data for the document. |
||
| 2635 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2636 | * @param object The document to be hydrated into in case of creation |
||
| 2637 | * @return object The document instance. |
||
| 2638 | * @internal Highly performance-sensitive method. |
||
| 2639 | */ |
||
| 2640 | 395 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2694 | |||
| 2695 | /** |
||
| 2696 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2697 | * |
||
| 2698 | * @param PersistentCollection $collection The collection to initialize. |
||
| 2699 | */ |
||
| 2700 | 158 | public function loadCollection(PersistentCollection $collection) |
|
| 2704 | |||
| 2705 | /** |
||
| 2706 | * Gets the identity map of the UnitOfWork. |
||
| 2707 | * |
||
| 2708 | * @return array |
||
| 2709 | */ |
||
| 2710 | public function getIdentityMap() |
||
| 2714 | |||
| 2715 | /** |
||
| 2716 | * Gets the original data of a document. The original data is the data that was |
||
| 2717 | * present at the time the document was reconstituted from the database. |
||
| 2718 | * |
||
| 2719 | * @param object $document |
||
| 2720 | * @return array |
||
| 2721 | */ |
||
| 2722 | 1 | public function getOriginalDocumentData($document) |
|
| 2730 | |||
| 2731 | /** |
||
| 2732 | * @ignore |
||
| 2733 | */ |
||
| 2734 | 52 | public function setOriginalDocumentData($document, array $data) |
|
| 2740 | |||
| 2741 | /** |
||
| 2742 | * INTERNAL: |
||
| 2743 | * Sets a property value of the original data array of a document. |
||
| 2744 | * |
||
| 2745 | * @ignore |
||
| 2746 | * @param string $oid |
||
| 2747 | * @param string $property |
||
| 2748 | * @param mixed $value |
||
| 2749 | */ |
||
| 2750 | 3 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2754 | |||
| 2755 | /** |
||
| 2756 | * Gets the identifier of a document. |
||
| 2757 | * |
||
| 2758 | * @param object $document |
||
| 2759 | * @return mixed The identifier value |
||
| 2760 | */ |
||
| 2761 | 367 | public function getDocumentIdentifier($document) |
|
| 2766 | |||
| 2767 | /** |
||
| 2768 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2769 | * |
||
| 2770 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2771 | */ |
||
| 2772 | public function hasPendingInsertions() |
||
| 2776 | |||
| 2777 | /** |
||
| 2778 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2779 | * number of documents in the identity map. |
||
| 2780 | * |
||
| 2781 | * @return integer |
||
| 2782 | */ |
||
| 2783 | 2 | public function size() |
|
| 2791 | |||
| 2792 | /** |
||
| 2793 | * INTERNAL: |
||
| 2794 | * Registers a document as managed. |
||
| 2795 | * |
||
| 2796 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2797 | * document class. If the class expects its database identifier to be a |
||
| 2798 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2799 | * document identifiers map will become inconsistent with the identity map. |
||
| 2800 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2801 | * conversion and throw an exception if it's inconsistent. |
||
| 2802 | * |
||
| 2803 | * @param object $document The document. |
||
| 2804 | * @param array $id The identifier values. |
||
| 2805 | * @param array $data The original document data. |
||
| 2806 | */ |
||
| 2807 | 383 | public function registerManaged($document, $id, array $data) |
|
| 2822 | |||
| 2823 | /** |
||
| 2824 | * INTERNAL: |
||
| 2825 | * Clears the property changeset of the document with the given OID. |
||
| 2826 | * |
||
| 2827 | * @param string $oid The document's OID. |
||
| 2828 | */ |
||
| 2829 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2833 | |||
| 2834 | /* PropertyChangedListener implementation */ |
||
| 2835 | |||
| 2836 | /** |
||
| 2837 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2838 | * |
||
| 2839 | * @param object $document The document that owns the property. |
||
| 2840 | * @param string $propertyName The name of the property that changed. |
||
| 2841 | * @param mixed $oldValue The old value of the property. |
||
| 2842 | * @param mixed $newValue The new value of the property. |
||
| 2843 | */ |
||
| 2844 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2859 | |||
| 2860 | /** |
||
| 2861 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2862 | * |
||
| 2863 | * @return array |
||
| 2864 | */ |
||
| 2865 | 5 | public function getScheduledDocumentInsertions() |
|
| 2869 | |||
| 2870 | /** |
||
| 2871 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2872 | * |
||
| 2873 | * @return array |
||
| 2874 | */ |
||
| 2875 | 3 | public function getScheduledDocumentUpserts() |
|
| 2879 | |||
| 2880 | /** |
||
| 2881 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2882 | * |
||
| 2883 | * @return array |
||
| 2884 | */ |
||
| 2885 | 3 | public function getScheduledDocumentUpdates() |
|
| 2889 | |||
| 2890 | /** |
||
| 2891 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2892 | * |
||
| 2893 | * @return array |
||
| 2894 | */ |
||
| 2895 | public function getScheduledDocumentDeletions() |
||
| 2899 | |||
| 2900 | /** |
||
| 2901 | * Get the currently scheduled complete collection deletions |
||
| 2902 | * |
||
| 2903 | * @return array |
||
| 2904 | */ |
||
| 2905 | public function getScheduledCollectionDeletions() |
||
| 2909 | |||
| 2910 | /** |
||
| 2911 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2912 | * |
||
| 2913 | * @return array |
||
| 2914 | */ |
||
| 2915 | public function getScheduledCollectionUpdates() |
||
| 2919 | |||
| 2920 | /** |
||
| 2921 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2922 | * |
||
| 2923 | * @param object |
||
| 2924 | * @return void |
||
| 2925 | */ |
||
| 2926 | public function initializeObject($obj) |
||
| 2934 | |||
| 2935 | 1 | private function objToStr($obj) |
|
| 2939 | } |
||
| 2940 |
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.