简体   繁体   中英

preupdate event doctrine

I have this code but I have a problem to insert object $modificacion in database.

class ListenerCrud{

    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function preUpdate(PreUpdateEventArgs $eventArgs){

        if ($eventArgs->getEntity() instanceof Usuario) {

            $em = $eventArgs->getEntityManager();
            $modificacion = new Modificacion();
            $modificacion->setFechamod(new \DateTime('now'));
            $modificacion->setEntidad('Usuario');
            $modificacion->setTipo('uupdate');
            $securityContext = $this->container->get('security.context');
            $modificacion->setEmpleado($securityContext->getToken()->getUser());
            $modificacion->setInfo('hi');
            $em->persist($modificacion);
            $classMetadata = $em->getClassMetadata(get_class($modificacion));
            $em->getUnitOfWork()->computeChangeSet($classMetadata, $modificacion);
        }
    }
} 

In the config.yml , I have:

listenercrud:
    class: mio\mioBundle\ListenerCrud
    arguments: [@service_container]
    tags:
        - { name: doctrine.event_listener, event: PreUpdate}

The event name, in your YAML file, should be "preUpdate", not "PreUpdate".

listenercrud:
    class: mio\mioBundle\ListenerCrud
    arguments: [@service_container]
    tags:
        - { name: doctrine.event_listener, event: preUpdate }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM