Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 : preFlush getScheduledEntityInsertions() works but not getScheduledEntityUpdates()

Tags:

doctrine-orm

I have some entities with abstract class: EntityDated wich mean that the entity contain 4 commons fields: created, updated, created_by and updated_by. I want update the 4 data when I create entity and update 'updated' and 'updated_by' when I update the entity.

I made a service calling my listener:

public function preFlush(PreFlushEventArgs $eventArgs)
    {
        $token = $this->container->get('security.context')->getToken();
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();

        // Inserts
        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {

                $entity->setCreated(new \Datetime()); 
                $entity->setCreatedBy($token->getUser()->getUsername());
                $entity->setUpdated(new \Datetime()); 
                $entity->setUpdatedBy($token->getUser()->getUsername()); 

            }
        }

        // Updates
        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {

                $entity->setUpdated(new \Datetime()); 
                $entity->setUpdatedBy($token->getUser()->getUsername()); 

            }
        }
    }

This works only for INSERTS not for UPDATES (changes are saved but updated and updatedBy stay the same). If i debug $uow->getScheduledEntityUpdates(), I see that it's empty. Why updated entities are not managed here?

like image 828
KmeCnin Avatar asked Feb 02 '26 04:02

KmeCnin


1 Answers

In order to perform both Inserts and Updates I had to change two points :

  • Use onFlush(), not preFlush()
  • Add recomputeSingleEntityChangeSet() after each change

The new code :

public function onFlush(OnFlushEventArgs $eventArgs)
{

    $token = $this->container->get('security.context')->getToken();
    $em = $eventArgs->getEntityManager();
    $uow = $em->getUnitOfWork();

    // Inserts
    foreach ($uow->getScheduledEntityInsertions() as $entity) {
        if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {

            $entity->setCreated(new \Datetime()); 
            $entity->setCreatedBy($token->getUser()->getUsername());
            $entity->setUpdated(new \Datetime()); 
            $entity->setUpdatedBy($token->getUser()->getUsername()); 

            $meta = $em->getClassMetadata(get_class($entity));
            $uow->recomputeSingleEntityChangeSet($meta, $entity);

        }
    }

    // Updates
    foreach ($uow->getScheduledEntityUpdates() as $entity) {
        if (is_subclass_of($entity, 'Kiwi\Bundle\TrainingBundle\Entity\EntityDated')) {

            $entity->setUpdated(new \Datetime()); 
            $entity->setUpdatedBy($token->getUser()->getUsername());

            $meta = $em->getClassMetadata(get_class($entity));
            $uow->recomputeSingleEntityChangeSet($meta, $entity); 

        }                
    }

}
like image 186
KmeCnin Avatar answered Feb 04 '26 16:02

KmeCnin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!