[345] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
| 7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
| 8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
| 9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
| 10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
| 11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 14 | * |
---|
| 15 | * This software consists of voluntary contributions made by many individuals |
---|
| 16 | * and is licensed under the LGPL. For more information, see |
---|
| 17 | * <http://www.doctrine-project.org>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | namespace Doctrine\ORM; |
---|
| 21 | |
---|
| 22 | use Exception, InvalidArgumentException, UnexpectedValueException, |
---|
| 23 | Doctrine\Common\Collections\ArrayCollection, |
---|
| 24 | Doctrine\Common\Collections\Collection, |
---|
| 25 | Doctrine\Common\NotifyPropertyChanged, |
---|
| 26 | Doctrine\Common\PropertyChangedListener, |
---|
| 27 | Doctrine\Common\Persistence\ObjectManagerAware, |
---|
| 28 | Doctrine\ORM\Event\LifecycleEventArgs, |
---|
| 29 | Doctrine\ORM\Mapping\ClassMetadata, |
---|
| 30 | Doctrine\ORM\Proxy\Proxy; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * The UnitOfWork is responsible for tracking changes to objects during an |
---|
| 34 | * "object-level" transaction and for writing out changes to the database |
---|
| 35 | * in the correct order. |
---|
| 36 | * |
---|
| 37 | * @since 2.0 |
---|
| 38 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
| 39 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
| 40 | * @author Jonathan Wage <jonwage@gmail.com> |
---|
| 41 | * @author Roman Borschel <roman@code-factory.org> |
---|
| 42 | * @internal This class contains highly performance-sensitive code. |
---|
| 43 | */ |
---|
| 44 | class UnitOfWork implements PropertyChangedListener |
---|
| 45 | { |
---|
| 46 | /** |
---|
| 47 | * An entity is in MANAGED state when its persistence is managed by an EntityManager. |
---|
| 48 | */ |
---|
| 49 | const STATE_MANAGED = 1; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * An entity is new if it has just been instantiated (i.e. using the "new" operator) |
---|
| 53 | * and is not (yet) managed by an EntityManager. |
---|
| 54 | */ |
---|
| 55 | const STATE_NEW = 2; |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * A detached entity is an instance with persistent state and identity that is not |
---|
| 59 | * (or no longer) associated with an EntityManager (and a UnitOfWork). |
---|
| 60 | */ |
---|
| 61 | const STATE_DETACHED = 3; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * A removed entity instance is an instance with a persistent identity, |
---|
| 65 | * associated with an EntityManager, whose persistent state will be deleted |
---|
| 66 | * on commit. |
---|
| 67 | */ |
---|
| 68 | const STATE_REMOVED = 4; |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * The identity map that holds references to all managed entities that have |
---|
| 72 | * an identity. The entities are grouped by their class name. |
---|
| 73 | * Since all classes in a hierarchy must share the same identifier set, |
---|
| 74 | * we always take the root class name of the hierarchy. |
---|
| 75 | * |
---|
| 76 | * @var array |
---|
| 77 | */ |
---|
| 78 | private $identityMap = array(); |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | * Map of all identifiers of managed entities. |
---|
| 82 | * Keys are object ids (spl_object_hash). |
---|
| 83 | * |
---|
| 84 | * @var array |
---|
| 85 | */ |
---|
| 86 | private $entityIdentifiers = array(); |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Map of the original entity data of managed entities. |
---|
| 90 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
---|
| 91 | * at commit time. |
---|
| 92 | * |
---|
| 93 | * @var array |
---|
| 94 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
---|
| 95 | * A value will only really be copied if the value in the entity is modified |
---|
| 96 | * by the user. |
---|
| 97 | */ |
---|
| 98 | private $originalEntityData = array(); |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Map of entity changes. Keys are object ids (spl_object_hash). |
---|
| 102 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
---|
| 103 | * |
---|
| 104 | * @var array |
---|
| 105 | */ |
---|
| 106 | private $entityChangeSets = array(); |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * The (cached) states of any known entities. |
---|
| 110 | * Keys are object ids (spl_object_hash). |
---|
| 111 | * |
---|
| 112 | * @var array |
---|
| 113 | */ |
---|
| 114 | private $entityStates = array(); |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * Map of entities that are scheduled for dirty checking at commit time. |
---|
| 118 | * This is only used for entities with a change tracking policy of DEFERRED_EXPLICIT. |
---|
| 119 | * Keys are object ids (spl_object_hash). |
---|
| 120 | * |
---|
| 121 | * @var array |
---|
| 122 | * @todo rename: scheduledForSynchronization |
---|
| 123 | */ |
---|
| 124 | private $scheduledForDirtyCheck = array(); |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * A list of all pending entity insertions. |
---|
| 128 | * |
---|
| 129 | * @var array |
---|
| 130 | */ |
---|
| 131 | private $entityInsertions = array(); |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * A list of all pending entity updates. |
---|
| 135 | * |
---|
| 136 | * @var array |
---|
| 137 | */ |
---|
| 138 | private $entityUpdates = array(); |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | * Any pending extra updates that have been scheduled by persisters. |
---|
| 142 | * |
---|
| 143 | * @var array |
---|
| 144 | */ |
---|
| 145 | private $extraUpdates = array(); |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * A list of all pending entity deletions. |
---|
| 149 | * |
---|
| 150 | * @var array |
---|
| 151 | */ |
---|
| 152 | private $entityDeletions = array(); |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * All pending collection deletions. |
---|
| 156 | * |
---|
| 157 | * @var array |
---|
| 158 | */ |
---|
| 159 | private $collectionDeletions = array(); |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * All pending collection updates. |
---|
| 163 | * |
---|
| 164 | * @var array |
---|
| 165 | */ |
---|
| 166 | private $collectionUpdates = array(); |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
---|
| 170 | * At the end of the UnitOfWork all these collections will make new snapshots |
---|
| 171 | * of their data. |
---|
| 172 | * |
---|
| 173 | * @var array |
---|
| 174 | */ |
---|
| 175 | private $visitedCollections = array(); |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * The EntityManager that "owns" this UnitOfWork instance. |
---|
| 179 | * |
---|
| 180 | * @var \Doctrine\ORM\EntityManager |
---|
| 181 | */ |
---|
| 182 | private $em; |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * The calculator used to calculate the order in which changes to |
---|
| 186 | * entities need to be written to the database. |
---|
| 187 | * |
---|
| 188 | * @var \Doctrine\ORM\Internal\CommitOrderCalculator |
---|
| 189 | */ |
---|
| 190 | private $commitOrderCalculator; |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * The entity persister instances used to persist entity instances. |
---|
| 194 | * |
---|
| 195 | * @var array |
---|
| 196 | */ |
---|
| 197 | private $persisters = array(); |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * The collection persister instances used to persist collections. |
---|
| 201 | * |
---|
| 202 | * @var array |
---|
| 203 | */ |
---|
| 204 | private $collectionPersisters = array(); |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * The EventManager used for dispatching events. |
---|
| 208 | * |
---|
| 209 | * @var EventManager |
---|
| 210 | */ |
---|
| 211 | private $evm; |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Orphaned entities that are scheduled for removal. |
---|
| 215 | * |
---|
| 216 | * @var array |
---|
| 217 | */ |
---|
| 218 | private $orphanRemovals = array(); |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * Read-Only objects are never evaluated |
---|
| 222 | * |
---|
| 223 | * @var array |
---|
| 224 | */ |
---|
| 225 | private $readOnlyObjects = array(); |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * Map of Entity Class-Names and corresponding IDs that should eager loaded when requested. |
---|
| 229 | * |
---|
| 230 | * @var array |
---|
| 231 | */ |
---|
| 232 | private $eagerLoadingEntities = array(); |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * Initializes a new UnitOfWork instance, bound to the given EntityManager. |
---|
| 236 | * |
---|
| 237 | * @param \Doctrine\ORM\EntityManager $em |
---|
| 238 | */ |
---|
| 239 | public function __construct(EntityManager $em) |
---|
| 240 | { |
---|
| 241 | $this->em = $em; |
---|
| 242 | $this->evm = $em->getEventManager(); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Commits the UnitOfWork, executing all operations that have been postponed |
---|
| 247 | * up to this point. The state of all managed entities will be synchronized with |
---|
| 248 | * the database. |
---|
| 249 | * |
---|
| 250 | * The operations are executed in the following order: |
---|
| 251 | * |
---|
| 252 | * 1) All entity insertions |
---|
| 253 | * 2) All entity updates |
---|
| 254 | * 3) All collection deletions |
---|
| 255 | * 4) All collection updates |
---|
| 256 | * 5) All entity deletions |
---|
| 257 | * |
---|
| 258 | * @param object $entity |
---|
| 259 | * @return void |
---|
| 260 | */ |
---|
| 261 | public function commit($entity = null) |
---|
| 262 | { |
---|
| 263 | // Raise preFlush |
---|
| 264 | if ($this->evm->hasListeners(Events::preFlush)) { |
---|
| 265 | $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->em)); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | // Compute changes done since last commit. |
---|
| 269 | if ($entity === null) { |
---|
| 270 | $this->computeChangeSets(); |
---|
| 271 | } else { |
---|
| 272 | $this->computeSingleEntityChangeSet($entity); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | if ( ! ($this->entityInsertions || |
---|
| 276 | $this->entityDeletions || |
---|
| 277 | $this->entityUpdates || |
---|
| 278 | $this->collectionUpdates || |
---|
| 279 | $this->collectionDeletions || |
---|
| 280 | $this->orphanRemovals)) { |
---|
| 281 | return; // Nothing to do. |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | if ($this->orphanRemovals) { |
---|
| 285 | foreach ($this->orphanRemovals as $orphan) { |
---|
| 286 | $this->remove($orphan); |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | // Raise onFlush |
---|
| 291 | if ($this->evm->hasListeners(Events::onFlush)) { |
---|
| 292 | $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em)); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | // Now we need a commit order to maintain referential integrity |
---|
| 296 | $commitOrder = $this->getCommitOrder(); |
---|
| 297 | |
---|
| 298 | $conn = $this->em->getConnection(); |
---|
| 299 | $conn->beginTransaction(); |
---|
| 300 | |
---|
| 301 | try { |
---|
| 302 | if ($this->entityInsertions) { |
---|
| 303 | foreach ($commitOrder as $class) { |
---|
| 304 | $this->executeInserts($class); |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | if ($this->entityUpdates) { |
---|
| 309 | foreach ($commitOrder as $class) { |
---|
| 310 | $this->executeUpdates($class); |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | // Extra updates that were requested by persisters. |
---|
| 315 | if ($this->extraUpdates) { |
---|
| 316 | $this->executeExtraUpdates(); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | // Collection deletions (deletions of complete collections) |
---|
| 320 | foreach ($this->collectionDeletions as $collectionToDelete) { |
---|
| 321 | $this->getCollectionPersister($collectionToDelete->getMapping())->delete($collectionToDelete); |
---|
| 322 | } |
---|
| 323 | // Collection updates (deleteRows, updateRows, insertRows) |
---|
| 324 | foreach ($this->collectionUpdates as $collectionToUpdate) { |
---|
| 325 | $this->getCollectionPersister($collectionToUpdate->getMapping())->update($collectionToUpdate); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | // Entity deletions come last and need to be in reverse commit order |
---|
| 329 | if ($this->entityDeletions) { |
---|
| 330 | for ($count = count($commitOrder), $i = $count - 1; $i >= 0; --$i) { |
---|
| 331 | $this->executeDeletions($commitOrder[$i]); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | $conn->commit(); |
---|
| 336 | } catch (Exception $e) { |
---|
| 337 | $this->em->close(); |
---|
| 338 | $conn->rollback(); |
---|
| 339 | |
---|
| 340 | throw $e; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | // Take new snapshots from visited collections |
---|
| 344 | foreach ($this->visitedCollections as $coll) { |
---|
| 345 | $coll->takeSnapshot(); |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | // Raise postFlush |
---|
| 349 | if ($this->evm->hasListeners(Events::postFlush)) { |
---|
| 350 | $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em)); |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | // Clear up |
---|
| 354 | $this->entityInsertions = |
---|
| 355 | $this->entityUpdates = |
---|
| 356 | $this->entityDeletions = |
---|
| 357 | $this->extraUpdates = |
---|
| 358 | $this->entityChangeSets = |
---|
| 359 | $this->collectionUpdates = |
---|
| 360 | $this->collectionDeletions = |
---|
| 361 | $this->visitedCollections = |
---|
| 362 | $this->scheduledForDirtyCheck = |
---|
| 363 | $this->orphanRemovals = array(); |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | /** |
---|
| 367 | * Compute the changesets of all entities scheduled for insertion |
---|
| 368 | * |
---|
| 369 | * @return void |
---|
| 370 | */ |
---|
| 371 | private function computeScheduleInsertsChangeSets() |
---|
| 372 | { |
---|
| 373 | foreach ($this->entityInsertions as $entity) { |
---|
| 374 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 375 | |
---|
| 376 | $this->computeChangeSet($class, $entity); |
---|
| 377 | } |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * Only flush the given entity according to a ruleset that keeps the UoW consistent. |
---|
| 382 | * |
---|
| 383 | * 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well! |
---|
| 384 | * 2. Read Only entities are skipped. |
---|
| 385 | * 3. Proxies are skipped. |
---|
| 386 | * 4. Only if entity is properly managed. |
---|
| 387 | * |
---|
| 388 | * @param object $entity |
---|
| 389 | * @return void |
---|
| 390 | */ |
---|
| 391 | private function computeSingleEntityChangeSet($entity) |
---|
| 392 | { |
---|
| 393 | if ( $this->getEntityState($entity) !== self::STATE_MANAGED) { |
---|
| 394 | throw new \InvalidArgumentException("Entity has to be managed for single computation " . self::objToStr($entity)); |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 398 | |
---|
| 399 | if ($class->isChangeTrackingDeferredImplicit()) { |
---|
| 400 | $this->persist($entity); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | // Compute changes for INSERTed entities first. This must always happen even in this case. |
---|
| 404 | $this->computeScheduleInsertsChangeSets(); |
---|
| 405 | |
---|
| 406 | if ($class->isReadOnly) { |
---|
| 407 | return; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | // Ignore uninitialized proxy objects |
---|
| 411 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
---|
| 412 | return; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION are processed here. |
---|
| 416 | $oid = spl_object_hash($entity); |
---|
| 417 | |
---|
| 418 | if ( ! isset($this->entityInsertions[$oid]) && isset($this->entityStates[$oid])) { |
---|
| 419 | $this->computeChangeSet($class, $entity); |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | /** |
---|
| 424 | * Executes any extra updates that have been scheduled. |
---|
| 425 | */ |
---|
| 426 | private function executeExtraUpdates() |
---|
| 427 | { |
---|
| 428 | foreach ($this->extraUpdates as $oid => $update) { |
---|
| 429 | list ($entity, $changeset) = $update; |
---|
| 430 | |
---|
| 431 | $this->entityChangeSets[$oid] = $changeset; |
---|
| 432 | $this->getEntityPersister(get_class($entity))->update($entity); |
---|
| 433 | } |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | /** |
---|
| 437 | * Gets the changeset for an entity. |
---|
| 438 | * |
---|
| 439 | * @return array |
---|
| 440 | */ |
---|
| 441 | public function getEntityChangeSet($entity) |
---|
| 442 | { |
---|
| 443 | $oid = spl_object_hash($entity); |
---|
| 444 | |
---|
| 445 | if (isset($this->entityChangeSets[$oid])) { |
---|
| 446 | return $this->entityChangeSets[$oid]; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | return array(); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | /** |
---|
| 453 | * Computes the changes that happened to a single entity. |
---|
| 454 | * |
---|
| 455 | * Modifies/populates the following properties: |
---|
| 456 | * |
---|
| 457 | * {@link _originalEntityData} |
---|
| 458 | * If the entity is NEW or MANAGED but not yet fully persisted (only has an id) |
---|
| 459 | * then it was not fetched from the database and therefore we have no original |
---|
| 460 | * entity data yet. All of the current entity data is stored as the original entity data. |
---|
| 461 | * |
---|
| 462 | * {@link _entityChangeSets} |
---|
| 463 | * The changes detected on all properties of the entity are stored there. |
---|
| 464 | * A change is a tuple array where the first entry is the old value and the second |
---|
| 465 | * entry is the new value of the property. Changesets are used by persisters |
---|
| 466 | * to INSERT/UPDATE the persistent entity state. |
---|
| 467 | * |
---|
| 468 | * {@link _entityUpdates} |
---|
| 469 | * If the entity is already fully MANAGED (has been fetched from the database before) |
---|
| 470 | * and any changes to its properties are detected, then a reference to the entity is stored |
---|
| 471 | * there to mark it for an update. |
---|
| 472 | * |
---|
| 473 | * {@link _collectionDeletions} |
---|
| 474 | * If a PersistentCollection has been de-referenced in a fully MANAGED entity, |
---|
| 475 | * then this collection is marked for deletion. |
---|
| 476 | * |
---|
| 477 | * @ignore |
---|
| 478 | * @internal Don't call from the outside. |
---|
| 479 | * @param ClassMetadata $class The class descriptor of the entity. |
---|
| 480 | * @param object $entity The entity for which to compute the changes. |
---|
| 481 | */ |
---|
| 482 | public function computeChangeSet(ClassMetadata $class, $entity) |
---|
| 483 | { |
---|
| 484 | $oid = spl_object_hash($entity); |
---|
| 485 | |
---|
| 486 | if (isset($this->readOnlyObjects[$oid])) { |
---|
| 487 | return; |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | if ( ! $class->isInheritanceTypeNone()) { |
---|
| 491 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | // Fire PreFlush lifecycle callbacks |
---|
| 495 | if (isset($class->lifecycleCallbacks[Events::preFlush])) { |
---|
| 496 | $class->invokeLifecycleCallbacks(Events::preFlush, $entity); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | $actualData = array(); |
---|
| 500 | |
---|
| 501 | foreach ($class->reflFields as $name => $refProp) { |
---|
| 502 | $value = $refProp->getValue($entity); |
---|
| 503 | |
---|
| 504 | if ($class->isCollectionValuedAssociation($name) && $value !== null && ! ($value instanceof PersistentCollection)) { |
---|
| 505 | // If $value is not a Collection then use an ArrayCollection. |
---|
| 506 | if ( ! $value instanceof Collection) { |
---|
| 507 | $value = new ArrayCollection($value); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | $assoc = $class->associationMappings[$name]; |
---|
| 511 | |
---|
| 512 | // Inject PersistentCollection |
---|
| 513 | $value = new PersistentCollection( |
---|
| 514 | $this->em, $this->em->getClassMetadata($assoc['targetEntity']), $value |
---|
| 515 | ); |
---|
| 516 | $value->setOwner($entity, $assoc); |
---|
| 517 | $value->setDirty( ! $value->isEmpty()); |
---|
| 518 | |
---|
| 519 | $class->reflFields[$name]->setValue($entity, $value); |
---|
| 520 | |
---|
| 521 | $actualData[$name] = $value; |
---|
| 522 | |
---|
| 523 | continue; |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | if (( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) && ($name !== $class->versionField)) { |
---|
| 527 | $actualData[$name] = $value; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | if ( ! isset($this->originalEntityData[$oid])) { |
---|
| 532 | // Entity is either NEW or MANAGED but not yet fully persisted (only has an id). |
---|
| 533 | // These result in an INSERT. |
---|
| 534 | $this->originalEntityData[$oid] = $actualData; |
---|
| 535 | $changeSet = array(); |
---|
| 536 | |
---|
| 537 | foreach ($actualData as $propName => $actualValue) { |
---|
| 538 | if ( ! isset($class->associationMappings[$propName])) { |
---|
| 539 | $changeSet[$propName] = array(null, $actualValue); |
---|
| 540 | |
---|
| 541 | continue; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | $assoc = $class->associationMappings[$propName]; |
---|
| 545 | |
---|
| 546 | if ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE) { |
---|
| 547 | $changeSet[$propName] = array(null, $actualValue); |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | $this->entityChangeSets[$oid] = $changeSet; |
---|
| 552 | } else { |
---|
| 553 | // Entity is "fully" MANAGED: it was already fully persisted before |
---|
| 554 | // and we have a copy of the original data |
---|
| 555 | $originalData = $this->originalEntityData[$oid]; |
---|
| 556 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
---|
| 557 | $changeSet = ($isChangeTrackingNotify && isset($this->entityChangeSets[$oid])) |
---|
| 558 | ? $this->entityChangeSets[$oid] |
---|
| 559 | : array(); |
---|
| 560 | |
---|
| 561 | foreach ($actualData as $propName => $actualValue) { |
---|
| 562 | // skip field, its a partially omitted one! |
---|
| 563 | if ( ! (isset($originalData[$propName]) || array_key_exists($propName, $originalData))) { |
---|
| 564 | continue; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | $orgValue = $originalData[$propName]; |
---|
| 568 | |
---|
| 569 | // skip if value havent changed |
---|
| 570 | if ($orgValue === $actualValue) { |
---|
| 571 | continue; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | // if regular field |
---|
| 575 | if ( ! isset($class->associationMappings[$propName])) { |
---|
| 576 | if ($isChangeTrackingNotify) { |
---|
| 577 | continue; |
---|
| 578 | } |
---|
| 579 | |
---|
| 580 | $changeSet[$propName] = array($orgValue, $actualValue); |
---|
| 581 | |
---|
| 582 | continue; |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | $assoc = $class->associationMappings[$propName]; |
---|
| 586 | |
---|
| 587 | // Persistent collection was exchanged with the "originally" |
---|
| 588 | // created one. This can only mean it was cloned and replaced |
---|
| 589 | // on another entity. |
---|
| 590 | if ($actualValue instanceof PersistentCollection) { |
---|
| 591 | $owner = $actualValue->getOwner(); |
---|
| 592 | if ($owner === null) { // cloned |
---|
| 593 | $actualValue->setOwner($entity, $assoc); |
---|
| 594 | } else if ($owner !== $entity) { // no clone, we have to fix |
---|
| 595 | if (!$actualValue->isInitialized()) { |
---|
| 596 | $actualValue->initialize(); // we have to do this otherwise the cols share state |
---|
| 597 | } |
---|
| 598 | $newValue = clone $actualValue; |
---|
| 599 | $newValue->setOwner($entity, $assoc); |
---|
| 600 | $class->reflFields[$propName]->setValue($entity, $newValue); |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | if ($orgValue instanceof PersistentCollection) { |
---|
| 605 | // A PersistentCollection was de-referenced, so delete it. |
---|
| 606 | $coid = spl_object_hash($orgValue); |
---|
| 607 | |
---|
| 608 | if (isset($this->collectionDeletions[$coid])) { |
---|
| 609 | continue; |
---|
| 610 | } |
---|
| 611 | |
---|
| 612 | $this->collectionDeletions[$coid] = $orgValue; |
---|
| 613 | $changeSet[$propName] = $orgValue; // Signal changeset, to-many assocs will be ignored. |
---|
| 614 | |
---|
| 615 | continue; |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | if ($assoc['type'] & ClassMetadata::TO_ONE) { |
---|
| 619 | if ($assoc['isOwningSide']) { |
---|
| 620 | $changeSet[$propName] = array($orgValue, $actualValue); |
---|
| 621 | } |
---|
| 622 | |
---|
| 623 | if ($orgValue !== null && $assoc['orphanRemoval']) { |
---|
| 624 | $this->scheduleOrphanRemoval($orgValue); |
---|
| 625 | } |
---|
| 626 | } |
---|
| 627 | } |
---|
| 628 | |
---|
| 629 | if ($changeSet) { |
---|
| 630 | $this->entityChangeSets[$oid] = $changeSet; |
---|
| 631 | $this->originalEntityData[$oid] = $actualData; |
---|
| 632 | $this->entityUpdates[$oid] = $entity; |
---|
| 633 | } |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | // Look for changes in associations of the entity |
---|
| 637 | foreach ($class->associationMappings as $field => $assoc) { |
---|
| 638 | if (($val = $class->reflFields[$field]->getValue($entity)) !== null) { |
---|
| 639 | $this->computeAssociationChanges($assoc, $val); |
---|
| 640 | if (!isset($this->entityChangeSets[$oid]) && |
---|
| 641 | $assoc['isOwningSide'] && |
---|
| 642 | $assoc['type'] == ClassMetadata::MANY_TO_MANY && |
---|
| 643 | $val instanceof PersistentCollection && |
---|
| 644 | $val->isDirty()) { |
---|
| 645 | $this->entityChangeSets[$oid] = array(); |
---|
| 646 | $this->originalEntityData[$oid] = $actualData; |
---|
| 647 | $this->entityUpdates[$oid] = $entity; |
---|
| 648 | } |
---|
| 649 | } |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | /** |
---|
| 654 | * Computes all the changes that have been done to entities and collections |
---|
| 655 | * since the last commit and stores these changes in the _entityChangeSet map |
---|
| 656 | * temporarily for access by the persisters, until the UoW commit is finished. |
---|
| 657 | */ |
---|
| 658 | public function computeChangeSets() |
---|
| 659 | { |
---|
| 660 | // Compute changes for INSERTed entities first. This must always happen. |
---|
| 661 | $this->computeScheduleInsertsChangeSets(); |
---|
| 662 | |
---|
| 663 | // Compute changes for other MANAGED entities. Change tracking policies take effect here. |
---|
| 664 | foreach ($this->identityMap as $className => $entities) { |
---|
| 665 | $class = $this->em->getClassMetadata($className); |
---|
| 666 | |
---|
| 667 | // Skip class if instances are read-only |
---|
| 668 | if ($class->isReadOnly) { |
---|
| 669 | continue; |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | // If change tracking is explicit or happens through notification, then only compute |
---|
| 673 | // changes on entities of that type that are explicitly marked for synchronization. |
---|
| 674 | switch (true) { |
---|
| 675 | case ($class->isChangeTrackingDeferredImplicit()): |
---|
| 676 | $entitiesToProcess = $entities; |
---|
| 677 | break; |
---|
| 678 | |
---|
| 679 | case (isset($this->scheduledForDirtyCheck[$className])): |
---|
| 680 | $entitiesToProcess = $this->scheduledForDirtyCheck[$className]; |
---|
| 681 | break; |
---|
| 682 | |
---|
| 683 | default: |
---|
| 684 | $entitiesToProcess = array(); |
---|
| 685 | |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | foreach ($entitiesToProcess as $entity) { |
---|
| 689 | // Ignore uninitialized proxy objects |
---|
| 690 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
---|
| 691 | continue; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | // Only MANAGED entities that are NOT SCHEDULED FOR INSERTION are processed here. |
---|
| 695 | $oid = spl_object_hash($entity); |
---|
| 696 | |
---|
| 697 | if ( ! isset($this->entityInsertions[$oid]) && isset($this->entityStates[$oid])) { |
---|
| 698 | $this->computeChangeSet($class, $entity); |
---|
| 699 | } |
---|
| 700 | } |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | /** |
---|
| 705 | * Computes the changes of an association. |
---|
| 706 | * |
---|
| 707 | * @param AssociationMapping $assoc |
---|
| 708 | * @param mixed $value The value of the association. |
---|
| 709 | */ |
---|
| 710 | private function computeAssociationChanges($assoc, $value) |
---|
| 711 | { |
---|
| 712 | if ($value instanceof Proxy && ! $value->__isInitialized__) { |
---|
| 713 | return; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | if ($value instanceof PersistentCollection && $value->isDirty()) { |
---|
| 717 | $coid = spl_object_hash($value); |
---|
| 718 | |
---|
| 719 | if ($assoc['isOwningSide']) { |
---|
| 720 | $this->collectionUpdates[$coid] = $value; |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | $this->visitedCollections[$coid] = $value; |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | // Look through the entities, and in any of their associations, |
---|
| 727 | // for transient (new) entities, recursively. ("Persistence by reachability") |
---|
| 728 | // Unwrap. Uninitialized collections will simply be empty. |
---|
| 729 | $unwrappedValue = ($assoc['type'] & ClassMetadata::TO_ONE) ? array($value) : $value->unwrap(); |
---|
| 730 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
---|
| 731 | |
---|
| 732 | foreach ($unwrappedValue as $key => $entry) { |
---|
| 733 | $state = $this->getEntityState($entry, self::STATE_NEW); |
---|
| 734 | $oid = spl_object_hash($entry); |
---|
| 735 | |
---|
| 736 | if (!($entry instanceof $assoc['targetEntity'])) { |
---|
| 737 | throw new ORMException(sprintf("Found entity of type %s on association %s#%s, but expecting %s", |
---|
| 738 | get_class($entry), |
---|
| 739 | $assoc['sourceEntity'], |
---|
| 740 | $assoc['fieldName'], |
---|
| 741 | $targetClass->name |
---|
| 742 | )); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | switch ($state) { |
---|
| 746 | case self::STATE_NEW: |
---|
| 747 | if ( ! $assoc['isCascadePersist']) { |
---|
| 748 | throw ORMInvalidArgumentException::newEntityFoundThroughRelationship($assoc, $entry); |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | $this->persistNew($targetClass, $entry); |
---|
| 752 | $this->computeChangeSet($targetClass, $entry); |
---|
| 753 | break; |
---|
| 754 | |
---|
| 755 | case self::STATE_REMOVED: |
---|
| 756 | // Consume the $value as array (it's either an array or an ArrayAccess) |
---|
| 757 | // and remove the element from Collection. |
---|
| 758 | if ($assoc['type'] & ClassMetadata::TO_MANY) { |
---|
| 759 | unset($value[$key]); |
---|
| 760 | } |
---|
| 761 | break; |
---|
| 762 | |
---|
| 763 | case self::STATE_DETACHED: |
---|
| 764 | // Can actually not happen right now as we assume STATE_NEW, |
---|
| 765 | // so the exception will be raised from the DBAL layer (constraint violation). |
---|
| 766 | throw ORMInvalidArgumentException::detachedEntityFoundThroughRelationship($assoc, $entry); |
---|
| 767 | break; |
---|
| 768 | |
---|
| 769 | default: |
---|
| 770 | // MANAGED associated entities are already taken into account |
---|
| 771 | // during changeset calculation anyway, since they are in the identity map. |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | private function persistNew($class, $entity) |
---|
| 777 | { |
---|
| 778 | $oid = spl_object_hash($entity); |
---|
| 779 | |
---|
| 780 | if (isset($class->lifecycleCallbacks[Events::prePersist])) { |
---|
| 781 | $class->invokeLifecycleCallbacks(Events::prePersist, $entity); |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | if ($this->evm->hasListeners(Events::prePersist)) { |
---|
| 785 | $this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity, $this->em)); |
---|
| 786 | } |
---|
| 787 | |
---|
| 788 | $idGen = $class->idGenerator; |
---|
| 789 | |
---|
| 790 | if ( ! $idGen->isPostInsertGenerator()) { |
---|
| 791 | $idValue = $idGen->generate($this->em, $entity); |
---|
| 792 | |
---|
| 793 | if ( ! $idGen instanceof \Doctrine\ORM\Id\AssignedGenerator) { |
---|
| 794 | $idValue = array($class->identifier[0] => $idValue); |
---|
| 795 | |
---|
| 796 | $class->setIdentifierValues($entity, $idValue); |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | $this->entityIdentifiers[$oid] = $idValue; |
---|
| 800 | } |
---|
| 801 | |
---|
| 802 | $this->entityStates[$oid] = self::STATE_MANAGED; |
---|
| 803 | |
---|
| 804 | $this->scheduleForInsert($entity); |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | /** |
---|
| 808 | * INTERNAL: |
---|
| 809 | * Computes the changeset of an individual entity, independently of the |
---|
| 810 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
---|
| 811 | * |
---|
| 812 | * The passed entity must be a managed entity. If the entity already has a change set |
---|
| 813 | * because this method is invoked during a commit cycle then the change sets are added. |
---|
| 814 | * whereby changes detected in this method prevail. |
---|
| 815 | * |
---|
| 816 | * @ignore |
---|
| 817 | * @param ClassMetadata $class The class descriptor of the entity. |
---|
| 818 | * @param object $entity The entity for which to (re)calculate the change set. |
---|
| 819 | * @throws InvalidArgumentException If the passed entity is not MANAGED. |
---|
| 820 | */ |
---|
| 821 | public function recomputeSingleEntityChangeSet(ClassMetadata $class, $entity) |
---|
| 822 | { |
---|
| 823 | $oid = spl_object_hash($entity); |
---|
| 824 | |
---|
| 825 | if ( ! isset($this->entityStates[$oid]) || $this->entityStates[$oid] != self::STATE_MANAGED) { |
---|
| 826 | throw ORMInvalidArgumentException::entityNotManaged($entity); |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | // skip if change tracking is "NOTIFY" |
---|
| 830 | if ($class->isChangeTrackingNotify()) { |
---|
| 831 | return; |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | if ( ! $class->isInheritanceTypeNone()) { |
---|
| 835 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 836 | } |
---|
| 837 | |
---|
| 838 | $actualData = array(); |
---|
| 839 | |
---|
| 840 | foreach ($class->reflFields as $name => $refProp) { |
---|
| 841 | if ( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity()) { |
---|
| 842 | $actualData[$name] = $refProp->getValue($entity); |
---|
| 843 | } |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | $originalData = $this->originalEntityData[$oid]; |
---|
| 847 | $changeSet = array(); |
---|
| 848 | |
---|
| 849 | foreach ($actualData as $propName => $actualValue) { |
---|
| 850 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; |
---|
| 851 | |
---|
| 852 | if (is_object($orgValue) && $orgValue !== $actualValue) { |
---|
| 853 | $changeSet[$propName] = array($orgValue, $actualValue); |
---|
| 854 | } else if ($orgValue != $actualValue || ($orgValue === null ^ $actualValue === null)) { |
---|
| 855 | $changeSet[$propName] = array($orgValue, $actualValue); |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | if ($changeSet) { |
---|
| 860 | if (isset($this->entityChangeSets[$oid])) { |
---|
| 861 | $this->entityChangeSets[$oid] = array_merge($this->entityChangeSets[$oid], $changeSet); |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | $this->originalEntityData[$oid] = $actualData; |
---|
| 865 | } |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /** |
---|
| 869 | * Executes all entity insertions for entities of the specified type. |
---|
| 870 | * |
---|
| 871 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
---|
| 872 | */ |
---|
| 873 | private function executeInserts($class) |
---|
| 874 | { |
---|
| 875 | $className = $class->name; |
---|
| 876 | $persister = $this->getEntityPersister($className); |
---|
| 877 | $entities = array(); |
---|
| 878 | |
---|
| 879 | $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]); |
---|
| 880 | $hasListeners = $this->evm->hasListeners(Events::postPersist); |
---|
| 881 | |
---|
| 882 | foreach ($this->entityInsertions as $oid => $entity) { |
---|
| 883 | if (get_class($entity) !== $className) { |
---|
| 884 | continue; |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | $persister->addInsert($entity); |
---|
| 888 | |
---|
| 889 | unset($this->entityInsertions[$oid]); |
---|
| 890 | |
---|
| 891 | if ($hasLifecycleCallbacks || $hasListeners) { |
---|
| 892 | $entities[] = $entity; |
---|
| 893 | } |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | $postInsertIds = $persister->executeInserts(); |
---|
| 897 | |
---|
| 898 | if ($postInsertIds) { |
---|
| 899 | // Persister returned post-insert IDs |
---|
| 900 | foreach ($postInsertIds as $id => $entity) { |
---|
| 901 | $oid = spl_object_hash($entity); |
---|
| 902 | $idField = $class->identifier[0]; |
---|
| 903 | |
---|
| 904 | $class->reflFields[$idField]->setValue($entity, $id); |
---|
| 905 | |
---|
| 906 | $this->entityIdentifiers[$oid] = array($idField => $id); |
---|
| 907 | $this->entityStates[$oid] = self::STATE_MANAGED; |
---|
| 908 | $this->originalEntityData[$oid][$idField] = $id; |
---|
| 909 | |
---|
| 910 | $this->addToIdentityMap($entity); |
---|
| 911 | } |
---|
| 912 | } |
---|
| 913 | |
---|
| 914 | foreach ($entities as $entity) { |
---|
| 915 | if ($hasLifecycleCallbacks) { |
---|
| 916 | $class->invokeLifecycleCallbacks(Events::postPersist, $entity); |
---|
| 917 | } |
---|
| 918 | |
---|
| 919 | if ($hasListeners) { |
---|
| 920 | $this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity, $this->em)); |
---|
| 921 | } |
---|
| 922 | } |
---|
| 923 | } |
---|
| 924 | |
---|
| 925 | /** |
---|
| 926 | * Executes all entity updates for entities of the specified type. |
---|
| 927 | * |
---|
| 928 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
---|
| 929 | */ |
---|
| 930 | private function executeUpdates($class) |
---|
| 931 | { |
---|
| 932 | $className = $class->name; |
---|
| 933 | $persister = $this->getEntityPersister($className); |
---|
| 934 | |
---|
| 935 | $hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]); |
---|
| 936 | $hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate); |
---|
| 937 | |
---|
| 938 | $hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]); |
---|
| 939 | $hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate); |
---|
| 940 | |
---|
| 941 | foreach ($this->entityUpdates as $oid => $entity) { |
---|
| 942 | if ( ! (get_class($entity) === $className || $entity instanceof Proxy && get_parent_class($entity) === $className)) { |
---|
| 943 | continue; |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | if ($hasPreUpdateLifecycleCallbacks) { |
---|
| 947 | $class->invokeLifecycleCallbacks(Events::preUpdate, $entity); |
---|
| 948 | |
---|
| 949 | $this->recomputeSingleEntityChangeSet($class, $entity); |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | if ($hasPreUpdateListeners) { |
---|
| 953 | $this->evm->dispatchEvent( |
---|
| 954 | Events::preUpdate, |
---|
| 955 | new Event\PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid]) |
---|
| 956 | ); |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | if ($this->entityChangeSets[$oid]) { |
---|
| 960 | $persister->update($entity); |
---|
| 961 | } |
---|
| 962 | |
---|
| 963 | unset($this->entityUpdates[$oid]); |
---|
| 964 | |
---|
| 965 | if ($hasPostUpdateLifecycleCallbacks) { |
---|
| 966 | $class->invokeLifecycleCallbacks(Events::postUpdate, $entity); |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | if ($hasPostUpdateListeners) { |
---|
| 970 | $this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($entity, $this->em)); |
---|
| 971 | } |
---|
| 972 | } |
---|
| 973 | } |
---|
| 974 | |
---|
| 975 | /** |
---|
| 976 | * Executes all entity deletions for entities of the specified type. |
---|
| 977 | * |
---|
| 978 | * @param \Doctrine\ORM\Mapping\ClassMetadata $class |
---|
| 979 | */ |
---|
| 980 | private function executeDeletions($class) |
---|
| 981 | { |
---|
| 982 | $className = $class->name; |
---|
| 983 | $persister = $this->getEntityPersister($className); |
---|
| 984 | |
---|
| 985 | $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]); |
---|
| 986 | $hasListeners = $this->evm->hasListeners(Events::postRemove); |
---|
| 987 | |
---|
| 988 | foreach ($this->entityDeletions as $oid => $entity) { |
---|
| 989 | if ( ! (get_class($entity) == $className || $entity instanceof Proxy && get_parent_class($entity) == $className)) { |
---|
| 990 | continue; |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | $persister->delete($entity); |
---|
| 994 | |
---|
| 995 | unset( |
---|
| 996 | $this->entityDeletions[$oid], |
---|
| 997 | $this->entityIdentifiers[$oid], |
---|
| 998 | $this->originalEntityData[$oid], |
---|
| 999 | $this->entityStates[$oid] |
---|
| 1000 | ); |
---|
| 1001 | |
---|
| 1002 | // Entity with this $oid after deletion treated as NEW, even if the $oid |
---|
| 1003 | // is obtained by a new entity because the old one went out of scope. |
---|
| 1004 | //$this->entityStates[$oid] = self::STATE_NEW; |
---|
| 1005 | if ( ! $class->isIdentifierNatural()) { |
---|
| 1006 | $class->reflFields[$class->identifier[0]]->setValue($entity, null); |
---|
| 1007 | } |
---|
| 1008 | |
---|
| 1009 | if ($hasLifecycleCallbacks) { |
---|
| 1010 | $class->invokeLifecycleCallbacks(Events::postRemove, $entity); |
---|
| 1011 | } |
---|
| 1012 | |
---|
| 1013 | if ($hasListeners) { |
---|
| 1014 | $this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity, $this->em)); |
---|
| 1015 | } |
---|
| 1016 | } |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | /** |
---|
| 1020 | * Gets the commit order. |
---|
| 1021 | * |
---|
| 1022 | * @return array |
---|
| 1023 | */ |
---|
| 1024 | private function getCommitOrder(array $entityChangeSet = null) |
---|
| 1025 | { |
---|
| 1026 | if ($entityChangeSet === null) { |
---|
| 1027 | $entityChangeSet = array_merge($this->entityInsertions, $this->entityUpdates, $this->entityDeletions); |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | $calc = $this->getCommitOrderCalculator(); |
---|
| 1031 | |
---|
| 1032 | // See if there are any new classes in the changeset, that are not in the |
---|
| 1033 | // commit order graph yet (dont have a node). |
---|
| 1034 | // We have to inspect changeSet to be able to correctly build dependencies. |
---|
| 1035 | // It is not possible to use IdentityMap here because post inserted ids |
---|
| 1036 | // are not yet available. |
---|
| 1037 | $newNodes = array(); |
---|
| 1038 | |
---|
| 1039 | foreach ($entityChangeSet as $oid => $entity) { |
---|
| 1040 | $className = get_class($entity); |
---|
| 1041 | |
---|
| 1042 | if ($calc->hasClass($className)) { |
---|
| 1043 | continue; |
---|
| 1044 | } |
---|
| 1045 | |
---|
| 1046 | $class = $this->em->getClassMetadata($className); |
---|
| 1047 | $calc->addClass($class); |
---|
| 1048 | |
---|
| 1049 | $newNodes[] = $class; |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | // Calculate dependencies for new nodes |
---|
| 1053 | while ($class = array_pop($newNodes)) { |
---|
| 1054 | foreach ($class->associationMappings as $assoc) { |
---|
| 1055 | if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { |
---|
| 1056 | continue; |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
---|
| 1060 | |
---|
| 1061 | if ( ! $calc->hasClass($targetClass->name)) { |
---|
| 1062 | $calc->addClass($targetClass); |
---|
| 1063 | |
---|
| 1064 | $newNodes[] = $targetClass; |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | $calc->addDependency($targetClass, $class); |
---|
| 1068 | |
---|
| 1069 | // If the target class has mapped subclasses, these share the same dependency. |
---|
| 1070 | if ( ! $targetClass->subClasses) { |
---|
| 1071 | continue; |
---|
| 1072 | } |
---|
| 1073 | |
---|
| 1074 | foreach ($targetClass->subClasses as $subClassName) { |
---|
| 1075 | $targetSubClass = $this->em->getClassMetadata($subClassName); |
---|
| 1076 | |
---|
| 1077 | if ( ! $calc->hasClass($subClassName)) { |
---|
| 1078 | $calc->addClass($targetSubClass); |
---|
| 1079 | |
---|
| 1080 | $newNodes[] = $targetSubClass; |
---|
| 1081 | } |
---|
| 1082 | |
---|
| 1083 | $calc->addDependency($targetSubClass, $class); |
---|
| 1084 | } |
---|
| 1085 | } |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | return $calc->getCommitOrder(); |
---|
| 1089 | } |
---|
| 1090 | |
---|
| 1091 | /** |
---|
| 1092 | * Schedules an entity for insertion into the database. |
---|
| 1093 | * If the entity already has an identifier, it will be added to the identity map. |
---|
| 1094 | * |
---|
| 1095 | * @param object $entity The entity to schedule for insertion. |
---|
| 1096 | */ |
---|
| 1097 | public function scheduleForInsert($entity) |
---|
| 1098 | { |
---|
| 1099 | $oid = spl_object_hash($entity); |
---|
| 1100 | |
---|
| 1101 | if (isset($this->entityUpdates[$oid])) { |
---|
| 1102 | throw new InvalidArgumentException("Dirty entity can not be scheduled for insertion."); |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | if (isset($this->entityDeletions[$oid])) { |
---|
| 1106 | throw ORMInvalidArgumentException::scheduleInsertForRemovedEntity($entity); |
---|
| 1107 | } |
---|
| 1108 | if (isset($this->originalEntityData[$oid]) && ! isset($this->entityInsertions[$oid])) { |
---|
| 1109 | throw ORMInvalidArgumentException::scheduleInsertForManagedEntity($entity); |
---|
| 1110 | } |
---|
| 1111 | |
---|
| 1112 | if (isset($this->entityInsertions[$oid])) { |
---|
| 1113 | throw ORMInvalidArgumentException::scheduleInsertTwice($entity); |
---|
| 1114 | } |
---|
| 1115 | |
---|
| 1116 | $this->entityInsertions[$oid] = $entity; |
---|
| 1117 | |
---|
| 1118 | if (isset($this->entityIdentifiers[$oid])) { |
---|
| 1119 | $this->addToIdentityMap($entity); |
---|
| 1120 | } |
---|
| 1121 | |
---|
| 1122 | if ($entity instanceof NotifyPropertyChanged) { |
---|
| 1123 | $entity->addPropertyChangedListener($this); |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | |
---|
| 1127 | /** |
---|
| 1128 | * Checks whether an entity is scheduled for insertion. |
---|
| 1129 | * |
---|
| 1130 | * @param object $entity |
---|
| 1131 | * @return boolean |
---|
| 1132 | */ |
---|
| 1133 | public function isScheduledForInsert($entity) |
---|
| 1134 | { |
---|
| 1135 | return isset($this->entityInsertions[spl_object_hash($entity)]); |
---|
| 1136 | } |
---|
| 1137 | |
---|
| 1138 | /** |
---|
| 1139 | * Schedules an entity for being updated. |
---|
| 1140 | * |
---|
| 1141 | * @param object $entity The entity to schedule for being updated. |
---|
| 1142 | */ |
---|
| 1143 | public function scheduleForUpdate($entity) |
---|
| 1144 | { |
---|
| 1145 | $oid = spl_object_hash($entity); |
---|
| 1146 | |
---|
| 1147 | if ( ! isset($this->entityIdentifiers[$oid])) { |
---|
| 1148 | throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "scheduling for update"); |
---|
| 1149 | } |
---|
| 1150 | |
---|
| 1151 | if (isset($this->entityDeletions[$oid])) { |
---|
| 1152 | throw ORMInvalidArgumentException::entityIsRemoved($entity, "schedule for update"); |
---|
| 1153 | } |
---|
| 1154 | |
---|
| 1155 | if ( ! isset($this->entityUpdates[$oid]) && ! isset($this->entityInsertions[$oid])) { |
---|
| 1156 | $this->entityUpdates[$oid] = $entity; |
---|
| 1157 | } |
---|
| 1158 | } |
---|
| 1159 | |
---|
| 1160 | /** |
---|
| 1161 | * INTERNAL: |
---|
| 1162 | * Schedules an extra update that will be executed immediately after the |
---|
| 1163 | * regular entity updates within the currently running commit cycle. |
---|
| 1164 | * |
---|
| 1165 | * Extra updates for entities are stored as (entity, changeset) tuples. |
---|
| 1166 | * |
---|
| 1167 | * @ignore |
---|
| 1168 | * @param object $entity The entity for which to schedule an extra update. |
---|
| 1169 | * @param array $changeset The changeset of the entity (what to update). |
---|
| 1170 | */ |
---|
| 1171 | public function scheduleExtraUpdate($entity, array $changeset) |
---|
| 1172 | { |
---|
| 1173 | $oid = spl_object_hash($entity); |
---|
| 1174 | $extraUpdate = array($entity, $changeset); |
---|
| 1175 | |
---|
| 1176 | if (isset($this->extraUpdates[$oid])) { |
---|
| 1177 | list($ignored, $changeset2) = $this->extraUpdates[$oid]; |
---|
| 1178 | |
---|
| 1179 | $extraUpdate = array($entity, $changeset + $changeset2); |
---|
| 1180 | } |
---|
| 1181 | |
---|
| 1182 | $this->extraUpdates[$oid] = $extraUpdate; |
---|
| 1183 | } |
---|
| 1184 | |
---|
| 1185 | /** |
---|
| 1186 | * Checks whether an entity is registered as dirty in the unit of work. |
---|
| 1187 | * Note: Is not very useful currently as dirty entities are only registered |
---|
| 1188 | * at commit time. |
---|
| 1189 | * |
---|
| 1190 | * @param object $entity |
---|
| 1191 | * @return boolean |
---|
| 1192 | */ |
---|
| 1193 | public function isScheduledForUpdate($entity) |
---|
| 1194 | { |
---|
| 1195 | return isset($this->entityUpdates[spl_object_hash($entity)]); |
---|
| 1196 | } |
---|
| 1197 | |
---|
| 1198 | |
---|
| 1199 | /** |
---|
| 1200 | * Checks whether an entity is registered to be checked in the unit of work. |
---|
| 1201 | * |
---|
| 1202 | * @param object $entity |
---|
| 1203 | * @return boolean |
---|
| 1204 | */ |
---|
| 1205 | public function isScheduledForDirtyCheck($entity) |
---|
| 1206 | { |
---|
| 1207 | $rootEntityName = $this->em->getClassMetadata(get_class($entity))->rootEntityName; |
---|
| 1208 | |
---|
| 1209 | return isset($this->scheduledForDirtyCheck[$rootEntityName][spl_object_hash($entity)]); |
---|
| 1210 | } |
---|
| 1211 | |
---|
| 1212 | /** |
---|
| 1213 | * INTERNAL: |
---|
| 1214 | * Schedules an entity for deletion. |
---|
| 1215 | * |
---|
| 1216 | * @param object $entity |
---|
| 1217 | */ |
---|
| 1218 | public function scheduleForDelete($entity) |
---|
| 1219 | { |
---|
| 1220 | $oid = spl_object_hash($entity); |
---|
| 1221 | |
---|
| 1222 | if (isset($this->entityInsertions[$oid])) { |
---|
| 1223 | if ($this->isInIdentityMap($entity)) { |
---|
| 1224 | $this->removeFromIdentityMap($entity); |
---|
| 1225 | } |
---|
| 1226 | |
---|
| 1227 | unset($this->entityInsertions[$oid], $this->entityStates[$oid]); |
---|
| 1228 | |
---|
| 1229 | return; // entity has not been persisted yet, so nothing more to do. |
---|
| 1230 | } |
---|
| 1231 | |
---|
| 1232 | if ( ! $this->isInIdentityMap($entity)) { |
---|
| 1233 | return; |
---|
| 1234 | } |
---|
| 1235 | |
---|
| 1236 | $this->removeFromIdentityMap($entity); |
---|
| 1237 | |
---|
| 1238 | if (isset($this->entityUpdates[$oid])) { |
---|
| 1239 | unset($this->entityUpdates[$oid]); |
---|
| 1240 | } |
---|
| 1241 | |
---|
| 1242 | if ( ! isset($this->entityDeletions[$oid])) { |
---|
| 1243 | $this->entityDeletions[$oid] = $entity; |
---|
| 1244 | $this->entityStates[$oid] = self::STATE_REMOVED; |
---|
| 1245 | } |
---|
| 1246 | } |
---|
| 1247 | |
---|
| 1248 | /** |
---|
| 1249 | * Checks whether an entity is registered as removed/deleted with the unit |
---|
| 1250 | * of work. |
---|
| 1251 | * |
---|
| 1252 | * @param object $entity |
---|
| 1253 | * @return boolean |
---|
| 1254 | */ |
---|
| 1255 | public function isScheduledForDelete($entity) |
---|
| 1256 | { |
---|
| 1257 | return isset($this->entityDeletions[spl_object_hash($entity)]); |
---|
| 1258 | } |
---|
| 1259 | |
---|
| 1260 | /** |
---|
| 1261 | * Checks whether an entity is scheduled for insertion, update or deletion. |
---|
| 1262 | * |
---|
| 1263 | * @param $entity |
---|
| 1264 | * @return boolean |
---|
| 1265 | */ |
---|
| 1266 | public function isEntityScheduled($entity) |
---|
| 1267 | { |
---|
| 1268 | $oid = spl_object_hash($entity); |
---|
| 1269 | |
---|
| 1270 | return isset($this->entityInsertions[$oid]) |
---|
| 1271 | || isset($this->entityUpdates[$oid]) |
---|
| 1272 | || isset($this->entityDeletions[$oid]); |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | /** |
---|
| 1276 | * INTERNAL: |
---|
| 1277 | * Registers an entity in the identity map. |
---|
| 1278 | * Note that entities in a hierarchy are registered with the class name of |
---|
| 1279 | * the root entity. |
---|
| 1280 | * |
---|
| 1281 | * @ignore |
---|
| 1282 | * @param object $entity The entity to register. |
---|
| 1283 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
---|
| 1284 | * the entity in question is already managed. |
---|
| 1285 | */ |
---|
| 1286 | public function addToIdentityMap($entity) |
---|
| 1287 | { |
---|
| 1288 | $classMetadata = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1289 | $idHash = implode(' ', $this->entityIdentifiers[spl_object_hash($entity)]); |
---|
| 1290 | |
---|
| 1291 | if ($idHash === '') { |
---|
| 1292 | throw ORMInvalidArgumentException::entityWithoutIdentity($classMetadata->name, $entity); |
---|
| 1293 | } |
---|
| 1294 | |
---|
| 1295 | $className = $classMetadata->rootEntityName; |
---|
| 1296 | |
---|
| 1297 | if (isset($this->identityMap[$className][$idHash])) { |
---|
| 1298 | return false; |
---|
| 1299 | } |
---|
| 1300 | |
---|
| 1301 | $this->identityMap[$className][$idHash] = $entity; |
---|
| 1302 | |
---|
| 1303 | return true; |
---|
| 1304 | } |
---|
| 1305 | |
---|
| 1306 | /** |
---|
| 1307 | * Gets the state of an entity with regard to the current unit of work. |
---|
| 1308 | * |
---|
| 1309 | * @param object $entity |
---|
| 1310 | * @param integer $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
---|
| 1311 | * This parameter can be set to improve performance of entity state detection |
---|
| 1312 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
---|
| 1313 | * is either known or does not matter for the caller of the method. |
---|
| 1314 | * @return int The entity state. |
---|
| 1315 | */ |
---|
| 1316 | public function getEntityState($entity, $assume = null) |
---|
| 1317 | { |
---|
| 1318 | $oid = spl_object_hash($entity); |
---|
| 1319 | |
---|
| 1320 | if (isset($this->entityStates[$oid])) { |
---|
| 1321 | return $this->entityStates[$oid]; |
---|
| 1322 | } |
---|
| 1323 | |
---|
| 1324 | if ($assume !== null) { |
---|
| 1325 | return $assume; |
---|
| 1326 | } |
---|
| 1327 | |
---|
| 1328 | // State can only be NEW or DETACHED, because MANAGED/REMOVED states are known. |
---|
| 1329 | // Note that you can not remember the NEW or DETACHED state in _entityStates since |
---|
| 1330 | // the UoW does not hold references to such objects and the object hash can be reused. |
---|
| 1331 | // More generally because the state may "change" between NEW/DETACHED without the UoW being aware of it. |
---|
| 1332 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1333 | $id = $class->getIdentifierValues($entity); |
---|
| 1334 | |
---|
| 1335 | if ( ! $id) { |
---|
| 1336 | return self::STATE_NEW; |
---|
| 1337 | } |
---|
| 1338 | |
---|
| 1339 | switch (true) { |
---|
| 1340 | case ($class->isIdentifierNatural()); |
---|
| 1341 | // Check for a version field, if available, to avoid a db lookup. |
---|
| 1342 | if ($class->isVersioned) { |
---|
| 1343 | return ($class->getFieldValue($entity, $class->versionField)) |
---|
| 1344 | ? self::STATE_DETACHED |
---|
| 1345 | : self::STATE_NEW; |
---|
| 1346 | } |
---|
| 1347 | |
---|
| 1348 | // Last try before db lookup: check the identity map. |
---|
| 1349 | if ($this->tryGetById($id, $class->rootEntityName)) { |
---|
| 1350 | return self::STATE_DETACHED; |
---|
| 1351 | } |
---|
| 1352 | |
---|
| 1353 | // db lookup |
---|
| 1354 | if ($this->getEntityPersister(get_class($entity))->exists($entity)) { |
---|
| 1355 | return self::STATE_DETACHED; |
---|
| 1356 | } |
---|
| 1357 | |
---|
| 1358 | return self::STATE_NEW; |
---|
| 1359 | |
---|
| 1360 | case ( ! $class->idGenerator->isPostInsertGenerator()): |
---|
| 1361 | // if we have a pre insert generator we can't be sure that having an id |
---|
| 1362 | // really means that the entity exists. We have to verify this through |
---|
| 1363 | // the last resort: a db lookup |
---|
| 1364 | |
---|
| 1365 | // Last try before db lookup: check the identity map. |
---|
| 1366 | if ($this->tryGetById($id, $class->rootEntityName)) { |
---|
| 1367 | return self::STATE_DETACHED; |
---|
| 1368 | } |
---|
| 1369 | |
---|
| 1370 | // db lookup |
---|
| 1371 | if ($this->getEntityPersister(get_class($entity))->exists($entity)) { |
---|
| 1372 | return self::STATE_DETACHED; |
---|
| 1373 | } |
---|
| 1374 | |
---|
| 1375 | return self::STATE_NEW; |
---|
| 1376 | |
---|
| 1377 | default: |
---|
| 1378 | return self::STATE_DETACHED; |
---|
| 1379 | } |
---|
| 1380 | } |
---|
| 1381 | |
---|
| 1382 | /** |
---|
| 1383 | * INTERNAL: |
---|
| 1384 | * Removes an entity from the identity map. This effectively detaches the |
---|
| 1385 | * entity from the persistence management of Doctrine. |
---|
| 1386 | * |
---|
| 1387 | * @ignore |
---|
| 1388 | * @param object $entity |
---|
| 1389 | * @return boolean |
---|
| 1390 | */ |
---|
| 1391 | public function removeFromIdentityMap($entity) |
---|
| 1392 | { |
---|
| 1393 | $oid = spl_object_hash($entity); |
---|
| 1394 | $classMetadata = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1395 | $idHash = implode(' ', $this->entityIdentifiers[$oid]); |
---|
| 1396 | |
---|
| 1397 | if ($idHash === '') { |
---|
| 1398 | throw ORMInvalidArgumentException::entityHasNoIdentity($entity, "remove from identity map"); |
---|
| 1399 | } |
---|
| 1400 | |
---|
| 1401 | $className = $classMetadata->rootEntityName; |
---|
| 1402 | |
---|
| 1403 | if (isset($this->identityMap[$className][$idHash])) { |
---|
| 1404 | unset($this->identityMap[$className][$idHash]); |
---|
| 1405 | unset($this->readOnlyObjects[$oid]); |
---|
| 1406 | |
---|
| 1407 | //$this->entityStates[$oid] = self::STATE_DETACHED; |
---|
| 1408 | |
---|
| 1409 | return true; |
---|
| 1410 | } |
---|
| 1411 | |
---|
| 1412 | return false; |
---|
| 1413 | } |
---|
| 1414 | |
---|
| 1415 | /** |
---|
| 1416 | * INTERNAL: |
---|
| 1417 | * Gets an entity in the identity map by its identifier hash. |
---|
| 1418 | * |
---|
| 1419 | * @ignore |
---|
| 1420 | * @param string $idHash |
---|
| 1421 | * @param string $rootClassName |
---|
| 1422 | * @return object |
---|
| 1423 | */ |
---|
| 1424 | public function getByIdHash($idHash, $rootClassName) |
---|
| 1425 | { |
---|
| 1426 | return $this->identityMap[$rootClassName][$idHash]; |
---|
| 1427 | } |
---|
| 1428 | |
---|
| 1429 | /** |
---|
| 1430 | * INTERNAL: |
---|
| 1431 | * Tries to get an entity by its identifier hash. If no entity is found for |
---|
| 1432 | * the given hash, FALSE is returned. |
---|
| 1433 | * |
---|
| 1434 | * @ignore |
---|
| 1435 | * @param string $idHash |
---|
| 1436 | * @param string $rootClassName |
---|
| 1437 | * @return mixed The found entity or FALSE. |
---|
| 1438 | */ |
---|
| 1439 | public function tryGetByIdHash($idHash, $rootClassName) |
---|
| 1440 | { |
---|
| 1441 | if (isset($this->identityMap[$rootClassName][$idHash])) { |
---|
| 1442 | return $this->identityMap[$rootClassName][$idHash]; |
---|
| 1443 | } |
---|
| 1444 | |
---|
| 1445 | return false; |
---|
| 1446 | } |
---|
| 1447 | |
---|
| 1448 | /** |
---|
| 1449 | * Checks whether an entity is registered in the identity map of this UnitOfWork. |
---|
| 1450 | * |
---|
| 1451 | * @param object $entity |
---|
| 1452 | * @return boolean |
---|
| 1453 | */ |
---|
| 1454 | public function isInIdentityMap($entity) |
---|
| 1455 | { |
---|
| 1456 | $oid = spl_object_hash($entity); |
---|
| 1457 | |
---|
| 1458 | if ( ! isset($this->entityIdentifiers[$oid])) { |
---|
| 1459 | return false; |
---|
| 1460 | } |
---|
| 1461 | |
---|
| 1462 | $classMetadata = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1463 | $idHash = implode(' ', $this->entityIdentifiers[$oid]); |
---|
| 1464 | |
---|
| 1465 | if ($idHash === '') { |
---|
| 1466 | return false; |
---|
| 1467 | } |
---|
| 1468 | |
---|
| 1469 | return isset($this->identityMap[$classMetadata->rootEntityName][$idHash]); |
---|
| 1470 | } |
---|
| 1471 | |
---|
| 1472 | /** |
---|
| 1473 | * INTERNAL: |
---|
| 1474 | * Checks whether an identifier hash exists in the identity map. |
---|
| 1475 | * |
---|
| 1476 | * @ignore |
---|
| 1477 | * @param string $idHash |
---|
| 1478 | * @param string $rootClassName |
---|
| 1479 | * @return boolean |
---|
| 1480 | */ |
---|
| 1481 | public function containsIdHash($idHash, $rootClassName) |
---|
| 1482 | { |
---|
| 1483 | return isset($this->identityMap[$rootClassName][$idHash]); |
---|
| 1484 | } |
---|
| 1485 | |
---|
| 1486 | /** |
---|
| 1487 | * Persists an entity as part of the current unit of work. |
---|
| 1488 | * |
---|
| 1489 | * @param object $entity The entity to persist. |
---|
| 1490 | */ |
---|
| 1491 | public function persist($entity) |
---|
| 1492 | { |
---|
| 1493 | $visited = array(); |
---|
| 1494 | |
---|
| 1495 | $this->doPersist($entity, $visited); |
---|
| 1496 | } |
---|
| 1497 | |
---|
| 1498 | /** |
---|
| 1499 | * Persists an entity as part of the current unit of work. |
---|
| 1500 | * |
---|
| 1501 | * This method is internally called during persist() cascades as it tracks |
---|
| 1502 | * the already visited entities to prevent infinite recursions. |
---|
| 1503 | * |
---|
| 1504 | * @param object $entity The entity to persist. |
---|
| 1505 | * @param array $visited The already visited entities. |
---|
| 1506 | */ |
---|
| 1507 | private function doPersist($entity, array &$visited) |
---|
| 1508 | { |
---|
| 1509 | $oid = spl_object_hash($entity); |
---|
| 1510 | |
---|
| 1511 | if (isset($visited[$oid])) { |
---|
| 1512 | return; // Prevent infinite recursion |
---|
| 1513 | } |
---|
| 1514 | |
---|
| 1515 | $visited[$oid] = $entity; // Mark visited |
---|
| 1516 | |
---|
| 1517 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1518 | |
---|
| 1519 | // We assume NEW, so DETACHED entities result in an exception on flush (constraint violation). |
---|
| 1520 | // If we would detect DETACHED here we would throw an exception anyway with the same |
---|
| 1521 | // consequences (not recoverable/programming error), so just assuming NEW here |
---|
| 1522 | // lets us avoid some database lookups for entities with natural identifiers. |
---|
| 1523 | $entityState = $this->getEntityState($entity, self::STATE_NEW); |
---|
| 1524 | |
---|
| 1525 | switch ($entityState) { |
---|
| 1526 | case self::STATE_MANAGED: |
---|
| 1527 | // Nothing to do, except if policy is "deferred explicit" |
---|
| 1528 | if ($class->isChangeTrackingDeferredExplicit()) { |
---|
| 1529 | $this->scheduleForDirtyCheck($entity); |
---|
| 1530 | } |
---|
| 1531 | break; |
---|
| 1532 | |
---|
| 1533 | case self::STATE_NEW: |
---|
| 1534 | $this->persistNew($class, $entity); |
---|
| 1535 | break; |
---|
| 1536 | |
---|
| 1537 | case self::STATE_REMOVED: |
---|
| 1538 | // Entity becomes managed again |
---|
| 1539 | unset($this->entityDeletions[$oid]); |
---|
| 1540 | |
---|
| 1541 | $this->entityStates[$oid] = self::STATE_MANAGED; |
---|
| 1542 | break; |
---|
| 1543 | |
---|
| 1544 | case self::STATE_DETACHED: |
---|
| 1545 | // Can actually not happen right now since we assume STATE_NEW. |
---|
| 1546 | throw ORMInvalidArgumentException::detachedEntityCannot($entity, "persisted"); |
---|
| 1547 | |
---|
| 1548 | default: |
---|
| 1549 | throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); |
---|
| 1550 | } |
---|
| 1551 | |
---|
| 1552 | $this->cascadePersist($entity, $visited); |
---|
| 1553 | } |
---|
| 1554 | |
---|
| 1555 | /** |
---|
| 1556 | * Deletes an entity as part of the current unit of work. |
---|
| 1557 | * |
---|
| 1558 | * @param object $entity The entity to remove. |
---|
| 1559 | */ |
---|
| 1560 | public function remove($entity) |
---|
| 1561 | { |
---|
| 1562 | $visited = array(); |
---|
| 1563 | |
---|
| 1564 | $this->doRemove($entity, $visited); |
---|
| 1565 | } |
---|
| 1566 | |
---|
| 1567 | /** |
---|
| 1568 | * Deletes an entity as part of the current unit of work. |
---|
| 1569 | * |
---|
| 1570 | * This method is internally called during delete() cascades as it tracks |
---|
| 1571 | * the already visited entities to prevent infinite recursions. |
---|
| 1572 | * |
---|
| 1573 | * @param object $entity The entity to delete. |
---|
| 1574 | * @param array $visited The map of the already visited entities. |
---|
| 1575 | * @throws InvalidArgumentException If the instance is a detached entity. |
---|
| 1576 | */ |
---|
| 1577 | private function doRemove($entity, array &$visited) |
---|
| 1578 | { |
---|
| 1579 | $oid = spl_object_hash($entity); |
---|
| 1580 | |
---|
| 1581 | if (isset($visited[$oid])) { |
---|
| 1582 | return; // Prevent infinite recursion |
---|
| 1583 | } |
---|
| 1584 | |
---|
| 1585 | $visited[$oid] = $entity; // mark visited |
---|
| 1586 | |
---|
| 1587 | // Cascade first, because scheduleForDelete() removes the entity from the identity map, which |
---|
| 1588 | // can cause problems when a lazy proxy has to be initialized for the cascade operation. |
---|
| 1589 | $this->cascadeRemove($entity, $visited); |
---|
| 1590 | |
---|
| 1591 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1592 | $entityState = $this->getEntityState($entity); |
---|
| 1593 | |
---|
| 1594 | switch ($entityState) { |
---|
| 1595 | case self::STATE_NEW: |
---|
| 1596 | case self::STATE_REMOVED: |
---|
| 1597 | // nothing to do |
---|
| 1598 | break; |
---|
| 1599 | |
---|
| 1600 | case self::STATE_MANAGED: |
---|
| 1601 | if (isset($class->lifecycleCallbacks[Events::preRemove])) { |
---|
| 1602 | $class->invokeLifecycleCallbacks(Events::preRemove, $entity); |
---|
| 1603 | } |
---|
| 1604 | |
---|
| 1605 | if ($this->evm->hasListeners(Events::preRemove)) { |
---|
| 1606 | $this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity, $this->em)); |
---|
| 1607 | } |
---|
| 1608 | |
---|
| 1609 | $this->scheduleForDelete($entity); |
---|
| 1610 | break; |
---|
| 1611 | |
---|
| 1612 | case self::STATE_DETACHED: |
---|
| 1613 | throw ORMInvalidArgumentException::detachedEntityCannot($entity, "removed"); |
---|
| 1614 | default: |
---|
| 1615 | throw new UnexpectedValueException("Unexpected entity state: $entityState." . self::objToStr($entity)); |
---|
| 1616 | } |
---|
| 1617 | |
---|
| 1618 | } |
---|
| 1619 | |
---|
| 1620 | /** |
---|
| 1621 | * Merges the state of the given detached entity into this UnitOfWork. |
---|
| 1622 | * |
---|
| 1623 | * @param object $entity |
---|
| 1624 | * @return object The managed copy of the entity. |
---|
| 1625 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
---|
| 1626 | * attribute and the version check against the managed copy fails. |
---|
| 1627 | * |
---|
| 1628 | * @todo Require active transaction!? OptimisticLockException may result in undefined state!? |
---|
| 1629 | */ |
---|
| 1630 | public function merge($entity) |
---|
| 1631 | { |
---|
| 1632 | $visited = array(); |
---|
| 1633 | |
---|
| 1634 | return $this->doMerge($entity, $visited); |
---|
| 1635 | } |
---|
| 1636 | |
---|
| 1637 | /** |
---|
| 1638 | * Executes a merge operation on an entity. |
---|
| 1639 | * |
---|
| 1640 | * @param object $entity |
---|
| 1641 | * @param array $visited |
---|
| 1642 | * @return object The managed copy of the entity. |
---|
| 1643 | * @throws OptimisticLockException If the entity uses optimistic locking through a version |
---|
| 1644 | * attribute and the version check against the managed copy fails. |
---|
| 1645 | * @throws InvalidArgumentException If the entity instance is NEW. |
---|
| 1646 | */ |
---|
| 1647 | private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null) |
---|
| 1648 | { |
---|
| 1649 | $oid = spl_object_hash($entity); |
---|
| 1650 | |
---|
| 1651 | if (isset($visited[$oid])) { |
---|
| 1652 | return $visited[$oid]; // Prevent infinite recursion |
---|
| 1653 | } |
---|
| 1654 | |
---|
| 1655 | $visited[$oid] = $entity; // mark visited |
---|
| 1656 | |
---|
| 1657 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1658 | |
---|
| 1659 | // First we assume DETACHED, although it can still be NEW but we can avoid |
---|
| 1660 | // an extra db-roundtrip this way. If it is not MANAGED but has an identity, |
---|
| 1661 | // we need to fetch it from the db anyway in order to merge. |
---|
| 1662 | // MANAGED entities are ignored by the merge operation. |
---|
| 1663 | $managedCopy = $entity; |
---|
| 1664 | |
---|
| 1665 | if ($this->getEntityState($entity, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
---|
| 1666 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
---|
| 1667 | $entity->__load(); |
---|
| 1668 | } |
---|
| 1669 | |
---|
| 1670 | // Try to look the entity up in the identity map. |
---|
| 1671 | $id = $class->getIdentifierValues($entity); |
---|
| 1672 | |
---|
| 1673 | // If there is no ID, it is actually NEW. |
---|
| 1674 | if ( ! $id) { |
---|
| 1675 | $managedCopy = $this->newInstance($class); |
---|
| 1676 | |
---|
| 1677 | $this->persistNew($class, $managedCopy); |
---|
| 1678 | } else { |
---|
| 1679 | $flatId = $id; |
---|
| 1680 | if ($class->containsForeignIdentifier) { |
---|
| 1681 | // convert foreign identifiers into scalar foreign key |
---|
| 1682 | // values to avoid object to string conversion failures. |
---|
| 1683 | foreach ($id as $idField => $idValue) { |
---|
| 1684 | if (isset($class->associationMappings[$idField])) { |
---|
| 1685 | $targetClassMetadata = $this->em->getClassMetadata($class->associationMappings[$idField]['targetEntity']); |
---|
| 1686 | $associatedId = $this->getEntityIdentifier($idValue); |
---|
| 1687 | $flatId[$idField] = $associatedId[$targetClassMetadata->identifier[0]]; |
---|
| 1688 | } |
---|
| 1689 | } |
---|
| 1690 | } |
---|
| 1691 | |
---|
| 1692 | $managedCopy = $this->tryGetById($flatId, $class->rootEntityName); |
---|
| 1693 | |
---|
| 1694 | if ($managedCopy) { |
---|
| 1695 | // We have the entity in-memory already, just make sure its not removed. |
---|
| 1696 | if ($this->getEntityState($managedCopy) == self::STATE_REMOVED) { |
---|
| 1697 | throw ORMInvalidArgumentException::entityIsRemoved($managedCopy, "merge"); |
---|
| 1698 | } |
---|
| 1699 | } else { |
---|
| 1700 | // We need to fetch the managed copy in order to merge. |
---|
| 1701 | $managedCopy = $this->em->find($class->name, $flatId); |
---|
| 1702 | } |
---|
| 1703 | |
---|
| 1704 | if ($managedCopy === null) { |
---|
| 1705 | // If the identifier is ASSIGNED, it is NEW, otherwise an error |
---|
| 1706 | // since the managed entity was not found. |
---|
| 1707 | if ( ! $class->isIdentifierNatural()) { |
---|
| 1708 | throw new EntityNotFoundException; |
---|
| 1709 | } |
---|
| 1710 | |
---|
| 1711 | $managedCopy = $this->newInstance($class); |
---|
| 1712 | $class->setIdentifierValues($managedCopy, $id); |
---|
| 1713 | |
---|
| 1714 | $this->persistNew($class, $managedCopy); |
---|
| 1715 | } else { |
---|
| 1716 | if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) { |
---|
| 1717 | $managedCopy->__load(); |
---|
| 1718 | } |
---|
| 1719 | } |
---|
| 1720 | } |
---|
| 1721 | |
---|
| 1722 | if ($class->isVersioned) { |
---|
| 1723 | $managedCopyVersion = $class->reflFields[$class->versionField]->getValue($managedCopy); |
---|
| 1724 | $entityVersion = $class->reflFields[$class->versionField]->getValue($entity); |
---|
| 1725 | |
---|
| 1726 | // Throw exception if versions dont match. |
---|
| 1727 | if ($managedCopyVersion != $entityVersion) { |
---|
| 1728 | throw OptimisticLockException::lockFailedVersionMissmatch($entity, $entityVersion, $managedCopyVersion); |
---|
| 1729 | } |
---|
| 1730 | } |
---|
| 1731 | |
---|
| 1732 | // Merge state of $entity into existing (managed) entity |
---|
| 1733 | foreach ($class->reflFields as $name => $prop) { |
---|
| 1734 | if ( ! isset($class->associationMappings[$name])) { |
---|
| 1735 | if ( ! $class->isIdentifier($name)) { |
---|
| 1736 | $prop->setValue($managedCopy, $prop->getValue($entity)); |
---|
| 1737 | } |
---|
| 1738 | } else { |
---|
| 1739 | $assoc2 = $class->associationMappings[$name]; |
---|
| 1740 | if ($assoc2['type'] & ClassMetadata::TO_ONE) { |
---|
| 1741 | $other = $prop->getValue($entity); |
---|
| 1742 | if ($other === null) { |
---|
| 1743 | $prop->setValue($managedCopy, null); |
---|
| 1744 | } else if ($other instanceof Proxy && !$other->__isInitialized__) { |
---|
| 1745 | // do not merge fields marked lazy that have not been fetched. |
---|
| 1746 | continue; |
---|
| 1747 | } else if ( ! $assoc2['isCascadeMerge']) { |
---|
| 1748 | if ($this->getEntityState($other, self::STATE_DETACHED) !== self::STATE_MANAGED) { |
---|
| 1749 | $targetClass = $this->em->getClassMetadata($assoc2['targetEntity']); |
---|
| 1750 | $relatedId = $targetClass->getIdentifierValues($other); |
---|
| 1751 | |
---|
| 1752 | if ($targetClass->subClasses) { |
---|
| 1753 | $other = $this->em->find($targetClass->name, $relatedId); |
---|
| 1754 | } else { |
---|
| 1755 | $other = $this->em->getProxyFactory()->getProxy($assoc2['targetEntity'], $relatedId); |
---|
| 1756 | $this->registerManaged($other, $relatedId, array()); |
---|
| 1757 | } |
---|
| 1758 | } |
---|
| 1759 | $prop->setValue($managedCopy, $other); |
---|
| 1760 | } |
---|
| 1761 | } else { |
---|
| 1762 | $mergeCol = $prop->getValue($entity); |
---|
| 1763 | if ($mergeCol instanceof PersistentCollection && !$mergeCol->isInitialized()) { |
---|
| 1764 | // do not merge fields marked lazy that have not been fetched. |
---|
| 1765 | // keep the lazy persistent collection of the managed copy. |
---|
| 1766 | continue; |
---|
| 1767 | } |
---|
| 1768 | |
---|
| 1769 | $managedCol = $prop->getValue($managedCopy); |
---|
| 1770 | if (!$managedCol) { |
---|
| 1771 | $managedCol = new PersistentCollection($this->em, |
---|
| 1772 | $this->em->getClassMetadata($assoc2['targetEntity']), |
---|
| 1773 | new ArrayCollection |
---|
| 1774 | ); |
---|
| 1775 | $managedCol->setOwner($managedCopy, $assoc2); |
---|
| 1776 | $prop->setValue($managedCopy, $managedCol); |
---|
| 1777 | $this->originalEntityData[$oid][$name] = $managedCol; |
---|
| 1778 | } |
---|
| 1779 | if ($assoc2['isCascadeMerge']) { |
---|
| 1780 | $managedCol->initialize(); |
---|
| 1781 | |
---|
| 1782 | // clear and set dirty a managed collection if its not also the same collection to merge from. |
---|
| 1783 | if (!$managedCol->isEmpty() && $managedCol !== $mergeCol) { |
---|
| 1784 | $managedCol->unwrap()->clear(); |
---|
| 1785 | $managedCol->setDirty(true); |
---|
| 1786 | |
---|
| 1787 | if ($assoc2['isOwningSide'] && $assoc2['type'] == ClassMetadata::MANY_TO_MANY && $class->isChangeTrackingNotify()) { |
---|
| 1788 | $this->scheduleForDirtyCheck($managedCopy); |
---|
| 1789 | } |
---|
| 1790 | } |
---|
| 1791 | } |
---|
| 1792 | } |
---|
| 1793 | } |
---|
| 1794 | |
---|
| 1795 | if ($class->isChangeTrackingNotify()) { |
---|
| 1796 | // Just treat all properties as changed, there is no other choice. |
---|
| 1797 | $this->propertyChanged($managedCopy, $name, null, $prop->getValue($managedCopy)); |
---|
| 1798 | } |
---|
| 1799 | } |
---|
| 1800 | |
---|
| 1801 | if ($class->isChangeTrackingDeferredExplicit()) { |
---|
| 1802 | $this->scheduleForDirtyCheck($entity); |
---|
| 1803 | } |
---|
| 1804 | } |
---|
| 1805 | |
---|
| 1806 | if ($prevManagedCopy !== null) { |
---|
| 1807 | $assocField = $assoc['fieldName']; |
---|
| 1808 | $prevClass = $this->em->getClassMetadata(get_class($prevManagedCopy)); |
---|
| 1809 | |
---|
| 1810 | if ($assoc['type'] & ClassMetadata::TO_ONE) { |
---|
| 1811 | $prevClass->reflFields[$assocField]->setValue($prevManagedCopy, $managedCopy); |
---|
| 1812 | } else { |
---|
| 1813 | $prevClass->reflFields[$assocField]->getValue($prevManagedCopy)->add($managedCopy); |
---|
| 1814 | |
---|
| 1815 | if ($assoc['type'] == ClassMetadata::ONE_TO_MANY) { |
---|
| 1816 | $class->reflFields[$assoc['mappedBy']]->setValue($managedCopy, $prevManagedCopy); |
---|
| 1817 | } |
---|
| 1818 | } |
---|
| 1819 | } |
---|
| 1820 | |
---|
| 1821 | // Mark the managed copy visited as well |
---|
| 1822 | $visited[spl_object_hash($managedCopy)] = true; |
---|
| 1823 | |
---|
| 1824 | $this->cascadeMerge($entity, $managedCopy, $visited); |
---|
| 1825 | |
---|
| 1826 | return $managedCopy; |
---|
| 1827 | } |
---|
| 1828 | |
---|
| 1829 | /** |
---|
| 1830 | * Detaches an entity from the persistence management. It's persistence will |
---|
| 1831 | * no longer be managed by Doctrine. |
---|
| 1832 | * |
---|
| 1833 | * @param object $entity The entity to detach. |
---|
| 1834 | */ |
---|
| 1835 | public function detach($entity) |
---|
| 1836 | { |
---|
| 1837 | $visited = array(); |
---|
| 1838 | |
---|
| 1839 | $this->doDetach($entity, $visited); |
---|
| 1840 | } |
---|
| 1841 | |
---|
| 1842 | /** |
---|
| 1843 | * Executes a detach operation on the given entity. |
---|
| 1844 | * |
---|
| 1845 | * @param object $entity |
---|
| 1846 | * @param array $visited |
---|
| 1847 | * @param boolean $noCascade if true, don't cascade detach operation |
---|
| 1848 | */ |
---|
| 1849 | private function doDetach($entity, array &$visited, $noCascade = false) |
---|
| 1850 | { |
---|
| 1851 | $oid = spl_object_hash($entity); |
---|
| 1852 | |
---|
| 1853 | if (isset($visited[$oid])) { |
---|
| 1854 | return; // Prevent infinite recursion |
---|
| 1855 | } |
---|
| 1856 | |
---|
| 1857 | $visited[$oid] = $entity; // mark visited |
---|
| 1858 | |
---|
| 1859 | switch ($this->getEntityState($entity, self::STATE_DETACHED)) { |
---|
| 1860 | case self::STATE_MANAGED: |
---|
| 1861 | if ($this->isInIdentityMap($entity)) { |
---|
| 1862 | $this->removeFromIdentityMap($entity); |
---|
| 1863 | } |
---|
| 1864 | |
---|
| 1865 | unset( |
---|
| 1866 | $this->entityInsertions[$oid], |
---|
| 1867 | $this->entityUpdates[$oid], |
---|
| 1868 | $this->entityDeletions[$oid], |
---|
| 1869 | $this->entityIdentifiers[$oid], |
---|
| 1870 | $this->entityStates[$oid], |
---|
| 1871 | $this->originalEntityData[$oid] |
---|
| 1872 | ); |
---|
| 1873 | break; |
---|
| 1874 | case self::STATE_NEW: |
---|
| 1875 | case self::STATE_DETACHED: |
---|
| 1876 | return; |
---|
| 1877 | } |
---|
| 1878 | |
---|
| 1879 | if ( ! $noCascade) { |
---|
| 1880 | $this->cascadeDetach($entity, $visited); |
---|
| 1881 | } |
---|
| 1882 | } |
---|
| 1883 | |
---|
| 1884 | /** |
---|
| 1885 | * Refreshes the state of the given entity from the database, overwriting |
---|
| 1886 | * any local, unpersisted changes. |
---|
| 1887 | * |
---|
| 1888 | * @param object $entity The entity to refresh. |
---|
| 1889 | * @throws InvalidArgumentException If the entity is not MANAGED. |
---|
| 1890 | */ |
---|
| 1891 | public function refresh($entity) |
---|
| 1892 | { |
---|
| 1893 | $visited = array(); |
---|
| 1894 | |
---|
| 1895 | $this->doRefresh($entity, $visited); |
---|
| 1896 | } |
---|
| 1897 | |
---|
| 1898 | /** |
---|
| 1899 | * Executes a refresh operation on an entity. |
---|
| 1900 | * |
---|
| 1901 | * @param object $entity The entity to refresh. |
---|
| 1902 | * @param array $visited The already visited entities during cascades. |
---|
| 1903 | * @throws InvalidArgumentException If the entity is not MANAGED. |
---|
| 1904 | */ |
---|
| 1905 | private function doRefresh($entity, array &$visited) |
---|
| 1906 | { |
---|
| 1907 | $oid = spl_object_hash($entity); |
---|
| 1908 | |
---|
| 1909 | if (isset($visited[$oid])) { |
---|
| 1910 | return; // Prevent infinite recursion |
---|
| 1911 | } |
---|
| 1912 | |
---|
| 1913 | $visited[$oid] = $entity; // mark visited |
---|
| 1914 | |
---|
| 1915 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1916 | |
---|
| 1917 | if ($this->getEntityState($entity) !== self::STATE_MANAGED) { |
---|
| 1918 | throw ORMInvalidArgumentException::entityNotManaged($entity); |
---|
| 1919 | } |
---|
| 1920 | |
---|
| 1921 | $this->getEntityPersister($class->name)->refresh( |
---|
| 1922 | array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), |
---|
| 1923 | $entity |
---|
| 1924 | ); |
---|
| 1925 | |
---|
| 1926 | $this->cascadeRefresh($entity, $visited); |
---|
| 1927 | } |
---|
| 1928 | |
---|
| 1929 | /** |
---|
| 1930 | * Cascades a refresh operation to associated entities. |
---|
| 1931 | * |
---|
| 1932 | * @param object $entity |
---|
| 1933 | * @param array $visited |
---|
| 1934 | */ |
---|
| 1935 | private function cascadeRefresh($entity, array &$visited) |
---|
| 1936 | { |
---|
| 1937 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1938 | |
---|
| 1939 | $associationMappings = array_filter( |
---|
| 1940 | $class->associationMappings, |
---|
| 1941 | function ($assoc) { return $assoc['isCascadeRefresh']; } |
---|
| 1942 | ); |
---|
| 1943 | |
---|
| 1944 | foreach ($associationMappings as $assoc) { |
---|
| 1945 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
---|
| 1946 | |
---|
| 1947 | switch (true) { |
---|
| 1948 | case ($relatedEntities instanceof PersistentCollection): |
---|
| 1949 | // Unwrap so that foreach() does not initialize |
---|
| 1950 | $relatedEntities = $relatedEntities->unwrap(); |
---|
| 1951 | // break; is commented intentionally! |
---|
| 1952 | |
---|
| 1953 | case ($relatedEntities instanceof Collection): |
---|
| 1954 | case (is_array($relatedEntities)): |
---|
| 1955 | foreach ($relatedEntities as $relatedEntity) { |
---|
| 1956 | $this->doRefresh($relatedEntity, $visited); |
---|
| 1957 | } |
---|
| 1958 | break; |
---|
| 1959 | |
---|
| 1960 | case ($relatedEntities !== null): |
---|
| 1961 | $this->doRefresh($relatedEntities, $visited); |
---|
| 1962 | break; |
---|
| 1963 | |
---|
| 1964 | default: |
---|
| 1965 | // Do nothing |
---|
| 1966 | } |
---|
| 1967 | } |
---|
| 1968 | } |
---|
| 1969 | |
---|
| 1970 | /** |
---|
| 1971 | * Cascades a detach operation to associated entities. |
---|
| 1972 | * |
---|
| 1973 | * @param object $entity |
---|
| 1974 | * @param array $visited |
---|
| 1975 | */ |
---|
| 1976 | private function cascadeDetach($entity, array &$visited) |
---|
| 1977 | { |
---|
| 1978 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 1979 | |
---|
| 1980 | $associationMappings = array_filter( |
---|
| 1981 | $class->associationMappings, |
---|
| 1982 | function ($assoc) { return $assoc['isCascadeDetach']; } |
---|
| 1983 | ); |
---|
| 1984 | |
---|
| 1985 | foreach ($associationMappings as $assoc) { |
---|
| 1986 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
---|
| 1987 | |
---|
| 1988 | switch (true) { |
---|
| 1989 | case ($relatedEntities instanceof PersistentCollection): |
---|
| 1990 | // Unwrap so that foreach() does not initialize |
---|
| 1991 | $relatedEntities = $relatedEntities->unwrap(); |
---|
| 1992 | // break; is commented intentionally! |
---|
| 1993 | |
---|
| 1994 | case ($relatedEntities instanceof Collection): |
---|
| 1995 | case (is_array($relatedEntities)): |
---|
| 1996 | foreach ($relatedEntities as $relatedEntity) { |
---|
| 1997 | $this->doDetach($relatedEntity, $visited); |
---|
| 1998 | } |
---|
| 1999 | break; |
---|
| 2000 | |
---|
| 2001 | case ($relatedEntities !== null): |
---|
| 2002 | $this->doDetach($relatedEntities, $visited); |
---|
| 2003 | break; |
---|
| 2004 | |
---|
| 2005 | default: |
---|
| 2006 | // Do nothing |
---|
| 2007 | } |
---|
| 2008 | } |
---|
| 2009 | } |
---|
| 2010 | |
---|
| 2011 | /** |
---|
| 2012 | * Cascades a merge operation to associated entities. |
---|
| 2013 | * |
---|
| 2014 | * @param object $entity |
---|
| 2015 | * @param object $managedCopy |
---|
| 2016 | * @param array $visited |
---|
| 2017 | */ |
---|
| 2018 | private function cascadeMerge($entity, $managedCopy, array &$visited) |
---|
| 2019 | { |
---|
| 2020 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 2021 | |
---|
| 2022 | $associationMappings = array_filter( |
---|
| 2023 | $class->associationMappings, |
---|
| 2024 | function ($assoc) { return $assoc['isCascadeMerge']; } |
---|
| 2025 | ); |
---|
| 2026 | |
---|
| 2027 | foreach ($associationMappings as $assoc) { |
---|
| 2028 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
---|
| 2029 | |
---|
| 2030 | if ($relatedEntities instanceof Collection) { |
---|
| 2031 | if ($relatedEntities === $class->reflFields[$assoc['fieldName']]->getValue($managedCopy)) { |
---|
| 2032 | continue; |
---|
| 2033 | } |
---|
| 2034 | |
---|
| 2035 | if ($relatedEntities instanceof PersistentCollection) { |
---|
| 2036 | // Unwrap so that foreach() does not initialize |
---|
| 2037 | $relatedEntities = $relatedEntities->unwrap(); |
---|
| 2038 | } |
---|
| 2039 | |
---|
| 2040 | foreach ($relatedEntities as $relatedEntity) { |
---|
| 2041 | $this->doMerge($relatedEntity, $visited, $managedCopy, $assoc); |
---|
| 2042 | } |
---|
| 2043 | } else if ($relatedEntities !== null) { |
---|
| 2044 | $this->doMerge($relatedEntities, $visited, $managedCopy, $assoc); |
---|
| 2045 | } |
---|
| 2046 | } |
---|
| 2047 | } |
---|
| 2048 | |
---|
| 2049 | /** |
---|
| 2050 | * Cascades the save operation to associated entities. |
---|
| 2051 | * |
---|
| 2052 | * @param object $entity |
---|
| 2053 | * @param array $visited |
---|
| 2054 | * @param array $insertNow |
---|
| 2055 | */ |
---|
| 2056 | private function cascadePersist($entity, array &$visited) |
---|
| 2057 | { |
---|
| 2058 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 2059 | |
---|
| 2060 | $associationMappings = array_filter( |
---|
| 2061 | $class->associationMappings, |
---|
| 2062 | function ($assoc) { return $assoc['isCascadePersist']; } |
---|
| 2063 | ); |
---|
| 2064 | |
---|
| 2065 | foreach ($associationMappings as $assoc) { |
---|
| 2066 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
---|
| 2067 | |
---|
| 2068 | switch (true) { |
---|
| 2069 | case ($relatedEntities instanceof PersistentCollection): |
---|
| 2070 | // Unwrap so that foreach() does not initialize |
---|
| 2071 | $relatedEntities = $relatedEntities->unwrap(); |
---|
| 2072 | // break; is commented intentionally! |
---|
| 2073 | |
---|
| 2074 | case ($relatedEntities instanceof Collection): |
---|
| 2075 | case (is_array($relatedEntities)): |
---|
| 2076 | foreach ($relatedEntities as $relatedEntity) { |
---|
| 2077 | $this->doPersist($relatedEntity, $visited); |
---|
| 2078 | } |
---|
| 2079 | break; |
---|
| 2080 | |
---|
| 2081 | case ($relatedEntities !== null): |
---|
| 2082 | $this->doPersist($relatedEntities, $visited); |
---|
| 2083 | break; |
---|
| 2084 | |
---|
| 2085 | default: |
---|
| 2086 | // Do nothing |
---|
| 2087 | } |
---|
| 2088 | } |
---|
| 2089 | } |
---|
| 2090 | |
---|
| 2091 | /** |
---|
| 2092 | * Cascades the delete operation to associated entities. |
---|
| 2093 | * |
---|
| 2094 | * @param object $entity |
---|
| 2095 | * @param array $visited |
---|
| 2096 | */ |
---|
| 2097 | private function cascadeRemove($entity, array &$visited) |
---|
| 2098 | { |
---|
| 2099 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 2100 | |
---|
| 2101 | $associationMappings = array_filter( |
---|
| 2102 | $class->associationMappings, |
---|
| 2103 | function ($assoc) { return $assoc['isCascadeRemove']; } |
---|
| 2104 | ); |
---|
| 2105 | |
---|
| 2106 | foreach ($associationMappings as $assoc) { |
---|
| 2107 | if ($entity instanceof Proxy && !$entity->__isInitialized__) { |
---|
| 2108 | $entity->__load(); |
---|
| 2109 | } |
---|
| 2110 | |
---|
| 2111 | $relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); |
---|
| 2112 | |
---|
| 2113 | switch (true) { |
---|
| 2114 | case ($relatedEntities instanceof Collection): |
---|
| 2115 | case (is_array($relatedEntities)): |
---|
| 2116 | // If its a PersistentCollection initialization is intended! No unwrap! |
---|
| 2117 | foreach ($relatedEntities as $relatedEntity) { |
---|
| 2118 | $this->doRemove($relatedEntity, $visited); |
---|
| 2119 | } |
---|
| 2120 | break; |
---|
| 2121 | |
---|
| 2122 | case ($relatedEntities !== null): |
---|
| 2123 | $this->doRemove($relatedEntities, $visited); |
---|
| 2124 | break; |
---|
| 2125 | |
---|
| 2126 | default: |
---|
| 2127 | // Do nothing |
---|
| 2128 | } |
---|
| 2129 | } |
---|
| 2130 | } |
---|
| 2131 | |
---|
| 2132 | /** |
---|
| 2133 | * Acquire a lock on the given entity. |
---|
| 2134 | * |
---|
| 2135 | * @param object $entity |
---|
| 2136 | * @param int $lockMode |
---|
| 2137 | * @param int $lockVersion |
---|
| 2138 | */ |
---|
| 2139 | public function lock($entity, $lockMode, $lockVersion = null) |
---|
| 2140 | { |
---|
| 2141 | if ($this->getEntityState($entity, self::STATE_DETACHED) != self::STATE_MANAGED) { |
---|
| 2142 | throw ORMInvalidArgumentException::entityNotManaged($entity); |
---|
| 2143 | } |
---|
| 2144 | |
---|
| 2145 | $entityName = get_class($entity); |
---|
| 2146 | $class = $this->em->getClassMetadata($entityName); |
---|
| 2147 | |
---|
| 2148 | switch ($lockMode) { |
---|
| 2149 | case \Doctrine\DBAL\LockMode::OPTIMISTIC; |
---|
| 2150 | if ( ! $class->isVersioned) { |
---|
| 2151 | throw OptimisticLockException::notVersioned($entityName); |
---|
| 2152 | } |
---|
| 2153 | |
---|
| 2154 | if ($lockVersion === null) { |
---|
| 2155 | return; |
---|
| 2156 | } |
---|
| 2157 | |
---|
| 2158 | $entityVersion = $class->reflFields[$class->versionField]->getValue($entity); |
---|
| 2159 | |
---|
| 2160 | if ($entityVersion != $lockVersion) { |
---|
| 2161 | throw OptimisticLockException::lockFailedVersionMissmatch($entity, $lockVersion, $entityVersion); |
---|
| 2162 | } |
---|
| 2163 | |
---|
| 2164 | break; |
---|
| 2165 | |
---|
| 2166 | case \Doctrine\DBAL\LockMode::PESSIMISTIC_READ: |
---|
| 2167 | case \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE: |
---|
| 2168 | if (!$this->em->getConnection()->isTransactionActive()) { |
---|
| 2169 | throw TransactionRequiredException::transactionRequired(); |
---|
| 2170 | } |
---|
| 2171 | |
---|
| 2172 | $oid = spl_object_hash($entity); |
---|
| 2173 | |
---|
| 2174 | $this->getEntityPersister($class->name)->lock( |
---|
| 2175 | array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]), |
---|
| 2176 | $lockMode |
---|
| 2177 | ); |
---|
| 2178 | break; |
---|
| 2179 | |
---|
| 2180 | default: |
---|
| 2181 | // Do nothing |
---|
| 2182 | } |
---|
| 2183 | } |
---|
| 2184 | |
---|
| 2185 | /** |
---|
| 2186 | * Gets the CommitOrderCalculator used by the UnitOfWork to order commits. |
---|
| 2187 | * |
---|
| 2188 | * @return \Doctrine\ORM\Internal\CommitOrderCalculator |
---|
| 2189 | */ |
---|
| 2190 | public function getCommitOrderCalculator() |
---|
| 2191 | { |
---|
| 2192 | if ($this->commitOrderCalculator === null) { |
---|
| 2193 | $this->commitOrderCalculator = new Internal\CommitOrderCalculator; |
---|
| 2194 | } |
---|
| 2195 | |
---|
| 2196 | return $this->commitOrderCalculator; |
---|
| 2197 | } |
---|
| 2198 | |
---|
| 2199 | /** |
---|
| 2200 | * Clears the UnitOfWork. |
---|
| 2201 | * |
---|
| 2202 | * @param string $entityName if given, only entities of this type will get detached |
---|
| 2203 | */ |
---|
| 2204 | public function clear($entityName = null) |
---|
| 2205 | { |
---|
| 2206 | if ($entityName === null) { |
---|
| 2207 | $this->identityMap = |
---|
| 2208 | $this->entityIdentifiers = |
---|
| 2209 | $this->originalEntityData = |
---|
| 2210 | $this->entityChangeSets = |
---|
| 2211 | $this->entityStates = |
---|
| 2212 | $this->scheduledForDirtyCheck = |
---|
| 2213 | $this->entityInsertions = |
---|
| 2214 | $this->entityUpdates = |
---|
| 2215 | $this->entityDeletions = |
---|
| 2216 | $this->collectionDeletions = |
---|
| 2217 | $this->collectionUpdates = |
---|
| 2218 | $this->extraUpdates = |
---|
| 2219 | $this->readOnlyObjects = |
---|
| 2220 | $this->orphanRemovals = array(); |
---|
| 2221 | |
---|
| 2222 | if ($this->commitOrderCalculator !== null) { |
---|
| 2223 | $this->commitOrderCalculator->clear(); |
---|
| 2224 | } |
---|
| 2225 | } else { |
---|
| 2226 | $visited = array(); |
---|
| 2227 | foreach ($this->identityMap as $className => $entities) { |
---|
| 2228 | if ($className === $entityName) { |
---|
| 2229 | foreach ($entities as $entity) { |
---|
| 2230 | $this->doDetach($entity, $visited, true); |
---|
| 2231 | } |
---|
| 2232 | } |
---|
| 2233 | } |
---|
| 2234 | } |
---|
| 2235 | |
---|
| 2236 | if ($this->evm->hasListeners(Events::onClear)) { |
---|
| 2237 | $this->evm->dispatchEvent(Events::onClear, new Event\OnClearEventArgs($this->em, $entityName)); |
---|
| 2238 | } |
---|
| 2239 | } |
---|
| 2240 | |
---|
| 2241 | /** |
---|
| 2242 | * INTERNAL: |
---|
| 2243 | * Schedules an orphaned entity for removal. The remove() operation will be |
---|
| 2244 | * invoked on that entity at the beginning of the next commit of this |
---|
| 2245 | * UnitOfWork. |
---|
| 2246 | * |
---|
| 2247 | * @ignore |
---|
| 2248 | * @param object $entity |
---|
| 2249 | */ |
---|
| 2250 | public function scheduleOrphanRemoval($entity) |
---|
| 2251 | { |
---|
| 2252 | $this->orphanRemovals[spl_object_hash($entity)] = $entity; |
---|
| 2253 | } |
---|
| 2254 | |
---|
| 2255 | /** |
---|
| 2256 | * INTERNAL: |
---|
| 2257 | * Schedules a complete collection for removal when this UnitOfWork commits. |
---|
| 2258 | * |
---|
| 2259 | * @param PersistentCollection $coll |
---|
| 2260 | */ |
---|
| 2261 | public function scheduleCollectionDeletion(PersistentCollection $coll) |
---|
| 2262 | { |
---|
| 2263 | $coid = spl_object_hash($coll); |
---|
| 2264 | |
---|
| 2265 | //TODO: if $coll is already scheduled for recreation ... what to do? |
---|
| 2266 | // Just remove $coll from the scheduled recreations? |
---|
| 2267 | if (isset($this->collectionUpdates[$coid])) { |
---|
| 2268 | unset($this->collectionUpdates[$coid]); |
---|
| 2269 | } |
---|
| 2270 | |
---|
| 2271 | $this->collectionDeletions[$coid] = $coll; |
---|
| 2272 | } |
---|
| 2273 | |
---|
| 2274 | public function isCollectionScheduledForDeletion(PersistentCollection $coll) |
---|
| 2275 | { |
---|
| 2276 | return isset($this->collectionsDeletions[spl_object_hash($coll)]); |
---|
| 2277 | } |
---|
| 2278 | |
---|
| 2279 | /** |
---|
| 2280 | * @param ClassMetadata $class |
---|
| 2281 | */ |
---|
| 2282 | private function newInstance($class) |
---|
| 2283 | { |
---|
| 2284 | $entity = $class->newInstance(); |
---|
| 2285 | |
---|
| 2286 | if ($entity instanceof \Doctrine\Common\Persistence\ObjectManagerAware) { |
---|
| 2287 | $entity->injectObjectManager($this->em, $class); |
---|
| 2288 | } |
---|
| 2289 | |
---|
| 2290 | return $entity; |
---|
| 2291 | } |
---|
| 2292 | |
---|
| 2293 | /** |
---|
| 2294 | * INTERNAL: |
---|
| 2295 | * Creates an entity. Used for reconstitution of persistent entities. |
---|
| 2296 | * |
---|
| 2297 | * @ignore |
---|
| 2298 | * @param string $className The name of the entity class. |
---|
| 2299 | * @param array $data The data for the entity. |
---|
| 2300 | * @param array $hints Any hints to account for during reconstitution/lookup of the entity. |
---|
| 2301 | * @return object The managed entity instance. |
---|
| 2302 | * @internal Highly performance-sensitive method. |
---|
| 2303 | * |
---|
| 2304 | * @todo Rename: getOrCreateEntity |
---|
| 2305 | */ |
---|
| 2306 | public function createEntity($className, array $data, &$hints = array()) |
---|
| 2307 | { |
---|
| 2308 | $class = $this->em->getClassMetadata($className); |
---|
| 2309 | //$isReadOnly = isset($hints[Query::HINT_READ_ONLY]); |
---|
| 2310 | |
---|
| 2311 | if ($class->isIdentifierComposite) { |
---|
| 2312 | $id = array(); |
---|
| 2313 | |
---|
| 2314 | foreach ($class->identifier as $fieldName) { |
---|
| 2315 | $id[$fieldName] = isset($class->associationMappings[$fieldName]) |
---|
| 2316 | ? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']] |
---|
| 2317 | : $data[$fieldName]; |
---|
| 2318 | } |
---|
| 2319 | |
---|
| 2320 | $idHash = implode(' ', $id); |
---|
| 2321 | } else { |
---|
| 2322 | $idHash = isset($class->associationMappings[$class->identifier[0]]) |
---|
| 2323 | ? $data[$class->associationMappings[$class->identifier[0]]['joinColumns'][0]['name']] |
---|
| 2324 | : $data[$class->identifier[0]]; |
---|
| 2325 | |
---|
| 2326 | $id = array($class->identifier[0] => $idHash); |
---|
| 2327 | } |
---|
| 2328 | |
---|
| 2329 | if (isset($this->identityMap[$class->rootEntityName][$idHash])) { |
---|
| 2330 | $entity = $this->identityMap[$class->rootEntityName][$idHash]; |
---|
| 2331 | $oid = spl_object_hash($entity); |
---|
| 2332 | |
---|
| 2333 | if ($entity instanceof Proxy && ! $entity->__isInitialized__) { |
---|
| 2334 | $entity->__isInitialized__ = true; |
---|
| 2335 | $overrideLocalValues = true; |
---|
| 2336 | |
---|
| 2337 | if ($entity instanceof NotifyPropertyChanged) { |
---|
| 2338 | $entity->addPropertyChangedListener($this); |
---|
| 2339 | } |
---|
| 2340 | } else { |
---|
| 2341 | $overrideLocalValues = isset($hints[Query::HINT_REFRESH]); |
---|
| 2342 | |
---|
| 2343 | // If only a specific entity is set to refresh, check that it's the one |
---|
| 2344 | if(isset($hints[Query::HINT_REFRESH_ENTITY])) { |
---|
| 2345 | $overrideLocalValues = $hints[Query::HINT_REFRESH_ENTITY] === $entity; |
---|
| 2346 | |
---|
| 2347 | // inject ObjectManager into just loaded proxies. |
---|
| 2348 | if ($overrideLocalValues && $entity instanceof ObjectManagerAware) { |
---|
| 2349 | $entity->injectObjectManager($this->em, $class); |
---|
| 2350 | } |
---|
| 2351 | } |
---|
| 2352 | } |
---|
| 2353 | |
---|
| 2354 | if ($overrideLocalValues) { |
---|
| 2355 | $this->originalEntityData[$oid] = $data; |
---|
| 2356 | } |
---|
| 2357 | } else { |
---|
| 2358 | $entity = $this->newInstance($class); |
---|
| 2359 | $oid = spl_object_hash($entity); |
---|
| 2360 | |
---|
| 2361 | $this->entityIdentifiers[$oid] = $id; |
---|
| 2362 | $this->entityStates[$oid] = self::STATE_MANAGED; |
---|
| 2363 | $this->originalEntityData[$oid] = $data; |
---|
| 2364 | |
---|
| 2365 | $this->identityMap[$class->rootEntityName][$idHash] = $entity; |
---|
| 2366 | |
---|
| 2367 | if ($entity instanceof NotifyPropertyChanged) { |
---|
| 2368 | $entity->addPropertyChangedListener($this); |
---|
| 2369 | } |
---|
| 2370 | |
---|
| 2371 | $overrideLocalValues = true; |
---|
| 2372 | } |
---|
| 2373 | |
---|
| 2374 | if ( ! $overrideLocalValues) { |
---|
| 2375 | return $entity; |
---|
| 2376 | } |
---|
| 2377 | |
---|
| 2378 | foreach ($data as $field => $value) { |
---|
| 2379 | if (isset($class->fieldMappings[$field])) { |
---|
| 2380 | $class->reflFields[$field]->setValue($entity, $value); |
---|
| 2381 | } |
---|
| 2382 | } |
---|
| 2383 | |
---|
| 2384 | // Loading the entity right here, if its in the eager loading map get rid of it there. |
---|
| 2385 | unset($this->eagerLoadingEntities[$class->rootEntityName][$idHash]); |
---|
| 2386 | |
---|
| 2387 | if (isset($this->eagerLoadingEntities[$class->rootEntityName]) && ! $this->eagerLoadingEntities[$class->rootEntityName]) { |
---|
| 2388 | unset($this->eagerLoadingEntities[$class->rootEntityName]); |
---|
| 2389 | } |
---|
| 2390 | |
---|
| 2391 | // Properly initialize any unfetched associations, if partial objects are not allowed. |
---|
| 2392 | if (isset($hints[Query::HINT_FORCE_PARTIAL_LOAD])) { |
---|
| 2393 | return $entity; |
---|
| 2394 | } |
---|
| 2395 | |
---|
| 2396 | foreach ($class->associationMappings as $field => $assoc) { |
---|
| 2397 | // Check if the association is not among the fetch-joined associations already. |
---|
| 2398 | if (isset($hints['fetchAlias']) && isset($hints['fetched'][$hints['fetchAlias']][$field])) { |
---|
| 2399 | continue; |
---|
| 2400 | } |
---|
| 2401 | |
---|
| 2402 | $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); |
---|
| 2403 | |
---|
| 2404 | switch (true) { |
---|
| 2405 | case ($assoc['type'] & ClassMetadata::TO_ONE): |
---|
| 2406 | if ( ! $assoc['isOwningSide']) { |
---|
| 2407 | // Inverse side of x-to-one can never be lazy |
---|
| 2408 | $class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity)); |
---|
| 2409 | |
---|
| 2410 | continue 2; |
---|
| 2411 | } |
---|
| 2412 | |
---|
| 2413 | $associatedId = array(); |
---|
| 2414 | |
---|
| 2415 | // TODO: Is this even computed right in all cases of composite keys? |
---|
| 2416 | foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { |
---|
| 2417 | $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null; |
---|
| 2418 | |
---|
| 2419 | if ($joinColumnValue !== null) { |
---|
| 2420 | if ($targetClass->containsForeignIdentifier) { |
---|
| 2421 | $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue; |
---|
| 2422 | } else { |
---|
| 2423 | $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; |
---|
| 2424 | } |
---|
| 2425 | } |
---|
| 2426 | } |
---|
| 2427 | |
---|
| 2428 | if ( ! $associatedId) { |
---|
| 2429 | // Foreign key is NULL |
---|
| 2430 | $class->reflFields[$field]->setValue($entity, null); |
---|
| 2431 | $this->originalEntityData[$oid][$field] = null; |
---|
| 2432 | |
---|
| 2433 | continue; |
---|
| 2434 | } |
---|
| 2435 | |
---|
| 2436 | if ( ! isset($hints['fetchMode'][$class->name][$field])) { |
---|
| 2437 | $hints['fetchMode'][$class->name][$field] = $assoc['fetch']; |
---|
| 2438 | } |
---|
| 2439 | |
---|
| 2440 | // Foreign key is set |
---|
| 2441 | // Check identity map first |
---|
| 2442 | // FIXME: Can break easily with composite keys if join column values are in |
---|
| 2443 | // wrong order. The correct order is the one in ClassMetadata#identifier. |
---|
| 2444 | $relatedIdHash = implode(' ', $associatedId); |
---|
| 2445 | |
---|
| 2446 | switch (true) { |
---|
| 2447 | case (isset($this->identityMap[$targetClass->rootEntityName][$relatedIdHash])): |
---|
| 2448 | $newValue = $this->identityMap[$targetClass->rootEntityName][$relatedIdHash]; |
---|
| 2449 | |
---|
| 2450 | // if this is an uninitialized proxy, we are deferring eager loads, |
---|
| 2451 | // this association is marked as eager fetch, and its an uninitialized proxy (wtf!) |
---|
| 2452 | // then we cann append this entity for eager loading! |
---|
| 2453 | if ($hints['fetchMode'][$class->name][$field] == ClassMetadata::FETCH_EAGER && |
---|
| 2454 | isset($hints['deferEagerLoad']) && |
---|
| 2455 | !$targetClass->isIdentifierComposite && |
---|
| 2456 | $newValue instanceof Proxy && |
---|
| 2457 | $newValue->__isInitialized__ === false) { |
---|
| 2458 | |
---|
| 2459 | $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId); |
---|
| 2460 | } |
---|
| 2461 | |
---|
| 2462 | break; |
---|
| 2463 | |
---|
| 2464 | case ($targetClass->subClasses): |
---|
| 2465 | // If it might be a subtype, it can not be lazy. There isn't even |
---|
| 2466 | // a way to solve this with deferred eager loading, which means putting |
---|
| 2467 | // an entity with subclasses at a *-to-one location is really bad! (performance-wise) |
---|
| 2468 | $newValue = $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity, $associatedId); |
---|
| 2469 | break; |
---|
| 2470 | |
---|
| 2471 | default: |
---|
| 2472 | switch (true) { |
---|
| 2473 | // We are negating the condition here. Other cases will assume it is valid! |
---|
| 2474 | case ($hints['fetchMode'][$class->name][$field] !== ClassMetadata::FETCH_EAGER): |
---|
| 2475 | $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId); |
---|
| 2476 | break; |
---|
| 2477 | |
---|
| 2478 | // Deferred eager load only works for single identifier classes |
---|
| 2479 | case (isset($hints['deferEagerLoad']) && ! $targetClass->isIdentifierComposite): |
---|
| 2480 | // TODO: Is there a faster approach? |
---|
| 2481 | $this->eagerLoadingEntities[$targetClass->rootEntityName][$relatedIdHash] = current($associatedId); |
---|
| 2482 | |
---|
| 2483 | $newValue = $this->em->getProxyFactory()->getProxy($assoc['targetEntity'], $associatedId); |
---|
| 2484 | break; |
---|
| 2485 | |
---|
| 2486 | default: |
---|
| 2487 | // TODO: This is very imperformant, ignore it? |
---|
| 2488 | $newValue = $this->em->find($assoc['targetEntity'], $associatedId); |
---|
| 2489 | break; |
---|
| 2490 | } |
---|
| 2491 | |
---|
| 2492 | // PERF: Inlined & optimized code from UnitOfWork#registerManaged() |
---|
| 2493 | $newValueOid = spl_object_hash($newValue); |
---|
| 2494 | $this->entityIdentifiers[$newValueOid] = $associatedId; |
---|
| 2495 | $this->identityMap[$targetClass->rootEntityName][$relatedIdHash] = $newValue; |
---|
| 2496 | $this->entityStates[$newValueOid] = self::STATE_MANAGED; |
---|
| 2497 | // make sure that when an proxy is then finally loaded, $this->originalEntityData is set also! |
---|
| 2498 | break; |
---|
| 2499 | } |
---|
| 2500 | |
---|
| 2501 | $this->originalEntityData[$oid][$field] = $newValue; |
---|
| 2502 | $class->reflFields[$field]->setValue($entity, $newValue); |
---|
| 2503 | |
---|
| 2504 | if ($assoc['inversedBy'] && $assoc['type'] & ClassMetadata::ONE_TO_ONE) { |
---|
| 2505 | $inverseAssoc = $targetClass->associationMappings[$assoc['inversedBy']]; |
---|
| 2506 | $targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($newValue, $entity); |
---|
| 2507 | } |
---|
| 2508 | |
---|
| 2509 | break; |
---|
| 2510 | |
---|
| 2511 | default: |
---|
| 2512 | // Inject collection |
---|
| 2513 | $pColl = new PersistentCollection($this->em, $targetClass, new ArrayCollection); |
---|
| 2514 | $pColl->setOwner($entity, $assoc); |
---|
| 2515 | $pColl->setInitialized(false); |
---|
| 2516 | |
---|
| 2517 | $reflField = $class->reflFields[$field]; |
---|
| 2518 | $reflField->setValue($entity, $pColl); |
---|
| 2519 | |
---|
| 2520 | if ($assoc['fetch'] == ClassMetadata::FETCH_EAGER) { |
---|
| 2521 | $this->loadCollection($pColl); |
---|
| 2522 | $pColl->takeSnapshot(); |
---|
| 2523 | } |
---|
| 2524 | |
---|
| 2525 | $this->originalEntityData[$oid][$field] = $pColl; |
---|
| 2526 | break; |
---|
| 2527 | } |
---|
| 2528 | } |
---|
| 2529 | |
---|
| 2530 | if ($overrideLocalValues) { |
---|
| 2531 | if (isset($class->lifecycleCallbacks[Events::postLoad])) { |
---|
| 2532 | $class->invokeLifecycleCallbacks(Events::postLoad, $entity); |
---|
| 2533 | } |
---|
| 2534 | |
---|
| 2535 | |
---|
| 2536 | if ($this->evm->hasListeners(Events::postLoad)) { |
---|
| 2537 | $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->em)); |
---|
| 2538 | } |
---|
| 2539 | } |
---|
| 2540 | |
---|
| 2541 | return $entity; |
---|
| 2542 | } |
---|
| 2543 | |
---|
| 2544 | /** |
---|
| 2545 | * @return void |
---|
| 2546 | */ |
---|
| 2547 | public function triggerEagerLoads() |
---|
| 2548 | { |
---|
| 2549 | if ( ! $this->eagerLoadingEntities) { |
---|
| 2550 | return; |
---|
| 2551 | } |
---|
| 2552 | |
---|
| 2553 | // avoid infinite recursion |
---|
| 2554 | $eagerLoadingEntities = $this->eagerLoadingEntities; |
---|
| 2555 | $this->eagerLoadingEntities = array(); |
---|
| 2556 | |
---|
| 2557 | foreach ($eagerLoadingEntities as $entityName => $ids) { |
---|
| 2558 | $class = $this->em->getClassMetadata($entityName); |
---|
| 2559 | |
---|
| 2560 | if ($ids) { |
---|
| 2561 | $this->getEntityPersister($entityName)->loadAll( |
---|
| 2562 | array_combine($class->identifier, array(array_values($ids))) |
---|
| 2563 | ); |
---|
| 2564 | } |
---|
| 2565 | } |
---|
| 2566 | } |
---|
| 2567 | |
---|
| 2568 | /** |
---|
| 2569 | * Initializes (loads) an uninitialized persistent collection of an entity. |
---|
| 2570 | * |
---|
| 2571 | * @param PeristentCollection $collection The collection to initialize. |
---|
| 2572 | * @todo Maybe later move to EntityManager#initialize($proxyOrCollection). See DDC-733. |
---|
| 2573 | */ |
---|
| 2574 | public function loadCollection(PersistentCollection $collection) |
---|
| 2575 | { |
---|
| 2576 | $assoc = $collection->getMapping(); |
---|
| 2577 | $persister = $this->getEntityPersister($assoc['targetEntity']); |
---|
| 2578 | |
---|
| 2579 | switch ($assoc['type']) { |
---|
| 2580 | case ClassMetadata::ONE_TO_MANY: |
---|
| 2581 | $persister->loadOneToManyCollection($assoc, $collection->getOwner(), $collection); |
---|
| 2582 | break; |
---|
| 2583 | |
---|
| 2584 | case ClassMetadata::MANY_TO_MANY: |
---|
| 2585 | $persister->loadManyToManyCollection($assoc, $collection->getOwner(), $collection); |
---|
| 2586 | break; |
---|
| 2587 | } |
---|
| 2588 | } |
---|
| 2589 | |
---|
| 2590 | /** |
---|
| 2591 | * Gets the identity map of the UnitOfWork. |
---|
| 2592 | * |
---|
| 2593 | * @return array |
---|
| 2594 | */ |
---|
| 2595 | public function getIdentityMap() |
---|
| 2596 | { |
---|
| 2597 | return $this->identityMap; |
---|
| 2598 | } |
---|
| 2599 | |
---|
| 2600 | /** |
---|
| 2601 | * Gets the original data of an entity. The original data is the data that was |
---|
| 2602 | * present at the time the entity was reconstituted from the database. |
---|
| 2603 | * |
---|
| 2604 | * @param object $entity |
---|
| 2605 | * @return array |
---|
| 2606 | */ |
---|
| 2607 | public function getOriginalEntityData($entity) |
---|
| 2608 | { |
---|
| 2609 | $oid = spl_object_hash($entity); |
---|
| 2610 | |
---|
| 2611 | if (isset($this->originalEntityData[$oid])) { |
---|
| 2612 | return $this->originalEntityData[$oid]; |
---|
| 2613 | } |
---|
| 2614 | |
---|
| 2615 | return array(); |
---|
| 2616 | } |
---|
| 2617 | |
---|
| 2618 | /** |
---|
| 2619 | * @ignore |
---|
| 2620 | */ |
---|
| 2621 | public function setOriginalEntityData($entity, array $data) |
---|
| 2622 | { |
---|
| 2623 | $this->originalEntityData[spl_object_hash($entity)] = $data; |
---|
| 2624 | } |
---|
| 2625 | |
---|
| 2626 | /** |
---|
| 2627 | * INTERNAL: |
---|
| 2628 | * Sets a property value of the original data array of an entity. |
---|
| 2629 | * |
---|
| 2630 | * @ignore |
---|
| 2631 | * @param string $oid |
---|
| 2632 | * @param string $property |
---|
| 2633 | * @param mixed $value |
---|
| 2634 | */ |
---|
| 2635 | public function setOriginalEntityProperty($oid, $property, $value) |
---|
| 2636 | { |
---|
| 2637 | $this->originalEntityData[$oid][$property] = $value; |
---|
| 2638 | } |
---|
| 2639 | |
---|
| 2640 | /** |
---|
| 2641 | * Gets the identifier of an entity. |
---|
| 2642 | * The returned value is always an array of identifier values. If the entity |
---|
| 2643 | * has a composite identifier then the identifier values are in the same |
---|
| 2644 | * order as the identifier field names as returned by ClassMetadata#getIdentifierFieldNames(). |
---|
| 2645 | * |
---|
| 2646 | * @param object $entity |
---|
| 2647 | * @return array The identifier values. |
---|
| 2648 | */ |
---|
| 2649 | public function getEntityIdentifier($entity) |
---|
| 2650 | { |
---|
| 2651 | return $this->entityIdentifiers[spl_object_hash($entity)]; |
---|
| 2652 | } |
---|
| 2653 | |
---|
| 2654 | /** |
---|
| 2655 | * Tries to find an entity with the given identifier in the identity map of |
---|
| 2656 | * this UnitOfWork. |
---|
| 2657 | * |
---|
| 2658 | * @param mixed $id The entity identifier to look for. |
---|
| 2659 | * @param string $rootClassName The name of the root class of the mapped entity hierarchy. |
---|
| 2660 | * @return mixed Returns the entity with the specified identifier if it exists in |
---|
| 2661 | * this UnitOfWork, FALSE otherwise. |
---|
| 2662 | */ |
---|
| 2663 | public function tryGetById($id, $rootClassName) |
---|
| 2664 | { |
---|
| 2665 | $idHash = implode(' ', (array) $id); |
---|
| 2666 | |
---|
| 2667 | if (isset($this->identityMap[$rootClassName][$idHash])) { |
---|
| 2668 | return $this->identityMap[$rootClassName][$idHash]; |
---|
| 2669 | } |
---|
| 2670 | |
---|
| 2671 | return false; |
---|
| 2672 | } |
---|
| 2673 | |
---|
| 2674 | /** |
---|
| 2675 | * Schedules an entity for dirty-checking at commit-time. |
---|
| 2676 | * |
---|
| 2677 | * @param object $entity The entity to schedule for dirty-checking. |
---|
| 2678 | * @todo Rename: scheduleForSynchronization |
---|
| 2679 | */ |
---|
| 2680 | public function scheduleForDirtyCheck($entity) |
---|
| 2681 | { |
---|
| 2682 | $rootClassName = $this->em->getClassMetadata(get_class($entity))->rootEntityName; |
---|
| 2683 | |
---|
| 2684 | $this->scheduledForDirtyCheck[$rootClassName][spl_object_hash($entity)] = $entity; |
---|
| 2685 | } |
---|
| 2686 | |
---|
| 2687 | /** |
---|
| 2688 | * Checks whether the UnitOfWork has any pending insertions. |
---|
| 2689 | * |
---|
| 2690 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
---|
| 2691 | */ |
---|
| 2692 | public function hasPendingInsertions() |
---|
| 2693 | { |
---|
| 2694 | return ! empty($this->entityInsertions); |
---|
| 2695 | } |
---|
| 2696 | |
---|
| 2697 | /** |
---|
| 2698 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
---|
| 2699 | * number of entities in the identity map. |
---|
| 2700 | * |
---|
| 2701 | * @return integer |
---|
| 2702 | */ |
---|
| 2703 | public function size() |
---|
| 2704 | { |
---|
| 2705 | $countArray = array_map(function ($item) { return count($item); }, $this->identityMap); |
---|
| 2706 | |
---|
| 2707 | return array_sum($countArray); |
---|
| 2708 | } |
---|
| 2709 | |
---|
| 2710 | /** |
---|
| 2711 | * Gets the EntityPersister for an Entity. |
---|
| 2712 | * |
---|
| 2713 | * @param string $entityName The name of the Entity. |
---|
| 2714 | * |
---|
| 2715 | * @return \Doctrine\ORM\Persisters\BasicEntityPersister |
---|
| 2716 | */ |
---|
| 2717 | public function getEntityPersister($entityName) |
---|
| 2718 | { |
---|
| 2719 | if (isset($this->persisters[$entityName])) { |
---|
| 2720 | return $this->persisters[$entityName]; |
---|
| 2721 | } |
---|
| 2722 | |
---|
| 2723 | $class = $this->em->getClassMetadata($entityName); |
---|
| 2724 | |
---|
| 2725 | switch (true) { |
---|
| 2726 | case ($class->isInheritanceTypeNone()): |
---|
| 2727 | $persister = new Persisters\BasicEntityPersister($this->em, $class); |
---|
| 2728 | break; |
---|
| 2729 | |
---|
| 2730 | case ($class->isInheritanceTypeSingleTable()): |
---|
| 2731 | $persister = new Persisters\SingleTablePersister($this->em, $class); |
---|
| 2732 | break; |
---|
| 2733 | |
---|
| 2734 | case ($class->isInheritanceTypeJoined()): |
---|
| 2735 | $persister = new Persisters\JoinedSubclassPersister($this->em, $class); |
---|
| 2736 | break; |
---|
| 2737 | |
---|
| 2738 | default: |
---|
| 2739 | $persister = new Persisters\UnionSubclassPersister($this->em, $class); |
---|
| 2740 | } |
---|
| 2741 | |
---|
| 2742 | $this->persisters[$entityName] = $persister; |
---|
| 2743 | |
---|
| 2744 | return $this->persisters[$entityName]; |
---|
| 2745 | } |
---|
| 2746 | |
---|
| 2747 | /** |
---|
| 2748 | * Gets a collection persister for a collection-valued association. |
---|
| 2749 | * |
---|
| 2750 | * @param AssociationMapping $association |
---|
| 2751 | * |
---|
| 2752 | * @return AbstractCollectionPersister |
---|
| 2753 | */ |
---|
| 2754 | public function getCollectionPersister(array $association) |
---|
| 2755 | { |
---|
| 2756 | $type = $association['type']; |
---|
| 2757 | |
---|
| 2758 | if (isset($this->collectionPersisters[$type])) { |
---|
| 2759 | return $this->collectionPersisters[$type]; |
---|
| 2760 | } |
---|
| 2761 | |
---|
| 2762 | switch ($type) { |
---|
| 2763 | case ClassMetadata::ONE_TO_MANY: |
---|
| 2764 | $persister = new Persisters\OneToManyPersister($this->em); |
---|
| 2765 | break; |
---|
| 2766 | |
---|
| 2767 | case ClassMetadata::MANY_TO_MANY: |
---|
| 2768 | $persister = new Persisters\ManyToManyPersister($this->em); |
---|
| 2769 | break; |
---|
| 2770 | } |
---|
| 2771 | |
---|
| 2772 | $this->collectionPersisters[$type] = $persister; |
---|
| 2773 | |
---|
| 2774 | return $this->collectionPersisters[$type]; |
---|
| 2775 | } |
---|
| 2776 | |
---|
| 2777 | /** |
---|
| 2778 | * INTERNAL: |
---|
| 2779 | * Registers an entity as managed. |
---|
| 2780 | * |
---|
| 2781 | * @param object $entity The entity. |
---|
| 2782 | * @param array $id The identifier values. |
---|
| 2783 | * @param array $data The original entity data. |
---|
| 2784 | */ |
---|
| 2785 | public function registerManaged($entity, array $id, array $data) |
---|
| 2786 | { |
---|
| 2787 | $oid = spl_object_hash($entity); |
---|
| 2788 | |
---|
| 2789 | $this->entityIdentifiers[$oid] = $id; |
---|
| 2790 | $this->entityStates[$oid] = self::STATE_MANAGED; |
---|
| 2791 | $this->originalEntityData[$oid] = $data; |
---|
| 2792 | |
---|
| 2793 | $this->addToIdentityMap($entity); |
---|
| 2794 | |
---|
| 2795 | if ($entity instanceof NotifyPropertyChanged) { |
---|
| 2796 | $entity->addPropertyChangedListener($this); |
---|
| 2797 | } |
---|
| 2798 | } |
---|
| 2799 | |
---|
| 2800 | /** |
---|
| 2801 | * INTERNAL: |
---|
| 2802 | * Clears the property changeset of the entity with the given OID. |
---|
| 2803 | * |
---|
| 2804 | * @param string $oid The entity's OID. |
---|
| 2805 | */ |
---|
| 2806 | public function clearEntityChangeSet($oid) |
---|
| 2807 | { |
---|
| 2808 | $this->entityChangeSets[$oid] = array(); |
---|
| 2809 | } |
---|
| 2810 | |
---|
| 2811 | /* PropertyChangedListener implementation */ |
---|
| 2812 | |
---|
| 2813 | /** |
---|
| 2814 | * Notifies this UnitOfWork of a property change in an entity. |
---|
| 2815 | * |
---|
| 2816 | * @param object $entity The entity that owns the property. |
---|
| 2817 | * @param string $propertyName The name of the property that changed. |
---|
| 2818 | * @param mixed $oldValue The old value of the property. |
---|
| 2819 | * @param mixed $newValue The new value of the property. |
---|
| 2820 | */ |
---|
| 2821 | public function propertyChanged($entity, $propertyName, $oldValue, $newValue) |
---|
| 2822 | { |
---|
| 2823 | $oid = spl_object_hash($entity); |
---|
| 2824 | $class = $this->em->getClassMetadata(get_class($entity)); |
---|
| 2825 | |
---|
| 2826 | $isAssocField = isset($class->associationMappings[$propertyName]); |
---|
| 2827 | |
---|
| 2828 | if ( ! $isAssocField && ! isset($class->fieldMappings[$propertyName])) { |
---|
| 2829 | return; // ignore non-persistent fields |
---|
| 2830 | } |
---|
| 2831 | |
---|
| 2832 | // Update changeset and mark entity for synchronization |
---|
| 2833 | $this->entityChangeSets[$oid][$propertyName] = array($oldValue, $newValue); |
---|
| 2834 | |
---|
| 2835 | if ( ! isset($this->scheduledForDirtyCheck[$class->rootEntityName][$oid])) { |
---|
| 2836 | $this->scheduleForDirtyCheck($entity); |
---|
| 2837 | } |
---|
| 2838 | } |
---|
| 2839 | |
---|
| 2840 | /** |
---|
| 2841 | * Gets the currently scheduled entity insertions in this UnitOfWork. |
---|
| 2842 | * |
---|
| 2843 | * @return array |
---|
| 2844 | */ |
---|
| 2845 | public function getScheduledEntityInsertions() |
---|
| 2846 | { |
---|
| 2847 | return $this->entityInsertions; |
---|
| 2848 | } |
---|
| 2849 | |
---|
| 2850 | /** |
---|
| 2851 | * Gets the currently scheduled entity updates in this UnitOfWork. |
---|
| 2852 | * |
---|
| 2853 | * @return array |
---|
| 2854 | */ |
---|
| 2855 | public function getScheduledEntityUpdates() |
---|
| 2856 | { |
---|
| 2857 | return $this->entityUpdates; |
---|
| 2858 | } |
---|
| 2859 | |
---|
| 2860 | /** |
---|
| 2861 | * Gets the currently scheduled entity deletions in this UnitOfWork. |
---|
| 2862 | * |
---|
| 2863 | * @return array |
---|
| 2864 | */ |
---|
| 2865 | public function getScheduledEntityDeletions() |
---|
| 2866 | { |
---|
| 2867 | return $this->entityDeletions; |
---|
| 2868 | } |
---|
| 2869 | |
---|
| 2870 | /** |
---|
| 2871 | * Get the currently scheduled complete collection deletions |
---|
| 2872 | * |
---|
| 2873 | * @return array |
---|
| 2874 | */ |
---|
| 2875 | public function getScheduledCollectionDeletions() |
---|
| 2876 | { |
---|
| 2877 | return $this->collectionDeletions; |
---|
| 2878 | } |
---|
| 2879 | |
---|
| 2880 | /** |
---|
| 2881 | * Gets the currently scheduled collection inserts, updates and deletes. |
---|
| 2882 | * |
---|
| 2883 | * @return array |
---|
| 2884 | */ |
---|
| 2885 | public function getScheduledCollectionUpdates() |
---|
| 2886 | { |
---|
| 2887 | return $this->collectionUpdates; |
---|
| 2888 | } |
---|
| 2889 | |
---|
| 2890 | /** |
---|
| 2891 | * Helper method to initialize a lazy loading proxy or persistent collection. |
---|
| 2892 | * |
---|
| 2893 | * @param object |
---|
| 2894 | * @return void |
---|
| 2895 | */ |
---|
| 2896 | public function initializeObject($obj) |
---|
| 2897 | { |
---|
| 2898 | if ($obj instanceof Proxy) { |
---|
| 2899 | $obj->__load(); |
---|
| 2900 | |
---|
| 2901 | return; |
---|
| 2902 | } |
---|
| 2903 | |
---|
| 2904 | if ($obj instanceof PersistentCollection) { |
---|
| 2905 | $obj->initialize(); |
---|
| 2906 | } |
---|
| 2907 | } |
---|
| 2908 | |
---|
| 2909 | /** |
---|
| 2910 | * Helper method to show an object as string. |
---|
| 2911 | * |
---|
| 2912 | * @param object $obj |
---|
| 2913 | * @return string |
---|
| 2914 | */ |
---|
| 2915 | private static function objToStr($obj) |
---|
| 2916 | { |
---|
| 2917 | return method_exists($obj, '__toString') ? (string)$obj : get_class($obj).'@'.spl_object_hash($obj); |
---|
| 2918 | } |
---|
| 2919 | |
---|
| 2920 | /** |
---|
| 2921 | * Marks an entity as read-only so that it will not be considered for updates during UnitOfWork#commit(). |
---|
| 2922 | * |
---|
| 2923 | * This operation cannot be undone as some parts of the UnitOfWork now keep gathering information |
---|
| 2924 | * on this object that might be necessary to perform a correct udpate. |
---|
| 2925 | * |
---|
| 2926 | * @throws \InvalidArgumentException |
---|
| 2927 | * @param $object |
---|
| 2928 | * @return void |
---|
| 2929 | */ |
---|
| 2930 | public function markReadOnly($object) |
---|
| 2931 | { |
---|
| 2932 | if ( ! is_object($object) || ! $this->isInIdentityMap($object)) { |
---|
| 2933 | throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object); |
---|
| 2934 | } |
---|
| 2935 | |
---|
| 2936 | $this->readOnlyObjects[spl_object_hash($object)] = true; |
---|
| 2937 | } |
---|
| 2938 | |
---|
| 2939 | /** |
---|
| 2940 | * Is this entity read only? |
---|
| 2941 | * |
---|
| 2942 | * @throws \InvalidArgumentException |
---|
| 2943 | * @param $object |
---|
| 2944 | * @return void |
---|
| 2945 | */ |
---|
| 2946 | public function isReadOnly($object) |
---|
| 2947 | { |
---|
| 2948 | if ( ! is_object($object) ) { |
---|
| 2949 | throw ORMInvalidArgumentException::readOnlyRequiresManagedEntity($object); |
---|
| 2950 | } |
---|
| 2951 | |
---|
| 2952 | return isset($this->readOnlyObjects[spl_object_hash($object)]); |
---|
| 2953 | } |
---|
| 2954 | } |
---|