doctrineリスナーで再帰ループを回避する1つの方法は、更新/永続化を行う前にイベントマネージャーからリスナーを削除することです。
たとえば、私が取り組んだいくつかのコードでは、次のようなものがあります。
// $evm is the Event Manager grabbed from the Entity Manager that
// is part of the Event passed to the listener function
public function removeThyself($evm)
{
$evm->removeEventListener(Events::postFlush, $this);
$evm->removeEventListener(Events::onFlush, $this);
}
public function readdTheyself($evm)
{
$evm->addEventListener(Events::postFlush, $this);
$evm->addEventListener(Events::onFlush, $this);
}
これらの関数は、リスナーが登録されているすべてのイベントからイベント リスナーを削除します。
次に、データベースに影響を与えるリスナーから何かを行う前に、これらを呼び出して、イベント リスナーが呼び出されないようにします。例えば
// $em is the Entity Manager, $evm is the Event Manager
$this->removeThyself($evm);
$em->flush($toFlush);
$this->readdTheyself($evm);