我創造了這樣一個事件:主義事件post_persist
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事件中可用。
我的問題是
- 我爲什麼不是我的事件時有我的數據庫新用戶?
- 我不明白聽者/訂閱者之間的主要區別。在這種情況下,有什麼更好的?
完美的Crozin!這就是我一直在尋找的東西 – Twinsen 2014-12-13 16:54:05