Answer the question
In order to leave comments, you need to log in
Why can't save a new entity after preUpdate event?
Event
public function preUpdate(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof Order) {
if ($args->hasChangedField('status') && $args->getNewValue('status') == 'stock') {
$this->container->get('activity_logger')->writeLog($entity, 'purchase');
}
}
public function writeLog ($object, $comment)
{
$entity = new Stock();
$entity->setCategory($object->getIsotope()->getCategory()->getId());
$entity->setComment($comment);
$entity->setDate(new \DateTime('now'));
$entity->setUser($object->getUser()->getId());
$entity->setChange(TRUE);
$this->em->persist($entity);
$this->em->flush();
}
Answer the question
In order to leave comments, you need to log in
found a not very nice solution
$sql = "INSERT INTO table (field1, field2) VALUES ('foo', 'var')";
$stmt = $em->getConnection()->prepare($sql);
$stmt->bindValue('invoice', $invoiceId);
$result = $stmt->execute();
I save all the entities for pushing at the preUpdate stage into an array into a class property, and pull them out and push them on postFlush.
class PurchaseListener {
protected $notifications = array();
public function preUpdate(LifecycleEventArgs $args) {
$entity = $args->getEntity();
$em = $args->getEntityManager();
if ($entity instanceof Purchase) {
...
$notification = new Notification();
$notification->setUser($entity->getUser())
->setText('блабла')
->setType('success');
$this->notifications[] = $notification;
...
}
}
public function postFlush(PostFlushEventArgs $event)
{
if(count($this->notifications)>0) {
$em = $event->getEntityManager();
foreach ($this->notifications as $thing) {
$em->persist($thing);
}
$this->notifications = array();
$em->flush();
}
}
}
The point is that preUpdate is called inside flush. And inside preUpdate, flush is called again through the logger. You need to either execute "raw" sql without ORM, or manually call the changeset calculation instead of flush:
$this->em->persist($entity);
$meta = $this->em->getClassMetadata(get_class($entity));
$this->em->getUnitOfWork()->computeChangeSet($meta, $entity);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question