DDC-3356: Event/Entity Listener onFlush() works but not preFlush() #4145

Open
opened 2026-01-22 14:36:07 +01:00 by admin · 0 comments
Owner

Originally created by @doctrinebot on GitHub (Oct 18, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user Max:

preFlush() example below don't do anything at all although it is 99% exactly the same as onFlush() which works as expected. Can anyone tell me what I am missing or what the reason is and the solution?

DEBUGGING TEST:

public function preFlush(PreFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); echo '1'; foreach ($uow->getScheduledEntityUpdates() as $entity) { echo '2'; if ($entity instanceof User) { echo '3'; } } exit; }

DEBUGGING RESULT:

1 so $uow->getScheduledEntityUpdates() is an empty array!

    entity.event_listener.user:
        class:  Site\FrontBundle\EventListener\Entity\UserListener
        tags:
            - { name: doctrine.event_listener, event: preFlush }
            - { name: doctrine.event_listener, event: onFlush }```

DOESN'T WORK - preFlush():

```class UserListener
{
    public function preFlush(PreFlushEventArgs $args)
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof User) {
                $userLog = new UserLog();
                $userLog->setDescription($entity->getId() . ' being updated.');

                $em->persist($userLog);
                $userLogMetadata = $em->getClassMetadata(get_class($userLog));
                $uow->computeChangeSet($userLogMetadata, $userLog);
            }
        }
    }
}```

WORKS - onFlush():

```class UserListener
{
    public function onFlush(OnFlushEventArgs $args)
    {
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if ($entity instanceof User) {
                $userLog = new UserLog();
                $userLog->setDescription($entity->getId() . ' being updated.');

                $em->persist($userLog);
                $userLogMetadata = $em->getClassMetadata(get_class($userLog));
                $uow->computeChangeSet($userLogMetadata, $userLog);
            }
        }
    }
}```
Originally created by @doctrinebot on GitHub (Oct 18, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user Max: preFlush() example below don't do anything at all although it is 99% exactly the same as onFlush() which works as expected. Can anyone tell me what I am missing or what the reason is and the solution? DEBUGGING TEST: `public function preFlush(PreFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); echo '1'; foreach ($uow->getScheduledEntityUpdates() as $entity) { echo '2'; if ($entity instanceof User) { echo '3'; } } exit; }` DEBUGGING RESULT: `1` so `$uow->getScheduledEntityUpdates()` is an empty array! `````` services: entity.event_listener.user: class: Site\FrontBundle\EventListener\Entity\UserListener tags: - { name: doctrine.event_listener, event: preFlush } - { name: doctrine.event_listener, event: onFlush }``` DOESN'T WORK - preFlush(): ```class UserListener { public function preFlush(PreFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); foreach ($uow->getScheduledEntityUpdates() as $entity) { if ($entity instanceof User) { $userLog = new UserLog(); $userLog->setDescription($entity->getId() . ' being updated.'); $em->persist($userLog); $userLogMetadata = $em->getClassMetadata(get_class($userLog)); $uow->computeChangeSet($userLogMetadata, $userLog); } } } }``` WORKS - onFlush(): ```class UserListener { public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); foreach ($uow->getScheduledEntityUpdates() as $entity) { if ($entity instanceof User) { $userLog = new UserLog(); $userLog->setDescription($entity->getId() . ' being updated.'); $em->persist($userLog); $userLogMetadata = $em->getClassMetadata(get_class($userLog)); $uow->computeChangeSet($userLogMetadata, $userLog); } } } }``` ``````
admin added the Bug label 2026-01-22 14:36:07 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4145