【发布时间】:2014-12-13 15:52:30
【问题描述】:
我创建了一个这样的事件:
service.yml
AccountManager:
class: %AccountManager.class%
tags:
- { name: doctrine.event_listener, event: postPersist }
我的订阅者
class AccountManager implements EventSubscriber
{
public function getSubscribedEvents()
{
return array(
'postPersist',
'postUpdate',
);
}
public function postUpdate(LifecycleEventArgs $args)
{
$this->index($args);
}
public function postPersist(LifecycleEventArgs $args)
{
$this->index($args);
}
public function index(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager(); //BREAKPOINT
if ($entity instanceof User) {
echo $entity;
}
}
}
如果我看到我添加断点的代码,$entityManager = $args->getEntityManager(); 我在我的数据库中找不到新用户。毕竟我在数据库中有新用户。
从文档学说,我可以阅读
postPersist - postPersist 事件发生在实体之后 实体已被持久化。数据库后调用 插入操作。生成的主键值在 postPersist 事件。
我的问题是
- 为什么在活动期间我的数据库中没有新用户?
- 我不明白 Listener/Subscriber 之间的主要区别。在这种情况下,哪个更好?
【问题讨论】:
标签: php symfony doctrine-orm doctrine