我已按照说明书中的说明进行操作:http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html但 不幸的是我的 EventListener 没有被调用。
服务.yml
services:
strego.acl.eventlistener:
class: Strego\TippBundle\EventListener\AclUpdater
tags:
- { name: doctrine.event_listener, event: prePersist, connection: default }
这是我要执行的事件监听器:
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;
}
}
现在我的控制器代码,我想用它来测试它:
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");
}
我不知道为什么不调用我的 prePersist Action。有人看到我的代码有问题吗?