Within a given lifecycle callback, can another object be modified and added to the same flush? Originally, I didn't include a second flush() in the callback and it wasn't being saved. I try adding one and it is saved but initiates multiple calls to EntityOne::preFlush()
and I needed to add a flag to ignore the future calls which doesn't seem right. What is the correct way to do this?
<?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);
}
}
}