![](/img/trans.png)
[英]Symfony2 and Doctrine - @ORM\PostPersist() does not work
[英]Symfony2 Doctrine Listener postPersist not invoking
我一直在遵循示例代碼來嘗試使我的學說事件監聽器正常工作。 但是,即使將類實例化為一個對象(我知道這一點是因為我記錄了__construct,並且也調用了__destructor,但postPersist函數從不執行。
我的services.yml文件具有以下內容(位於AH / Core / SolutionBundle / Resources / config / 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 / Core / SolutionBundle / DependencyInjection / SolutionExtension.php中加載-已確認,因為其他服務正在正常運行)
我的實體只是一個標准的學說實體,除了使用一些額外的基於注釋的集成(例如JMS序列化器)外,沒有什么特別的。 與大多數其他實體唯一不同的是,我們使用來自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 \\ Core \\ SolutionBundle \\ Listener \\ ClientSolutionReverseSyncListener __構造AH \\ Core \\ SolutionBundle \\ Listener \\ ClientSolutionReverseSyncListener __構造
我迷失了我在這里出錯的地方,它緊隨文檔超級關閉: 學說文檔
我還檢查了此文檔,該文檔與上面的文檔類似: 圍繞偵聽器的Symfony文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.