I've created a entity listener for a given entity Foo, which listens to the preFlush event. I want to create a new Bar entity whenever the Foo entity is updated or created.
My problem is the preFlush event is triggered again by the computeChangeSets() resulting in an endless loop. Anyone have a solution or an alternative method to approach this use case?
class SomeListener
{
/**
* @ORM\preFlush
*/
public function onPreFlush(Foo $foo, PreFlushEventArgs $eventArgs)
{
$em = $eventArgs->getEntityManager();
$uow = $em->getUnitOfWork();
$bar = new Bar();
$bar->setX('test');
$foo->addBar($bar);
$em->persist($bar);
$meta = $em->getClassMetadata(get_class($foo));
$uow->recomputeSingleEntityChangeSet($meta, $foo);
$uow->computeChangeSets(); //This line invokes preFlush listener again
}
}
The PreFlush event is dispatched before the change-sets are computed.
In other words: Your calls to recomputeSingleEntityChangeSet() and computeChangeSets() are not needed here. Remove those and your listener should be working fine!
PS: computeChangeSets() will compute all change-sets, so you don't need to call recomputeSingleEntityChangeSet() along with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With