Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event Listener not registered or called on Doctrine Event

I have followed the instructions in the cookboock : http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html but unfortunately my EventListener does not get called.

service.yml

services:
      strego.acl.eventlistener:
        class: Strego\TippBundle\EventListener\AclUpdater
        tags:
            - { name: doctrine.event_listener, event: prePersist, connection: default }

And here is my Eventlistener to execute:

namespace Strego\TippBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class AclUpdater implements ContainerAwareInterface
{

    protected $container;


    public function prePersist(LifecycleEventArgs $args) {
        $args->idonotexist(); // should crash if called
        }
    }

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

and now my controller code, with which i want to test it:

public function testAction($id){
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('StregoTippBundle:BetGroup')->find($id);
        $entity->setUpdatedAt(new \DateTime());
        $entity->setTitle('update Name! #2');
        $em->persist($entity);
        $em->flush();         
        return new \Symfony\Component\HttpFoundation\Response("done");
    }

I have no Idae why my prePersist Action is not called. Does anyone see the issue with my code?

like image 803
m0c Avatar asked Nov 02 '12 12:11

m0c


1 Answers

prePersist is called when you save entity for the first time. In controller, you are updating existing one.

Put preUpdate and try that or in controller change from editing to creating new entity. You probably don't need connection:default in config.yml.

like image 123
Zeljko Avatar answered Nov 16 '22 21:11

Zeljko