Search code examples
doctrine-orm

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


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?


Solution

  • 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); 
    
            }                
        }
    
    }