Events for Doctrine

0

Well, I have a question in doctrine, could anyone help me.

I have 3 fields being (date delete, date change and date inclusion), however I have to work in a dynamic way with them not to be remounting the code, I am using the doctrine events method being them, preRemove, prePersist and preUpdate, where the preRemove I will update the field date delete prePersist the registration field and the preUpdate the change field.

But the start and change fields of the entity are working, so when I go to preRemove it is not, it duplicates the registry and this is not what I would like, could anyone help me?

Below is my event code.

<?php
namespace User\Event;

use Zend\ServiceManager\ServiceManager;
use Zend\Authentication\AuthenticationServiceInterface;
use User\Entity;

class Events
{
    protected static $TEMPO_UPDATE;

    private $sm;

    public function __construct(ServiceManager $sm){
        $this->sm = $sm;
    }

    private function getUsuario(){
        $authService = $this->sm->get(AuthenticationServiceInterface::class);
        if($authService->hasIdentity()){
            return $authService->getIdentity();
        }
    }

    public function preRemove($eventArgs){
        $entity = $eventArgs->getEntity();
        $em = $eventArgs->getEntityManager();

        if (method_exists($entity, 'setDtExc')) {
            self::$TEMPO_UPDATE = $entity->getDtAlt();
            $entity->setDtAlt(-1);
            $entity->setDtExc(TEMPO);
            $em->persist($entity);
            $em->flush($entity);
            $em->detach($entity);
        }

        if (method_exists($entity, 'setUsuario') && !$entity instanceof Entity\Usuario && !$entity instanceof Entity\UsuarioPermissao && !$entity instanceof Entity\Grupo && !$entity instanceof Entity\GrupoPermissao) {
            $entity->setUsuario($this->getUsuario());
        }
    }

    public function prePersist($eventArgs)
    {
        $entity = $eventArgs->getEntity();
        if (method_exists($entity, 'setDtIni')) {
            $entity->setDtIni(TEMPO);
        }

        if (method_exists($entity, 'setUsuario') && !$entity instanceof Entity\Usuario && !$entity instanceof Entity\UsuarioPermissao && !$entity instanceof Entity\Grupo && !$entity instanceof Entity\GrupoPermissao) {
            $entity->setUsuario($this->getUsuario());
        }
    }

    public function preUpdate($eventArgs)
    {
        $entity = $eventArgs->getEntity();
        if (method_exists($entity, 'setDtAlt')) {
            switch($entity->getDtAlt()){
                case -1:
                $entity->setDtAlt(self::$TEMPO_UPDATE);
                break;
                default:
                $entity->setDtAlt(TEMPO);
            }
        }

        if (method_exists($entity, 'setUsuario') && !$entity instanceof Entity\Usuario && !$entity instanceof Entity\UsuarioPermissao && !$entity instanceof Entity\Grupo && !$entity instanceof Entity\GrupoPermissao) {
            $entity->setUsuario($this->getUsuario());
        }
    }
}

?>
    
asked by anonymous 19.01.2018 / 17:13

0 answers