6

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?

1 Answer 1

6

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.

3
  • Thanks for the help, how is it possible that the event is also triggered during DoctrineFixture load? Even though the listener is registered, the listener is not triggered.
    – m0c
    Commented Nov 10, 2012 at 15:33
  • I am not sure I understand so just to clarify; you are telling that DoctrineFixtures does not trigger prePersist() listener? Honestly, I have no idea why that would happen but I am not using it for a long time. I forgot the reasons why but maybe this was one of them. Suggestion: if it doesn't work right, don't use it. All my test classes extend my own TestCase which then extends Phpunit. In that middle class, I put fixtures using regular $em->persist($entity) and all listeners are triggered.
    – Zeljko
    Commented Nov 10, 2012 at 19:05
  • Exactly, within Doctrinefixtures my listeners are not triggered. So you are using testclasses to load fixtures? I would prefer to still use Doctrinefixtures, because i currently have all my fixtures build like that with references.
    – m0c
    Commented Nov 11, 2012 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.