我一直在使用下面的示例代碼來嘗試讓我的doctrine事件監聽器工作。但是,即使該類正在被實例化爲一個對象(我知道這是因爲我記錄了__construct,並且__destructor也被調用了,但postPersist函數從來沒有這樣做過。Symfony2 Doctrine監聽器postPersist未調用
我的services.yml文件包含以下內容(位於AH /核心/ SolutionBundle /資源/配置/ services.yml):
solutions_core_reverse_sync:
class: AH\Core\SolutionBundle\Listener\ClientSolutionReverseSyncListener
arguments: [@service_container]
tags:
- { name: doctrine.event_listener, event: postPersist }
(此外,services.yml文件被在AH /核心/ SolutionBundle/DependencyInjection/SolutionExtension.php加載 - 證實,因爲其他服務正在運行就好)
我的實體只是一個標準的學說實體,沒有什麼特別的,exc ept使用一些額外的基於註釋的集成,如JMS Serializer。唯一與大多數其他實體不同的是,我們使用Doctrine的標準SingleTableInheritence,使用@ORM \ DiscriminatorMap註釋和子實體。
我的聽衆只有一個骷髏,現在,以測試它是否工作沒有任何干擾:
<?php
namespace AH\Core\SolutionBundle\Listener;
use Symfony\Component\DependencyInjection\Container;
use Doctrine\ORM\Event\LifecycleEventArgs;
class ClientSolutionReverseSyncListener
{
protected $container;
public function __construct(Container $container)
{
$this->container = $container;
echo __CLASS__.' __construct'.PHP_EOL;
}
public function postPersist(LifecycleEventArgs $args)
{
echo __CLASS__.' postPersist fired'.PHP_EOL;
}
public function __destruct()
{
echo __CLASS__.' __destruct'.PHP_EOL;
}
}
當測試它,運行下面的代碼,我只看到了__construct和__destruct運行(通過呼應的方式),但不是postPersist:
$cs = $csm->findClientSolutionById(123); // don't worry where $csm comes from
$cs->setUid('do some update: '.rand(0,10000));
$this->em->persist($cs);
輸出示例:
AH \核心\ Soluti onBundle \監聽\ ClientSolutionReverseSyncListener __construct AH \核心\ SolutionBundle \監聽\ ClientSolutionReverseSyncListener __destruct
我在我去錯在這裏,其中的損失,它遵循超近的文檔: Doctrine documentation
我還檢查了這個文檔,它類似於上面: Symfony documentation around listeners
不同的方式 - 你爲什麼不試試這個 - 只需添加功能實體和映射到陽明/ XML或註釋鏈接(http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest /reference/events.html#lifecycle-callbacks) –
做'$這個 - > EM->的flush();'也真正堅持你的實體 – Ziumin
感謝@Ziumin是的,我試過了,雖然postPersist實際上應該沖洗之前進行發射。此外,當試圖我仍然有相同的結果。 – Sarel