m0c*_*m0c 4 symfony doctrine-orm symfony-2.1
我遵循了Cookboock中的说明:http ://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html, 但不幸的是我的EventListener没有被调用。
service.yml
services:
strego.acl.eventlistener:
class: Strego\TippBundle\EventListener\AclUpdater
tags:
- { name: doctrine.event_listener, event: prePersist, connection: default }
Run Code Online (Sandbox Code Playgroud)
这是我执行的事件监听器:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在是我要测试的控制器代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么不要求我的坚持前行动。有人看到我的代码有问题吗?
prePersist首次保存实体时会调用。在控制器中,您正在更新现有的控制器。
放preUpdate,并尝试或从编辑到创建新的实体控制人变更情况。您可能不需要config.yml中的connection:default。
| 归档时间: |
|
| 查看次数: |
3010 次 |
| 最近记录: |