UnitOfWork::getScheduledEntityUpdates doesn't return the correct entity state #6378

Closed
opened 2026-01-22 15:32:02 +01:00 by admin · 1 comment
Owner

Originally created by @pmishev on GitHub (Jan 8, 2020).

Bug Report

Q A
BC Break don't know
Version 2.6.6

Summary

When I call $unitOfWork->getScheduledEntityUpdates() in an onFlush subscriber, I expect to get the entity with its relations calculated, but I am getting an old state of that entity in my case

How to reproduce

In my controller I have:

        $media = new Media();
        $media->setName('M1');
        $em->persist($media);
        $em->flush();

        $journalist = new Journalist();
        $journalist->setName('J1');
        $em->persist($journalist);
        $em->flush();

        $jml = new JournalistMediaLink();
        $jml->setJournalist($journalist);
        $jml->setMedia($media);
        $em->persist($jml);
        $em->flush();

//        $em->clear();
//        $jml = $em->find(JournalistMediaLink::class, $jml->getId());

        $secondJournalist = new Journalist();
        $secondJournalist->setName('J2');
        $em->persist($secondJournalist);
        $em->flush();

        $jml->setJournalist($secondJournalist);

        dump($jml->getJournalist()->getName());  // Here I get 'J2' as expected

        $em->flush();

In my subscriber:

public function onFlush(OnFlushEventArgs $args)
{
...
        foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {
                if ($entity instanceof JournalistMediaLink) {
                    dump($entity->getJournalist()->getName());die; // Expecting to get 'J2' here
                }
...
}

As an output from the dumps, I expect to get:

J2
J2

But the output I get is:

J2
J1

NOTE: If I uncomment the 2 commented lines in the code above, I get the correct output that I expect.

Originally created by @pmishev on GitHub (Jan 8, 2020). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | don't know | Version | 2.6.6 #### Summary When I call `$unitOfWork->getScheduledEntityUpdates()` in an onFlush subscriber, I expect to get the entity with its relations calculated, but I am getting an old state of that entity in my case #### How to reproduce In my controller I have: ``` $media = new Media(); $media->setName('M1'); $em->persist($media); $em->flush(); $journalist = new Journalist(); $journalist->setName('J1'); $em->persist($journalist); $em->flush(); $jml = new JournalistMediaLink(); $jml->setJournalist($journalist); $jml->setMedia($media); $em->persist($jml); $em->flush(); // $em->clear(); // $jml = $em->find(JournalistMediaLink::class, $jml->getId()); $secondJournalist = new Journalist(); $secondJournalist->setName('J2'); $em->persist($secondJournalist); $em->flush(); $jml->setJournalist($secondJournalist); dump($jml->getJournalist()->getName()); // Here I get 'J2' as expected $em->flush(); ``` In my subscriber: ``` public function onFlush(OnFlushEventArgs $args) { ... foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) { if ($entity instanceof JournalistMediaLink) { dump($entity->getJournalist()->getName());die; // Expecting to get 'J2' here } ... } ``` As an output from the dumps, I expect to get: ``` J2 J2 ``` But the output I get is: ``` J2 J1 ``` NOTE: If I uncomment the 2 commented lines in the code above, I get the correct output that I expect.
admin closed this issue 2026-01-22 15:32:03 +01:00
Author
Owner

@pmishev commented on GitHub (Jan 10, 2020):

Never mind.. turned out some other subscriber was calling $em->refresh() on the entity and messing it up

@pmishev commented on GitHub (Jan 10, 2020): Never mind.. turned out some other subscriber was calling $em->refresh() on the entity and messing it up
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6378