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 |
||
| 44 | class UnitOfWork implements PropertyChangedListener |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 48 | */ |
||
| 49 | public const STATE_MANAGED = 1; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 53 | * and is not (yet) managed by a DocumentManager. |
||
| 54 | */ |
||
| 55 | public const STATE_NEW = 2; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A detached document is an instance with a persistent identity that is not |
||
| 59 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 60 | */ |
||
| 61 | public const STATE_DETACHED = 3; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * A removed document instance is an instance with a persistent identity, |
||
| 65 | * associated with a DocumentManager, whose persistent state has been |
||
| 66 | * deleted (or is scheduled for deletion). |
||
| 67 | */ |
||
| 68 | public const STATE_REMOVED = 4; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The identity map holds references to all managed documents. |
||
| 72 | * |
||
| 73 | * Documents are grouped by their class name, and then indexed by the |
||
| 74 | * serialized string of their database identifier field or, if the class |
||
| 75 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 76 | * differentiation of values that may be equal (via type juggling) but not |
||
| 77 | * identical. |
||
| 78 | * |
||
| 79 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 80 | * we always take the root class name of the hierarchy. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $identityMap = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Map of all identifiers of managed documents. |
||
| 88 | * Keys are object ids (spl_object_hash). |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $documentIdentifiers = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Map of the original document data of managed documents. |
||
| 96 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 97 | * at commit time. |
||
| 98 | * |
||
| 99 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 100 | * A value will only really be copied if the value in the document is modified |
||
| 101 | * by the user. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private $originalDocumentData = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 109 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | private $documentChangeSets = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The (cached) states of any known documents. |
||
| 117 | * Keys are object ids (spl_object_hash). |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $documentStates = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 125 | * |
||
| 126 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 127 | * object hash. This is only used for documents with a change tracking |
||
| 128 | * policy of DEFERRED_EXPLICIT. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | * @todo rename: scheduledForSynchronization |
||
| 132 | */ |
||
| 133 | private $scheduledForDirtyCheck = []; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of all pending document insertions. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $documentInsertions = []; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A list of all pending document updates. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $documentUpdates = []; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A list of all pending document upserts. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $documentUpserts = []; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending document deletions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $documentDeletions = []; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * All pending collection deletions. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $collectionDeletions = []; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * All pending collection updates. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $collectionUpdates = []; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of documents related to collections scheduled for update or deletion |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $hasScheduledCollections = []; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 186 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 187 | * of their data. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $visitedCollections = []; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 195 | * |
||
| 196 | * @var DocumentManager |
||
| 197 | */ |
||
| 198 | private $dm; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The EventManager used for dispatching events. |
||
| 202 | * |
||
| 203 | * @var EventManager |
||
| 204 | */ |
||
| 205 | private $evm; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Additional documents that are scheduled for removal. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | private $orphanRemovals = []; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 216 | * |
||
| 217 | * @var HydratorFactory |
||
| 218 | */ |
||
| 219 | private $hydratorFactory; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The document persister instances used to persist document instances. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $persisters = []; |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The collection persister instance used to persist changes to collections. |
||
| 230 | * |
||
| 231 | * @var Persisters\CollectionPersister |
||
| 232 | */ |
||
| 233 | private $collectionPersister; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The persistence builder instance used in DocumentPersisters. |
||
| 237 | * |
||
| 238 | * @var PersistenceBuilder |
||
| 239 | */ |
||
| 240 | private $persistenceBuilder; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Array of parent associations between embedded documents. |
||
| 244 | * |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | private $parentAssociations = []; |
||
| 248 | |||
| 249 | /** @var LifecycleEventManager */ |
||
| 250 | private $lifecycleEventManager; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 254 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 255 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 256 | * |
||
| 257 | * @var array |
||
| 258 | */ |
||
| 259 | private $embeddedDocumentsRegistry = []; |
||
| 260 | |||
| 261 | /** @var int */ |
||
| 262 | private $commitsInProgress = 0; |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 266 | */ |
||
| 267 | 1634 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 268 | { |
||
| 269 | 1634 | $this->dm = $dm; |
|
| 270 | 1634 | $this->evm = $evm; |
|
| 271 | 1634 | $this->hydratorFactory = $hydratorFactory; |
|
| 272 | 1634 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 273 | 1634 | } |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 277 | * queries for insert persistence. |
||
| 278 | */ |
||
| 279 | 1122 | public function getPersistenceBuilder() : PersistenceBuilder |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Sets the parent association for a given embedded document. |
||
| 289 | */ |
||
| 290 | 202 | public function setParentAssociation(object $document, array $mapping, object $parent, string $propertyPath) : void |
|
| 291 | { |
||
| 292 | 202 | $oid = spl_object_hash($document); |
|
| 293 | 202 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 294 | 202 | $this->parentAssociations[$oid] = [$mapping, $parent, $propertyPath]; |
|
| 295 | 202 | } |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Gets the parent association for a given embedded document. |
||
| 299 | * |
||
| 300 | * <code> |
||
| 301 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 302 | * </code> |
||
| 303 | */ |
||
| 304 | 226 | public function getParentAssociation(object $document) : ?array |
|
| 305 | { |
||
| 306 | 226 | $oid = spl_object_hash($document); |
|
| 307 | |||
| 308 | 226 | return $this->parentAssociations[$oid] ?? null; |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the document persister instance for the given document name |
||
| 313 | */ |
||
| 314 | 1120 | public function getDocumentPersister(string $documentName) : Persisters\DocumentPersister |
|
| 315 | { |
||
| 316 | 1120 | if (! isset($this->persisters[$documentName])) { |
|
| 317 | 1107 | $class = $this->dm->getClassMetadata($documentName); |
|
| 318 | 1107 | $pb = $this->getPersistenceBuilder(); |
|
| 319 | 1107 | $this->persisters[$documentName] = new Persisters\DocumentPersister($pb, $this->dm, $this, $this->hydratorFactory, $class); |
|
|
|
|||
| 320 | } |
||
| 321 | 1120 | return $this->persisters[$documentName]; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the collection persister instance. |
||
| 326 | */ |
||
| 327 | 1120 | public function getCollectionPersister() : CollectionPersister |
|
| 328 | { |
||
| 329 | 1120 | if (! isset($this->collectionPersister)) { |
|
| 330 | 1120 | $pb = $this->getPersistenceBuilder(); |
|
| 331 | 1120 | $this->collectionPersister = new Persisters\CollectionPersister($this->dm, $pb, $this); |
|
| 332 | } |
||
| 333 | 1120 | return $this->collectionPersister; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Set the document persister instance to use for the given document name |
||
| 338 | */ |
||
| 339 | 13 | public function setDocumentPersister(string $documentName, Persisters\DocumentPersister $persister) : void |
|
| 340 | { |
||
| 341 | 13 | $this->persisters[$documentName] = $persister; |
|
| 342 | 13 | } |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 346 | * up to this point. The state of all managed documents will be synchronized with |
||
| 347 | * the database. |
||
| 348 | * |
||
| 349 | * The operations are executed in the following order: |
||
| 350 | * |
||
| 351 | * 1) All document insertions |
||
| 352 | * 2) All document updates |
||
| 353 | * 3) All document deletions |
||
| 354 | * |
||
| 355 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 356 | */ |
||
| 357 | 602 | public function commit(array $options = []) : void |
|
| 358 | { |
||
| 359 | // Raise preFlush |
||
| 360 | 602 | if ($this->evm->hasListeners(Events::preFlush)) { |
|
| 361 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->dm)); |
||
| 362 | } |
||
| 363 | |||
| 364 | // Compute changes done since last commit. |
||
| 365 | 602 | $this->computeChangeSets(); |
|
| 366 | |||
| 367 | 601 | if (! ($this->documentInsertions || |
|
| 368 | 255 | $this->documentUpserts || |
|
| 369 | 213 | $this->documentDeletions || |
|
| 370 | 196 | $this->documentUpdates || |
|
| 371 | 22 | $this->collectionUpdates || |
|
| 372 | 22 | $this->collectionDeletions || |
|
| 373 | 601 | $this->orphanRemovals) |
|
| 374 | ) { |
||
| 375 | 22 | return; // Nothing to do. |
|
| 376 | } |
||
| 377 | |||
| 378 | 598 | $this->commitsInProgress++; |
|
| 379 | 598 | if ($this->commitsInProgress > 1) { |
|
| 380 | throw MongoDBException::commitInProgress(); |
||
| 381 | } |
||
| 382 | try { |
||
| 383 | 598 | if ($this->orphanRemovals) { |
|
| 384 | 50 | foreach ($this->orphanRemovals as $removal) { |
|
| 385 | 50 | $this->remove($removal); |
|
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | // Raise onFlush |
||
| 390 | 598 | if ($this->evm->hasListeners(Events::onFlush)) { |
|
| 391 | 5 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->dm)); |
|
| 392 | } |
||
| 393 | |||
| 394 | 597 | foreach ($this->getClassesForCommitAction($this->documentUpserts) as $classAndDocuments) { |
|
| 395 | 86 | [$class, $documents] = $classAndDocuments; |
|
| 396 | 86 | $this->executeUpserts($class, $documents, $options); |
|
| 397 | } |
||
| 398 | |||
| 399 | 597 | foreach ($this->getClassesForCommitAction($this->documentInsertions) as $classAndDocuments) { |
|
| 400 | 521 | [$class, $documents] = $classAndDocuments; |
|
| 401 | 521 | $this->executeInserts($class, $documents, $options); |
|
| 402 | } |
||
| 403 | |||
| 404 | 596 | foreach ($this->getClassesForCommitAction($this->documentUpdates) as $classAndDocuments) { |
|
| 405 | 228 | [$class, $documents] = $classAndDocuments; |
|
| 406 | 228 | $this->executeUpdates($class, $documents, $options); |
|
| 407 | } |
||
| 408 | |||
| 409 | 596 | foreach ($this->getClassesForCommitAction($this->documentDeletions, true) as $classAndDocuments) { |
|
| 410 | 74 | [$class, $documents] = $classAndDocuments; |
|
| 411 | 74 | $this->executeDeletions($class, $documents, $options); |
|
| 412 | } |
||
| 413 | |||
| 414 | // Raise postFlush |
||
| 415 | 596 | if ($this->evm->hasListeners(Events::postFlush)) { |
|
| 416 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->dm)); |
||
| 417 | } |
||
| 418 | |||
| 419 | // Clear up |
||
| 420 | 596 | $this->documentInsertions = |
|
| 421 | 596 | $this->documentUpserts = |
|
| 422 | 596 | $this->documentUpdates = |
|
| 423 | 596 | $this->documentDeletions = |
|
| 424 | 596 | $this->documentChangeSets = |
|
| 425 | 596 | $this->collectionUpdates = |
|
| 426 | 596 | $this->collectionDeletions = |
|
| 427 | 596 | $this->visitedCollections = |
|
| 428 | 596 | $this->scheduledForDirtyCheck = |
|
| 429 | 596 | $this->orphanRemovals = |
|
| 430 | 596 | $this->hasScheduledCollections = []; |
|
| 431 | 596 | } finally { |
|
| 432 | 598 | $this->commitsInProgress--; |
|
| 433 | } |
||
| 434 | 596 | } |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Groups a list of scheduled documents by their class. |
||
| 438 | */ |
||
| 439 | 597 | private function getClassesForCommitAction(array $documents, bool $includeEmbedded = false) : array |
|
| 440 | { |
||
| 441 | 597 | if (empty($documents)) { |
|
| 442 | 597 | return []; |
|
| 443 | } |
||
| 444 | 596 | $divided = []; |
|
| 445 | 596 | $embeds = []; |
|
| 446 | 596 | foreach ($documents as $oid => $d) { |
|
| 447 | 596 | $className = get_class($d); |
|
| 448 | 596 | if (isset($embeds[$className])) { |
|
| 449 | 74 | continue; |
|
| 450 | } |
||
| 451 | 596 | if (isset($divided[$className])) { |
|
| 452 | 159 | $divided[$className][1][$oid] = $d; |
|
| 453 | 159 | continue; |
|
| 454 | } |
||
| 455 | 596 | $class = $this->dm->getClassMetadata($className); |
|
| 456 | 596 | if ($class->isEmbeddedDocument && ! $includeEmbedded) { |
|
| 457 | 176 | $embeds[$className] = true; |
|
| 458 | 176 | continue; |
|
| 459 | } |
||
| 460 | 596 | if (empty($divided[$class->name])) { |
|
| 461 | 596 | $divided[$class->name] = [$class, [$oid => $d]]; |
|
| 462 | } else { |
||
| 463 | 596 | $divided[$class->name][1][$oid] = $d; |
|
| 464 | } |
||
| 465 | } |
||
| 466 | 596 | return $divided; |
|
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Compute changesets of all documents scheduled for insertion. |
||
| 471 | * |
||
| 472 | * Embedded documents will not be processed. |
||
| 473 | */ |
||
| 474 | 607 | private function computeScheduleInsertsChangeSets() : void |
|
| 475 | { |
||
| 476 | 607 | foreach ($this->documentInsertions as $document) { |
|
| 477 | 534 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 478 | 534 | if ($class->isEmbeddedDocument) { |
|
| 479 | 158 | continue; |
|
| 480 | } |
||
| 481 | |||
| 482 | 528 | $this->computeChangeSet($class, $document); |
|
| 483 | } |
||
| 484 | 606 | } |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Compute changesets of all documents scheduled for upsert. |
||
| 488 | * |
||
| 489 | * Embedded documents will not be processed. |
||
| 490 | */ |
||
| 491 | 606 | private function computeScheduleUpsertsChangeSets() : void |
|
| 492 | { |
||
| 493 | 606 | foreach ($this->documentUpserts as $document) { |
|
| 494 | 85 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 495 | 85 | if ($class->isEmbeddedDocument) { |
|
| 496 | continue; |
||
| 497 | } |
||
| 498 | |||
| 499 | 85 | $this->computeChangeSet($class, $document); |
|
| 500 | } |
||
| 501 | 606 | } |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Gets the changeset for a document. |
||
| 505 | * |
||
| 506 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 507 | */ |
||
| 508 | 602 | public function getDocumentChangeSet(object $document) : array |
|
| 509 | { |
||
| 510 | 602 | $oid = spl_object_hash($document); |
|
| 511 | |||
| 512 | 602 | return $this->documentChangeSets[$oid] ?? []; |
|
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * INTERNAL: |
||
| 517 | * Sets the changeset for a document. |
||
| 518 | */ |
||
| 519 | 1 | public function setDocumentChangeSet(object $document, array $changeset) : void |
|
| 520 | { |
||
| 521 | 1 | $this->documentChangeSets[spl_object_hash($document)] = $changeset; |
|
| 522 | 1 | } |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | 607 | public function getDocumentActualData(object $document) : array |
|
| 530 | { |
||
| 531 | 607 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 532 | 607 | $actualData = []; |
|
| 533 | 607 | foreach ($class->reflFields as $name => $refProp) { |
|
| 534 | 607 | $mapping = $class->fieldMappings[$name]; |
|
| 535 | // skip not saved fields |
||
| 536 | 607 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 537 | 49 | continue; |
|
| 538 | } |
||
| 539 | 607 | $value = $refProp->getValue($document); |
|
| 540 | 607 | if ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 541 | 607 | && $value !== null && ! ($value instanceof PersistentCollectionInterface)) { |
|
| 542 | // If $actualData[$name] is not a Collection then use an ArrayCollection. |
||
| 543 | 393 | if (! $value instanceof Collection) { |
|
| 544 | 146 | $value = new ArrayCollection($value); |
|
| 545 | } |
||
| 546 | |||
| 547 | // Inject PersistentCollection |
||
| 548 | 393 | $coll = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $mapping, $value); |
|
| 549 | 393 | $coll->setOwner($document, $mapping); |
|
| 550 | 393 | $coll->setDirty(! $value->isEmpty()); |
|
| 551 | 393 | $class->reflFields[$name]->setValue($document, $coll); |
|
| 552 | 393 | $actualData[$name] = $coll; |
|
| 553 | } else { |
||
| 554 | 607 | $actualData[$name] = $value; |
|
| 555 | } |
||
| 556 | } |
||
| 557 | 607 | return $actualData; |
|
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Computes the changes that happened to a single document. |
||
| 562 | * |
||
| 563 | * Modifies/populates the following properties: |
||
| 564 | * |
||
| 565 | * {@link originalDocumentData} |
||
| 566 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 567 | * then it was not fetched from the database and therefore we have no original |
||
| 568 | * document data yet. All of the current document data is stored as the original document data. |
||
| 569 | * |
||
| 570 | * {@link documentChangeSets} |
||
| 571 | * The changes detected on all properties of the document are stored there. |
||
| 572 | * A change is a tuple array where the first entry is the old value and the second |
||
| 573 | * entry is the new value of the property. Changesets are used by persisters |
||
| 574 | * to INSERT/UPDATE the persistent document state. |
||
| 575 | * |
||
| 576 | * {@link documentUpdates} |
||
| 577 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 578 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 579 | * there to mark it for an update. |
||
| 580 | */ |
||
| 581 | 603 | public function computeChangeSet(ClassMetadata $class, object $document) : void |
|
| 582 | { |
||
| 583 | 603 | if (! $class->isInheritanceTypeNone()) { |
|
| 584 | 183 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 585 | } |
||
| 586 | |||
| 587 | // Fire PreFlush lifecycle callbacks |
||
| 588 | 603 | if (! empty($class->lifecycleCallbacks[Events::preFlush])) { |
|
| 589 | 11 | $class->invokeLifecycleCallbacks(Events::preFlush, $document, [new Event\PreFlushEventArgs($this->dm)]); |
|
| 590 | } |
||
| 591 | |||
| 592 | 603 | $this->computeOrRecomputeChangeSet($class, $document); |
|
| 593 | 602 | } |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 597 | */ |
||
| 598 | 603 | private function computeOrRecomputeChangeSet(ClassMetadata $class, object $document, bool $recompute = false) : void |
|
| 599 | { |
||
| 600 | 603 | $oid = spl_object_hash($document); |
|
| 601 | 603 | $actualData = $this->getDocumentActualData($document); |
|
| 602 | 603 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 603 | 603 | if ($isNewDocument) { |
|
| 604 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 605 | // These result in an INSERT. |
||
| 606 | 602 | $this->originalDocumentData[$oid] = $actualData; |
|
| 607 | 602 | $changeSet = []; |
|
| 608 | 602 | foreach ($actualData as $propName => $actualValue) { |
|
| 609 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 610 | * was cloned and its ownership must be fixed |
||
| 611 | */ |
||
| 612 | 602 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 613 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 614 | $actualValue = $actualData[$propName]; |
||
| 615 | } |
||
| 616 | // ignore inverse side of reference relationship |
||
| 617 | 602 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
|
| 618 | 187 | continue; |
|
| 619 | } |
||
| 620 | 602 | $changeSet[$propName] = [null, $actualValue]; |
|
| 621 | } |
||
| 622 | 602 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 623 | } else { |
||
| 624 | 288 | if ($class->isReadOnly) { |
|
| 625 | 2 | return; |
|
| 626 | } |
||
| 627 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 628 | // and we have a copy of the original data |
||
| 629 | 286 | $originalData = $this->originalDocumentData[$oid]; |
|
| 630 | 286 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 631 | 286 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 632 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 633 | } else { |
||
| 634 | 286 | $changeSet = []; |
|
| 635 | } |
||
| 636 | |||
| 637 | 286 | $gridFSMetadataProperty = null; |
|
| 638 | |||
| 639 | 286 | if ($class->isFile) { |
|
| 640 | try { |
||
| 641 | 4 | $gridFSMetadata = $class->getFieldMappingByDbFieldName('metadata'); |
|
| 642 | 3 | $gridFSMetadataProperty = $gridFSMetadata['fieldName']; |
|
| 643 | 1 | } catch (MappingException $e) { |
|
| 644 | } |
||
| 645 | } |
||
| 646 | |||
| 647 | 286 | foreach ($actualData as $propName => $actualValue) { |
|
| 648 | // skip not saved fields |
||
| 649 | 286 | if ((isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) || |
|
| 650 | 286 | ($class->isFile && $propName !== $gridFSMetadataProperty)) { |
|
| 651 | 4 | continue; |
|
| 652 | } |
||
| 653 | |||
| 654 | 285 | $orgValue = $originalData[$propName] ?? null; |
|
| 655 | |||
| 656 | // skip if value has not changed |
||
| 657 | 285 | if ($orgValue === $actualValue) { |
|
| 658 | 283 | if (! $actualValue instanceof PersistentCollectionInterface) { |
|
| 659 | 283 | continue; |
|
| 660 | } |
||
| 661 | |||
| 662 | 199 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
|
| 663 | // consider dirty collections as changed as well |
||
| 664 | 175 | continue; |
|
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 669 | 246 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 670 | 14 | if ($orgValue !== null) { |
|
| 671 | 8 | $this->scheduleOrphanRemoval($orgValue); |
|
| 672 | } |
||
| 673 | 14 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 674 | 14 | continue; |
|
| 675 | } |
||
| 676 | |||
| 677 | // if owning side of reference-one relationship |
||
| 678 | 239 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 679 | 13 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 680 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 681 | } |
||
| 682 | |||
| 683 | 13 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 684 | 13 | continue; |
|
| 685 | } |
||
| 686 | |||
| 687 | 232 | if ($isChangeTrackingNotify) { |
|
| 688 | 3 | continue; |
|
| 689 | } |
||
| 690 | |||
| 691 | // ignore inverse side of reference relationship |
||
| 692 | 230 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
|
| 693 | 6 | continue; |
|
| 694 | } |
||
| 695 | |||
| 696 | // Persistent collection was exchanged with the "originally" |
||
| 697 | // created one. This can only mean it was cloned and replaced |
||
| 698 | // on another document. |
||
| 699 | 228 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 700 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 701 | } |
||
| 702 | |||
| 703 | // if embed-many or reference-many relationship |
||
| 704 | 228 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 705 | 119 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 706 | /* If original collection was exchanged with a non-empty value |
||
| 707 | * and $set will be issued, there is no need to $unset it first |
||
| 708 | */ |
||
| 709 | 119 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 710 | 27 | continue; |
|
| 711 | } |
||
| 712 | 100 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
|
| 713 | 18 | $this->scheduleCollectionDeletion($orgValue); |
|
| 714 | } |
||
| 715 | 100 | continue; |
|
| 716 | } |
||
| 717 | |||
| 718 | // skip equivalent date values |
||
| 719 | 146 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 720 | /** @var DateType $dateType */ |
||
| 721 | 37 | $dateType = Type::getType('date'); |
|
| 722 | 37 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 723 | 37 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 724 | |||
| 725 | 37 | $orgTimestamp = $dbOrgValue instanceof UTCDateTime ? $dbOrgValue->toDateTime()->getTimestamp() : null; |
|
| 726 | 37 | $actualTimestamp = $dbActualValue instanceof UTCDateTime ? $dbActualValue->toDateTime()->getTimestamp() : null; |
|
| 727 | |||
| 728 | 37 | if ($orgTimestamp === $actualTimestamp) { |
|
| 729 | 30 | continue; |
|
| 730 | } |
||
| 731 | } |
||
| 732 | |||
| 733 | // regular field |
||
| 734 | 129 | $changeSet[$propName] = [$orgValue, $actualValue]; |
|
| 735 | } |
||
| 736 | 286 | if ($changeSet) { |
|
| 737 | 235 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 738 | 19 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 739 | 232 | : $changeSet; |
|
| 740 | |||
| 741 | 235 | $this->originalDocumentData[$oid] = $actualData; |
|
| 742 | 235 | $this->scheduleForUpdate($document); |
|
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | // Look for changes in associations of the document |
||
| 747 | 603 | $associationMappings = array_filter( |
|
| 748 | 603 | $class->associationMappings, |
|
| 749 | static function ($assoc) { |
||
| 750 | 464 | return empty($assoc['notSaved']); |
|
| 751 | 603 | } |
|
| 752 | ); |
||
| 753 | |||
| 754 | 603 | foreach ($associationMappings as $mapping) { |
|
| 755 | 464 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 756 | |||
| 757 | 464 | if ($value === null) { |
|
| 758 | 323 | continue; |
|
| 759 | } |
||
| 760 | |||
| 761 | 445 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 762 | |||
| 763 | 444 | if (isset($mapping['reference'])) { |
|
| 764 | 336 | continue; |
|
| 765 | } |
||
| 766 | |||
| 767 | 347 | $values = $mapping['type'] === ClassMetadata::ONE ? [$value] : $value->unwrap(); |
|
| 768 | |||
| 769 | 347 | foreach ($values as $obj) { |
|
| 770 | 181 | $oid2 = spl_object_hash($obj); |
|
| 771 | |||
| 772 | 181 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 773 | 179 | if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) { |
|
| 774 | // instance of $value is the same as it was previously otherwise there would be |
||
| 775 | // change set already in place |
||
| 776 | 41 | $this->documentChangeSets[$oid][$mapping['fieldName']] = [$value, $value]; |
|
| 777 | } |
||
| 778 | |||
| 779 | 179 | if (! $isNewDocument) { |
|
| 780 | 81 | $this->scheduleForUpdate($document); |
|
| 781 | } |
||
| 782 | |||
| 783 | 347 | break; |
|
| 784 | } |
||
| 785 | } |
||
| 786 | } |
||
| 787 | 602 | } |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Computes all the changes that have been done to documents and collections |
||
| 791 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 792 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 793 | */ |
||
| 794 | 607 | public function computeChangeSets() : void |
|
| 795 | { |
||
| 796 | 607 | $this->computeScheduleInsertsChangeSets(); |
|
| 797 | 606 | $this->computeScheduleUpsertsChangeSets(); |
|
| 798 | |||
| 799 | // Compute changes for other MANAGED documents. Change tracking policies take effect here. |
||
| 800 | 606 | foreach ($this->identityMap as $className => $documents) { |
|
| 801 | 606 | $class = $this->dm->getClassMetadata($className); |
|
| 802 | 606 | if ($class->isEmbeddedDocument) { |
|
| 803 | /* we do not want to compute changes to embedded documents up front |
||
| 804 | * in case embedded document was replaced and its changeset |
||
| 805 | * would corrupt data. Embedded documents' change set will |
||
| 806 | * be calculated by reachability from owning document. |
||
| 807 | */ |
||
| 808 | 171 | continue; |
|
| 809 | } |
||
| 810 | |||
| 811 | // If change tracking is explicit or happens through notification, then only compute |
||
| 812 | // changes on document of that type that are explicitly marked for synchronization. |
||
| 813 | switch (true) { |
||
| 814 | 606 | case $class->isChangeTrackingDeferredImplicit(): |
|
| 815 | 605 | $documentsToProcess = $documents; |
|
| 816 | 605 | break; |
|
| 817 | |||
| 818 | 4 | case isset($this->scheduledForDirtyCheck[$className]): |
|
| 819 | 3 | $documentsToProcess = $this->scheduledForDirtyCheck[$className]; |
|
| 820 | 3 | break; |
|
| 821 | |||
| 822 | default: |
||
| 823 | 4 | $documentsToProcess = []; |
|
| 824 | } |
||
| 825 | |||
| 826 | 606 | foreach ($documentsToProcess as $document) { |
|
| 827 | // Ignore uninitialized proxy objects |
||
| 828 | 601 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
|
| 829 | 9 | continue; |
|
| 830 | } |
||
| 831 | // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. |
||
| 832 | 601 | $oid = spl_object_hash($document); |
|
| 833 | 601 | if (isset($this->documentInsertions[$oid]) |
|
| 834 | 329 | || isset($this->documentUpserts[$oid]) |
|
| 835 | 283 | || isset($this->documentDeletions[$oid]) |
|
| 836 | 601 | || ! isset($this->documentStates[$oid]) |
|
| 837 | ) { |
||
| 838 | 599 | continue; |
|
| 839 | } |
||
| 840 | |||
| 841 | 606 | $this->computeChangeSet($class, $document); |
|
| 842 | } |
||
| 843 | } |
||
| 844 | 606 | } |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Computes the changes of an association. |
||
| 848 | * |
||
| 849 | * @param mixed $value The value of the association. |
||
| 850 | * |
||
| 851 | * @throws InvalidArgumentException |
||
| 852 | */ |
||
| 853 | 445 | private function computeAssociationChanges(object $parentDocument, array $assoc, $value) : void |
|
| 957 | |||
| 958 | /** |
||
| 959 | * INTERNAL: |
||
| 960 | * Computes the changeset of an individual document, independently of the |
||
| 961 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 962 | * |
||
| 963 | * The passed document must be a managed document. If the document already has a change set |
||
| 964 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 965 | * whereby changes detected in this method prevail. |
||
| 966 | * |
||
| 967 | * @throws InvalidArgumentException If the passed document is not MANAGED. |
||
| 968 | * |
||
| 969 | * @ignore |
||
| 970 | */ |
||
| 971 | 19 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, object $document) : void |
|
| 972 | { |
||
| 973 | // Ignore uninitialized proxy objects |
||
| 990 | |||
| 991 | /** |
||
| 992 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 993 | */ |
||
| 994 | 632 | private function persistNew(ClassMetadata $class, object $document) : void |
|
| 1037 | |||
| 1038 | /** |
||
| 1039 | * Executes all document insertions for documents of the specified type. |
||
| 1040 | */ |
||
| 1041 | 521 | private function executeInserts(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Executes all document upserts for documents of the specified type. |
||
| 1059 | */ |
||
| 1060 | 86 | private function executeUpserts(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Executes all document updates for documents of the specified type. |
||
| 1078 | */ |
||
| 1079 | 228 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Executes all document deletions for documents of the specified type. |
||
| 1103 | */ |
||
| 1104 | 74 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = []) : void |
|
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Schedules a document for insertion into the database. |
||
| 1143 | * If the document already has an identifier, it will be added to the |
||
| 1144 | * identity map. |
||
| 1145 | * |
||
| 1146 | * @throws InvalidArgumentException |
||
| 1147 | */ |
||
| 1148 | 563 | public function scheduleForInsert(ClassMetadata $class, object $document) : void |
|
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Schedules a document for upsert into the database and adds it to the |
||
| 1173 | * identity map |
||
| 1174 | * |
||
| 1175 | * @throws InvalidArgumentException |
||
| 1176 | */ |
||
| 1177 | 92 | public function scheduleForUpsert(ClassMetadata $class, object $document) : void |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Checks whether a document is scheduled for insertion. |
||
| 1201 | */ |
||
| 1202 | 104 | public function isScheduledForInsert(object $document) : bool |
|
| 1206 | |||
| 1207 | /** |
||
| 1208 | * Checks whether a document is scheduled for upsert. |
||
| 1209 | */ |
||
| 1210 | 5 | public function isScheduledForUpsert(object $document) : bool |
|
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Schedules a document for being updated. |
||
| 1217 | * |
||
| 1218 | * @throws InvalidArgumentException |
||
| 1219 | */ |
||
| 1220 | 236 | public function scheduleForUpdate(object $document) : void |
|
| 1239 | |||
| 1240 | /** |
||
| 1241 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1242 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1243 | * at commit time. |
||
| 1244 | */ |
||
| 1245 | 21 | public function isScheduledForUpdate(object $document) : bool |
|
| 1249 | |||
| 1250 | 1 | public function isScheduledForDirtyCheck(object $document) : bool |
|
| 1255 | |||
| 1256 | /** |
||
| 1257 | * INTERNAL: |
||
| 1258 | * Schedules a document for deletion. |
||
| 1259 | */ |
||
| 1260 | 79 | public function scheduleForDelete(object $document) : void |
|
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1291 | * of work. |
||
| 1292 | */ |
||
| 1293 | 5 | public function isScheduledForDelete(object $document) : bool |
|
| 1297 | |||
| 1298 | /** |
||
| 1299 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1300 | */ |
||
| 1301 | 250 | public function isDocumentScheduled(object $document) : bool |
|
| 1309 | |||
| 1310 | /** |
||
| 1311 | * INTERNAL: |
||
| 1312 | * Registers a document in the identity map. |
||
| 1313 | * |
||
| 1314 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1315 | * the root document. Identifiers are serialized before being used as array |
||
| 1316 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1317 | * |
||
| 1318 | * @ignore |
||
| 1319 | */ |
||
| 1320 | 671 | public function addToIdentityMap(object $document) : bool |
|
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Gets the state of a document with regard to the current unit of work. |
||
| 1341 | * |
||
| 1342 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1343 | * This parameter can be set to improve performance of document state detection |
||
| 1344 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1345 | * is either known or does not matter for the caller of the method. |
||
| 1346 | */ |
||
| 1347 | 636 | public function getDocumentState(object $document, ?int $assume = null) : int |
|
| 1397 | |||
| 1398 | /** |
||
| 1399 | * INTERNAL: |
||
| 1400 | * Removes a document from the identity map. This effectively detaches the |
||
| 1401 | * document from the persistence management of Doctrine. |
||
| 1402 | * |
||
| 1403 | * @throws InvalidArgumentException |
||
| 1404 | * |
||
| 1405 | * @ignore |
||
| 1406 | */ |
||
| 1407 | 91 | public function removeFromIdentityMap(object $document) : bool |
|
| 1427 | |||
| 1428 | /** |
||
| 1429 | * INTERNAL: |
||
| 1430 | * Gets a document in the identity map by its identifier hash. |
||
| 1431 | * |
||
| 1432 | * @param mixed $id Document identifier |
||
| 1433 | * |
||
| 1434 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1435 | * |
||
| 1436 | * @ignore |
||
| 1437 | */ |
||
| 1438 | 40 | public function getById($id, ClassMetadata $class) : object |
|
| 1448 | |||
| 1449 | /** |
||
| 1450 | * INTERNAL: |
||
| 1451 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1452 | * for the given hash, FALSE is returned. |
||
| 1453 | * |
||
| 1454 | * @param mixed $id Document identifier |
||
| 1455 | * |
||
| 1456 | * @return mixed The found document or FALSE. |
||
| 1457 | * |
||
| 1458 | * @throws InvalidArgumentException If the class does not have an identifier. |
||
| 1459 | * |
||
| 1460 | * @ignore |
||
| 1461 | */ |
||
| 1462 | 318 | public function tryGetById($id, ClassMetadata $class) |
|
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Schedules a document for dirty-checking at commit-time. |
||
| 1475 | * |
||
| 1476 | * @todo Rename: scheduleForSynchronization |
||
| 1477 | */ |
||
| 1478 | 3 | public function scheduleForDirtyCheck(object $document) : void |
|
| 1483 | |||
| 1484 | /** |
||
| 1485 | * Checks whether a document is registered in the identity map. |
||
| 1486 | */ |
||
| 1487 | 87 | public function isInIdentityMap(object $document) : bool |
|
| 1500 | |||
| 1501 | 671 | private function getIdForIdentityMap(object $document) : string |
|
| 1514 | |||
| 1515 | /** |
||
| 1516 | * INTERNAL: |
||
| 1517 | * Checks whether an identifier exists in the identity map. |
||
| 1518 | * |
||
| 1519 | * @ignore |
||
| 1520 | */ |
||
| 1521 | public function containsId($id, string $rootClassName) : bool |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Persists a document as part of the current unit of work. |
||
| 1528 | * |
||
| 1529 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1530 | * @throws InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1531 | */ |
||
| 1532 | 633 | public function persist(object $document) : void |
|
| 1541 | |||
| 1542 | /** |
||
| 1543 | * Saves a document as part of the current unit of work. |
||
| 1544 | * This method is internally called during save() cascades as it tracks |
||
| 1545 | * the already visited documents to prevent infinite recursions. |
||
| 1546 | * |
||
| 1547 | * NOTE: This method always considers documents that are not yet known to |
||
| 1548 | * this UnitOfWork as NEW. |
||
| 1549 | * |
||
| 1550 | * @throws InvalidArgumentException |
||
| 1551 | * @throws MongoDBException |
||
| 1552 | */ |
||
| 1553 | 632 | private function doPersist(object $document, array &$visited) : void |
|
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Deletes a document as part of the current unit of work. |
||
| 1601 | */ |
||
| 1602 | 78 | public function remove(object $document) |
|
| 1607 | |||
| 1608 | /** |
||
| 1609 | * Deletes a document as part of the current unit of work. |
||
| 1610 | * |
||
| 1611 | * This method is internally called during delete() cascades as it tracks |
||
| 1612 | * the already visited documents to prevent infinite recursions. |
||
| 1613 | * |
||
| 1614 | * @throws MongoDBException |
||
| 1615 | */ |
||
| 1616 | 78 | private function doRemove(object $document, array &$visited) : void |
|
| 1617 | { |
||
| 1618 | 78 | $oid = spl_object_hash($document); |
|
| 1619 | 78 | if (isset($visited[$oid])) { |
|
| 1620 | 1 | return; // Prevent infinite recursion |
|
| 1621 | } |
||
| 1622 | |||
| 1623 | 78 | $visited[$oid] = $document; // mark visited |
|
| 1624 | |||
| 1625 | /* Cascade first, because scheduleForDelete() removes the entity from |
||
| 1626 | * the identity map, which can cause problems when a lazy Proxy has to |
||
| 1627 | * be initialized for the cascade operation. |
||
| 1628 | */ |
||
| 1629 | 78 | $this->cascadeRemove($document, $visited); |
|
| 1630 | |||
| 1631 | 78 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1632 | 78 | $documentState = $this->getDocumentState($document); |
|
| 1633 | switch ($documentState) { |
||
| 1634 | 78 | case self::STATE_NEW: |
|
| 1635 | 78 | case self::STATE_REMOVED: |
|
| 1636 | // nothing to do |
||
| 1637 | 1 | break; |
|
| 1638 | 78 | case self::STATE_MANAGED: |
|
| 1639 | 78 | $this->lifecycleEventManager->preRemove($class, $document); |
|
| 1640 | 78 | $this->scheduleForDelete($document); |
|
| 1641 | 78 | break; |
|
| 1642 | case self::STATE_DETACHED: |
||
| 1643 | throw MongoDBException::detachedDocumentCannotBeRemoved(); |
||
| 1644 | default: |
||
| 1645 | throw MongoDBException::invalidDocumentState($documentState); |
||
| 1646 | } |
||
| 1647 | 78 | } |
|
| 1648 | |||
| 1649 | /** |
||
| 1650 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1651 | */ |
||
| 1652 | 11 | public function merge(object $document) : object |
|
| 1658 | |||
| 1659 | /** |
||
| 1660 | * Executes a merge operation on a document. |
||
| 1661 | * |
||
| 1662 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1663 | * @throws LockException If the document uses optimistic locking through a |
||
| 1664 | * version attribute and the version check against the |
||
| 1665 | * managed copy fails. |
||
| 1666 | */ |
||
| 1667 | 11 | private function doMerge(object $document, array &$visited, ?object $prevManagedCopy = null, ?array $assoc = null) : object |
|
| 1668 | { |
||
| 1669 | 11 | $oid = spl_object_hash($document); |
|
| 1670 | |||
| 1671 | 11 | if (isset($visited[$oid])) { |
|
| 1672 | 1 | return $visited[$oid]; // Prevent infinite recursion |
|
| 1673 | } |
||
| 1674 | |||
| 1675 | 11 | $visited[$oid] = $document; // mark visited |
|
| 1676 | |||
| 1677 | 11 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1678 | |||
| 1679 | /* First we assume DETACHED, although it can still be NEW but we can |
||
| 1680 | * avoid an extra DB round trip this way. If it is not MANAGED but has |
||
| 1681 | * an identity, we need to fetch it from the DB anyway in order to |
||
| 1682 | * merge. MANAGED documents are ignored by the merge operation. |
||
| 1683 | */ |
||
| 1684 | 11 | $managedCopy = $document; |
|
| 1685 | |||
| 1686 | 11 | if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
|
| 1687 | 11 | if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { |
|
| 1688 | $document->initializeProxy(); |
||
| 1689 | } |
||
| 1690 | |||
| 1691 | 11 | $identifier = $class->getIdentifier(); |
|
| 1692 | // We always have one element in the identifier array but it might be null |
||
| 1693 | 11 | $id = $identifier[0] !== null ? $class->getIdentifierObject($document) : null; |
|
| 1694 | 11 | $managedCopy = null; |
|
| 1695 | |||
| 1696 | // Try to fetch document from the database |
||
| 1697 | 11 | if (! $class->isEmbeddedDocument && $id !== null) { |
|
| 1698 | 11 | $managedCopy = $this->dm->find($class->name, $id); |
|
| 1699 | |||
| 1700 | // Managed copy may be removed in which case we can't merge |
||
| 1701 | 11 | if ($managedCopy && $this->getDocumentState($managedCopy) === self::STATE_REMOVED) { |
|
| 1702 | throw new InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.'); |
||
| 1703 | } |
||
| 1704 | |||
| 1705 | 11 | if ($managedCopy instanceof GhostObjectInterface && ! $managedCopy->isProxyInitialized()) { |
|
| 1706 | $managedCopy->initializeProxy(); |
||
| 1707 | } |
||
| 1708 | } |
||
| 1709 | |||
| 1710 | 11 | if ($managedCopy === null) { |
|
| 1711 | // Create a new managed instance |
||
| 1712 | 4 | $managedCopy = $class->newInstance(); |
|
| 1713 | 4 | if ($id !== null) { |
|
| 1714 | 3 | $class->setIdentifierValue($managedCopy, $id); |
|
| 1715 | } |
||
| 1716 | 4 | $this->persistNew($class, $managedCopy); |
|
| 1717 | } |
||
| 1718 | |||
| 1719 | 11 | if ($class->isVersioned) { |
|
| 1720 | $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); |
||
| 1721 | $documentVersion = $class->reflFields[$class->versionField]->getValue($document); |
||
| 1722 | |||
| 1723 | // Throw exception if versions don't match |
||
| 1724 | if ($managedCopyVersion !== $documentVersion) { |
||
| 1725 | throw LockException::lockFailedVersionMissmatch($document, $documentVersion, $managedCopyVersion); |
||
| 1726 | } |
||
| 1727 | } |
||
| 1728 | |||
| 1729 | // Merge state of $document into existing (managed) document |
||
| 1730 | 11 | foreach ($class->reflClass->getProperties() as $prop) { |
|
| 1731 | 11 | $name = $prop->name; |
|
| 1732 | 11 | $prop->setAccessible(true); |
|
| 1733 | 11 | if (! isset($class->associationMappings[$name])) { |
|
| 1734 | 11 | if (! $class->isIdentifier($name)) { |
|
| 1735 | 11 | $prop->setValue($managedCopy, $prop->getValue($document)); |
|
| 1736 | } |
||
| 1737 | } else { |
||
| 1738 | 11 | $assoc2 = $class->associationMappings[$name]; |
|
| 1739 | |||
| 1740 | 11 | if ($assoc2['type'] === 'one') { |
|
| 1741 | 5 | $other = $prop->getValue($document); |
|
| 1742 | |||
| 1743 | 5 | if ($other === null) { |
|
| 1744 | 2 | $prop->setValue($managedCopy, null); |
|
| 1745 | 4 | } elseif ($other instanceof GhostObjectInterface && ! $other->isProxyInitialized()) { |
|
| 1746 | // Do not merge fields marked lazy that have not been fetched |
||
| 1747 | continue; |
||
| 1748 | 4 | } elseif (! $assoc2['isCascadeMerge']) { |
|
| 1749 | if ($this->getDocumentState($other) === self::STATE_DETACHED) { |
||
| 1750 | $targetDocument = $assoc2['targetDocument'] ?? get_class($other); |
||
| 1751 | /** @var ClassMetadata $targetClass */ |
||
| 1752 | $targetClass = $this->dm->getClassMetadata($targetDocument); |
||
| 1753 | $relatedId = $targetClass->getIdentifierObject($other); |
||
| 1754 | |||
| 1755 | if ($targetClass->subClasses) { |
||
| 1756 | $other = $this->dm->find($targetClass->name, $relatedId); |
||
| 1757 | } else { |
||
| 1758 | $other = $this |
||
| 1759 | ->dm |
||
| 1760 | ->getProxyFactory() |
||
| 1761 | ->getProxy($targetClass, $relatedId); |
||
| 1762 | $this->registerManaged($other, $relatedId, []); |
||
| 1763 | } |
||
| 1764 | } |
||
| 1765 | |||
| 1766 | 5 | $prop->setValue($managedCopy, $other); |
|
| 1767 | } |
||
| 1768 | } else { |
||
| 1769 | 10 | $mergeCol = $prop->getValue($document); |
|
| 1770 | |||
| 1771 | 10 | if ($mergeCol instanceof PersistentCollectionInterface && ! $mergeCol->isInitialized() && ! $assoc2['isCascadeMerge']) { |
|
| 1772 | /* Do not merge fields marked lazy that have not |
||
| 1773 | * been fetched. Keep the lazy persistent collection |
||
| 1774 | * of the managed copy. |
||
| 1775 | */ |
||
| 1776 | 3 | continue; |
|
| 1777 | } |
||
| 1778 | |||
| 1779 | 10 | $managedCol = $prop->getValue($managedCopy); |
|
| 1780 | |||
| 1781 | 10 | if (! $managedCol) { |
|
| 1782 | 1 | $managedCol = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $assoc2, null); |
|
| 1783 | 1 | $managedCol->setOwner($managedCopy, $assoc2); |
|
| 1784 | 1 | $prop->setValue($managedCopy, $managedCol); |
|
| 1785 | 1 | $this->originalDocumentData[$oid][$name] = $managedCol; |
|
| 1786 | } |
||
| 1787 | |||
| 1788 | /* Note: do not process association's target documents. |
||
| 1789 | * They will be handled during the cascade. Initialize |
||
| 1790 | * and, if necessary, clear $managedCol for now. |
||
| 1791 | */ |
||
| 1792 | 10 | if ($assoc2['isCascadeMerge']) { |
|
| 1793 | 10 | $managedCol->initialize(); |
|
| 1794 | |||
| 1795 | // If $managedCol differs from the merged collection, clear and set dirty |
||
| 1796 | 10 | if (! $managedCol->isEmpty() && $managedCol !== $mergeCol) { |
|
| 1797 | 3 | $managedCol->unwrap()->clear(); |
|
| 1798 | 3 | $managedCol->setDirty(true); |
|
| 1799 | |||
| 1800 | 3 | if ($assoc2['isOwningSide'] && $class->isChangeTrackingNotify()) { |
|
| 1801 | $this->scheduleForDirtyCheck($managedCopy); |
||
| 1802 | } |
||
| 1803 | } |
||
| 1804 | } |
||
| 1805 | } |
||
| 1806 | } |
||
| 1807 | |||
| 1808 | 11 | if (! $class->isChangeTrackingNotify()) { |
|
| 1809 | 11 | continue; |
|
| 1810 | } |
||
| 1811 | |||
| 1812 | // Just treat all properties as changed, there is no other choice. |
||
| 1813 | $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | 11 | if ($class->isChangeTrackingDeferredExplicit()) { |
|
| 1817 | $this->scheduleForDirtyCheck($document); |
||
| 1818 | } |
||
| 1819 | } |
||
| 1820 | |||
| 1821 | 11 | if ($prevManagedCopy !== null) { |
|
| 1822 | 5 | $assocField = $assoc['fieldName']; |
|
| 1823 | 5 | $prevClass = $this->dm->getClassMetadata(get_class($prevManagedCopy)); |
|
| 1824 | |||
| 1825 | 5 | if ($assoc['type'] === 'one') { |
|
| 1826 | 3 | $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); |
|
| 1827 | } else { |
||
| 1828 | 4 | $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy); |
|
| 1829 | |||
| 1830 | 4 | if ($assoc['type'] === 'many' && isset($assoc['mappedBy'])) { |
|
| 1831 | 1 | $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy); |
|
| 1832 | } |
||
| 1833 | } |
||
| 1834 | } |
||
| 1835 | |||
| 1836 | // Mark the managed copy visited as well |
||
| 1837 | 11 | $visited[spl_object_hash($managedCopy)] = $managedCopy; |
|
| 1838 | |||
| 1839 | 11 | $this->cascadeMerge($document, $managedCopy, $visited); |
|
| 1840 | |||
| 1841 | 11 | return $managedCopy; |
|
| 1842 | } |
||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Detaches a document from the persistence management. It's persistence will |
||
| 1846 | * no longer be managed by Doctrine. |
||
| 1847 | */ |
||
| 1848 | 11 | public function detach(object $document) : void |
|
| 1853 | |||
| 1854 | /** |
||
| 1855 | * Executes a detach operation on the given document. |
||
| 1856 | * |
||
| 1857 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1858 | */ |
||
| 1859 | 17 | private function doDetach(object $document, array &$visited) : void |
|
| 1891 | |||
| 1892 | /** |
||
| 1893 | * Refreshes the state of the given document from the database, overwriting |
||
| 1894 | * any local, unpersisted changes. |
||
| 1895 | * |
||
| 1896 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1897 | */ |
||
| 1898 | 24 | public function refresh(object $document) : void |
|
| 1903 | |||
| 1904 | /** |
||
| 1905 | * Executes a refresh operation on a document. |
||
| 1906 | * |
||
| 1907 | * @throws InvalidArgumentException If the document is not MANAGED. |
||
| 1908 | */ |
||
| 1909 | 24 | private function doRefresh(object $document, array &$visited) : void |
|
| 1930 | |||
| 1931 | /** |
||
| 1932 | * Cascades a refresh operation to associated documents. |
||
| 1933 | */ |
||
| 1934 | 23 | private function cascadeRefresh(object $document, array &$visited) : void |
|
| 1960 | |||
| 1961 | /** |
||
| 1962 | * Cascades a detach operation to associated documents. |
||
| 1963 | */ |
||
| 1964 | 17 | private function cascadeDetach(object $document, array &$visited) : void |
|
| 1985 | /** |
||
| 1986 | * Cascades a merge operation to associated documents. |
||
| 1987 | */ |
||
| 1988 | 11 | private function cascadeMerge(object $document, object $managedCopy, array &$visited) : void |
|
| 2016 | |||
| 2017 | /** |
||
| 2018 | * Cascades the save operation to associated documents. |
||
| 2019 | */ |
||
| 2020 | 629 | private function cascadePersist(object $document, array &$visited) : void |
|
| 2069 | |||
| 2070 | /** |
||
| 2071 | * Cascades the delete operation to associated documents. |
||
| 2072 | */ |
||
| 2073 | 78 | private function cascadeRemove(object $document, array &$visited) : void |
|
| 2095 | |||
| 2096 | /** |
||
| 2097 | * Acquire a lock on the given document. |
||
| 2098 | * |
||
| 2099 | * @throws LockException |
||
| 2100 | * @throws InvalidArgumentException |
||
| 2101 | */ |
||
| 2102 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
| 2126 | |||
| 2127 | /** |
||
| 2128 | * Releases a lock on the given document. |
||
| 2129 | * |
||
| 2130 | * @throws InvalidArgumentException |
||
| 2131 | */ |
||
| 2132 | 1 | public function unlock(object $document) : void |
|
| 2140 | |||
| 2141 | /** |
||
| 2142 | * Clears the UnitOfWork. |
||
| 2143 | */ |
||
| 2144 | 390 | public function clear(?string $documentName = null) : void |
|
| 2182 | |||
| 2183 | /** |
||
| 2184 | * INTERNAL: |
||
| 2185 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2186 | * invoked on that document at the beginning of the next commit of this |
||
| 2187 | * UnitOfWork. |
||
| 2188 | * |
||
| 2189 | * @ignore |
||
| 2190 | */ |
||
| 2191 | 53 | public function scheduleOrphanRemoval(object $document) : void |
|
| 2195 | |||
| 2196 | /** |
||
| 2197 | * INTERNAL: |
||
| 2198 | * Unschedules an embedded or referenced object for removal. |
||
| 2199 | * |
||
| 2200 | * @ignore |
||
| 2201 | */ |
||
| 2202 | 120 | public function unscheduleOrphanRemoval(object $document) : void |
|
| 2207 | |||
| 2208 | /** |
||
| 2209 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2210 | * 1) sets owner if it was cloned |
||
| 2211 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2212 | * 3) NOP if state is OK |
||
| 2213 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2214 | */ |
||
| 2215 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, object $document, ClassMetadata $class, string $propName) : PersistentCollectionInterface |
|
| 2235 | |||
| 2236 | /** |
||
| 2237 | * INTERNAL: |
||
| 2238 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2239 | */ |
||
| 2240 | 43 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2251 | |||
| 2252 | /** |
||
| 2253 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2254 | */ |
||
| 2255 | 213 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) : bool |
|
| 2259 | |||
| 2260 | /** |
||
| 2261 | * INTERNAL: |
||
| 2262 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2263 | */ |
||
| 2264 | 225 | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) : void |
|
| 2275 | |||
| 2276 | /** |
||
| 2277 | * INTERNAL: |
||
| 2278 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2279 | */ |
||
| 2280 | 247 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2297 | |||
| 2298 | /** |
||
| 2299 | * INTERNAL: |
||
| 2300 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2301 | */ |
||
| 2302 | 225 | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) : void |
|
| 2313 | |||
| 2314 | /** |
||
| 2315 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2316 | */ |
||
| 2317 | 133 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) : bool |
|
| 2321 | |||
| 2322 | /** |
||
| 2323 | * INTERNAL: |
||
| 2324 | * Gets PersistentCollections that have been visited during computing change |
||
| 2325 | * set of $document |
||
| 2326 | * |
||
| 2327 | * @return PersistentCollectionInterface[] |
||
| 2328 | */ |
||
| 2329 | 582 | public function getVisitedCollections(object $document) : array |
|
| 2335 | |||
| 2336 | /** |
||
| 2337 | * INTERNAL: |
||
| 2338 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2339 | * |
||
| 2340 | * @return PersistentCollectionInterface[] |
||
| 2341 | */ |
||
| 2342 | 582 | public function getScheduledCollections(object $document) : array |
|
| 2348 | |||
| 2349 | /** |
||
| 2350 | * Checks whether the document is related to a PersistentCollection |
||
| 2351 | * scheduled for update or deletion. |
||
| 2352 | */ |
||
| 2353 | 51 | public function hasScheduledCollections(object $document) : bool |
|
| 2357 | |||
| 2358 | /** |
||
| 2359 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2360 | * a collection scheduled for update or deletion. |
||
| 2361 | * |
||
| 2362 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2363 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2364 | * |
||
| 2365 | * If the collection is nested within atomic collection, it is immediately |
||
| 2366 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2367 | * calculating update data way easier. |
||
| 2368 | */ |
||
| 2369 | 249 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) : void |
|
| 2395 | |||
| 2396 | /** |
||
| 2397 | * Get the top-most owning document of a given document |
||
| 2398 | * |
||
| 2399 | * If a top-level document is provided, that same document will be returned. |
||
| 2400 | * For an embedded document, we will walk through parent associations until |
||
| 2401 | * we find a top-level document. |
||
| 2402 | * |
||
| 2403 | * @throws UnexpectedValueException When a top-level document could not be found. |
||
| 2404 | */ |
||
| 2405 | 251 | public function getOwningDocument(object $document) : object |
|
| 2421 | |||
| 2422 | /** |
||
| 2423 | * Gets the class name for an association (embed or reference) with respect |
||
| 2424 | * to any discriminator value. |
||
| 2425 | * |
||
| 2426 | * @param array|object|null $data |
||
| 2427 | */ |
||
| 2428 | 239 | public function getClassNameForAssociation(array $mapping, $data) : string |
|
| 2458 | |||
| 2459 | /** |
||
| 2460 | * INTERNAL: |
||
| 2461 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2462 | * |
||
| 2463 | * @internal Highly performance-sensitive method. |
||
| 2464 | * |
||
| 2465 | * @ignore |
||
| 2466 | */ |
||
| 2467 | 408 | public function getOrCreateDocument(string $className, array $data, array &$hints = [], ?object $document = null) : object |
|
| 2540 | |||
| 2541 | /** |
||
| 2542 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2543 | */ |
||
| 2544 | 184 | public function loadCollection(PersistentCollectionInterface $collection) : void |
|
| 2549 | |||
| 2550 | /** |
||
| 2551 | * Gets the identity map of the UnitOfWork. |
||
| 2552 | */ |
||
| 2553 | public function getIdentityMap() : array |
||
| 2557 | |||
| 2558 | /** |
||
| 2559 | * Gets the original data of a document. The original data is the data that was |
||
| 2560 | * present at the time the document was reconstituted from the database. |
||
| 2561 | * |
||
| 2562 | * @return array |
||
| 2563 | */ |
||
| 2564 | 1 | public function getOriginalDocumentData(object $document) : array |
|
| 2570 | |||
| 2571 | 63 | public function setOriginalDocumentData(object $document, array $data) : void |
|
| 2577 | |||
| 2578 | /** |
||
| 2579 | * INTERNAL: |
||
| 2580 | * Sets a property value of the original data array of a document. |
||
| 2581 | * |
||
| 2582 | * @param mixed $value |
||
| 2583 | * |
||
| 2584 | * @ignore |
||
| 2585 | */ |
||
| 2586 | 3 | public function setOriginalDocumentProperty(string $oid, string $property, $value) : void |
|
| 2590 | |||
| 2591 | /** |
||
| 2592 | * Gets the identifier of a document. |
||
| 2593 | * |
||
| 2594 | * @return mixed The identifier value |
||
| 2595 | */ |
||
| 2596 | 443 | public function getDocumentIdentifier(object $document) |
|
| 2600 | |||
| 2601 | /** |
||
| 2602 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2603 | * |
||
| 2604 | * @return bool TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2605 | */ |
||
| 2606 | public function hasPendingInsertions() : bool |
||
| 2610 | |||
| 2611 | /** |
||
| 2612 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2613 | * number of documents in the identity map. |
||
| 2614 | */ |
||
| 2615 | 2 | public function size() : int |
|
| 2623 | |||
| 2624 | /** |
||
| 2625 | * INTERNAL: |
||
| 2626 | * Registers a document as managed. |
||
| 2627 | * |
||
| 2628 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2629 | * document class. If the class expects its database identifier to be an |
||
| 2630 | * ObjectId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2631 | * document identifiers map will become inconsistent with the identity map. |
||
| 2632 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2633 | * conversion and throw an exception if it's inconsistent. |
||
| 2634 | * |
||
| 2635 | * @param mixed $id The identifier values. |
||
| 2636 | */ |
||
| 2637 | 393 | public function registerManaged(object $document, $id, array $data) : void |
|
| 2652 | |||
| 2653 | /** |
||
| 2654 | * INTERNAL: |
||
| 2655 | * Clears the property changeset of the document with the given OID. |
||
| 2656 | */ |
||
| 2657 | public function clearDocumentChangeSet(string $oid) |
||
| 2661 | |||
| 2662 | /* PropertyChangedListener implementation */ |
||
| 2663 | |||
| 2664 | /** |
||
| 2665 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2666 | * |
||
| 2667 | * @param object $document The document that owns the property. |
||
| 2668 | * @param string $propertyName The name of the property that changed. |
||
| 2669 | * @param mixed $oldValue The old value of the property. |
||
| 2670 | * @param mixed $newValue The new value of the property. |
||
| 2671 | */ |
||
| 2672 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2689 | |||
| 2690 | /** |
||
| 2691 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2692 | */ |
||
| 2693 | 3 | public function getScheduledDocumentInsertions() : array |
|
| 2697 | |||
| 2698 | /** |
||
| 2699 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2700 | */ |
||
| 2701 | 1 | public function getScheduledDocumentUpserts() : array |
|
| 2705 | |||
| 2706 | /** |
||
| 2707 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2708 | */ |
||
| 2709 | 2 | public function getScheduledDocumentUpdates() : array |
|
| 2713 | |||
| 2714 | /** |
||
| 2715 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2716 | */ |
||
| 2717 | public function getScheduledDocumentDeletions() : array |
||
| 2721 | |||
| 2722 | /** |
||
| 2723 | * Get the currently scheduled complete collection deletions |
||
| 2724 | */ |
||
| 2725 | public function getScheduledCollectionDeletions() : array |
||
| 2729 | |||
| 2730 | /** |
||
| 2731 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2732 | */ |
||
| 2733 | public function getScheduledCollectionUpdates() : array |
||
| 2737 | |||
| 2738 | /** |
||
| 2739 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2740 | */ |
||
| 2741 | public function initializeObject(object $obj) : void |
||
| 2749 | |||
| 2750 | private function objToStr(object $obj) : string |
||
| 2754 | } |
||
| 2755 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.