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

Kme*_*nin 2 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()); 

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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 ?

Kme*_*nin 6

为了执行插入和更新,我不得不改变两点:

  • 使用onFlush(),而不是preFlush()
  • 每次更改后添加recomputeSingleEntityChangeSet()

新代码:

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

        }                
    }

}
Run Code Online (Sandbox Code Playgroud)