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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With