在给定的生命周期回调中,是否可以修改另一个对象并将其添加到同一刷新中?最初,我没有在回调中包含第二个flush(),因此它没有被保存。我尝试添加一个,它已保存,但会发起多个呼叫
EntityOne::preFlush()
我需要添加一个标志来忽略未来的呼叫,这似乎是不对的。正确的方法是什么?
<?php
class MyService
{
public function save()
{
//...
$this->em->persist($entityTwo);
$this->em->flush();
}
}
class EntityOne
{
/**
* @var EntityTwo
*/
private $entityTwo;
private $preFlushComplete=false;
/**
* @ORM\PreFlush
*/
public function preFlush(PreFlushEventArgs $event)
{
if(!$this->preFlushComplete) {
$this->preFlushComplete=true;
$this->entityTwo->setTime(now());
$event->getEntityManager()->persist($this->entityTwo);
}
}
}