Ответы пользователя по тегу Doctrine ORM
  • Почему не получается сохранить новую сущность после preUpdate event?

    @sand_alkr
    инженер-программист
    Я все сущности для проталкивания на этапе preUpdate сохраняю в массив в свойство класса, а на 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();
            }
        }
    }
    Ответ написан
    Комментировать