Search code examples
phpsymfonydoctrine-ormunit-of-worketag

Get scheduled extra updates from Doctrine UnitOfWork


I have a Doctrine Event Listener that listens to the onFlush event. I use it to update an eTag on an entity when saved.

I need to get access to the entities scheduled for deletion, so I can access their associated object, however:

I'm using a soft-deletable filter, so the entities aren't actually in $uow->getScheduledEntityDeletions(), they're in $uow->extraUpdates marking the deleted flag changed instead.

This variable is private and I don't know of any programatic way to get notified of this change. Any ideas?

private function updateIfRequired(OnFlushEventArgs $args)
{
    $em     = $args->getEntityManager();
    $uow    = $em->getUnitOfWork();



    // Entities either updated or just inserted
    $upsertedEntities = array_merge(

        $uow->getScheduledEntityUpdates(),
        $uow->getScheduledEntityInsertions()
    );

    foreach ($upsertedEntities as $entity) {

        if ($entity instanceof ETaggableInterface || $entity instanceof ETagRelatedInterface) {
            $this->updateETag($entity);
        }
    }

    // When soft-deleted, this and getScheduledEntityUpdates are both empty!
    $deletedEntities = $uow->getScheduledEntityDeletions();

    foreach ($deletedEntities as $entity) {

        $this->deletedEntities[spl_object_hash($entity)] = $entity;
        $this->updateETag($entity);
    }

}

Solution

  • Maybe you should listen to preSoftDelete events: https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/SoftDeleteable/SoftDeleteableListener.php#L74