Doctrine\ORM\UnitOfWork::scheduleForDelete PHP Method

scheduleForDelete() public method

INTERNAL: Schedules an entity for deletion.
public scheduleForDelete ( object $entity )
$entity object
    public function scheduleForDelete($entity)
    {
        $oid = spl_object_hash($entity);
        
        if (isset($this->entityInsertions[$oid])) {
            if ($this->isInIdentityMap($entity)) {
                $this->removeFromIdentityMap($entity);
            }
            unset($this->entityInsertions[$oid]);
            return; // entity has not been persisted yet, so nothing more to do.
        }

        if ( ! $this->isInIdentityMap($entity)) {
            return; // ignore
        }

        $this->removeFromIdentityMap($entity);

        if (isset($this->entityUpdates[$oid])) {
            unset($this->entityUpdates[$oid]);
        }
        if ( ! isset($this->entityDeletions[$oid])) {
            $this->entityDeletions[$oid] = $entity;
        }
    }

Usage Example

 /**
  * Schedule updates for routing
  *
  * @param EntityManager $em
  * @param UnitOfWork $uow
  */
 protected function updateRouting(EntityManager $em, UnitOfWork $uow)
 {
     // 302 old routes to the new 200
     foreach ($uow->getScheduledEntityUpdates() as $entity) {
         if ($entity instanceof FieldableEntity) {
             $changeSet = $uow->getEntityChangeSet($entity);
             $oldRoute = $entity->getRoute();
             // Check if we have a route. If not, create one and continue
             if (!$oldRoute instanceof Route) {
                 // create the new route
                 $oldRoute = $this->getEntityRoute($entity);
                 $entity->setRoute($oldRoute);
                 $this->computeChangeSet($em, $oldRoute);
                 $this->recomputeSingleEntityChangeSet($em, $entity);
             }
             // Check if the route has been manually updated
             $newRoute = $this->getEntityRoute($entity);
             // if the route changed, update it
             if ($newRoute->getPath() !== $oldRoute->getPath()) {
                 // create the new route entity
                 $entity->setRoute($newRoute);
                 $this->computeChangeSet($em, $newRoute);
                 // set any old route to redirect to the new route
                 $this->redirectRoute($oldRoute);
                 $this->recomputeSingleEntityChangeSet($em, $oldRoute);
             }
             if (isset($changeSet['deletedOn'])) {
                 if ($changeSet['deletedOn'] instanceof \DateTime) {
                     // delete
                     $this->deletedRoute($oldRoute);
                     $this->recomputeSingleEntityChangeSet($em, $oldRoute);
                 } else {
                     // un-delete
                     $newRoute = $this->getEntityRoute($entity);
                     $entity->setRoute($newRoute);
                     $uow->scheduleForDelete($oldRoute);
                     $this->computeChangeSet($em, $newRoute);
                     $this->recomputeSingleEntityChangeSet($em, $oldRoute);
                 }
             }
             $em->persist($entity);
             $this->recomputeSingleEntityChangeSet($em, $entity);
         }
     }
 }