2013-06-03 10 views
11

Mam w aplikacji osobną funkcję nasłuchiwania. Po uruchomieniu chcę utworzyć dodatkowe rekordy. Uproszczony przykład podstawowej funkcjonalności znajduje się poniżej. W obecnej realizacji wydaje się, że nowe wydarzenia nie są utrzymywane. Czy są tu jeszcze inne telefony? Dzięki.Dodawanie dodatkowych wywołań trwałych do preUpdate wywołania w Symfony 2.1

public function preUpdate(Event\LifecycleEventArgs $eventArgs) 
{ 
    $em = $eventArgs->getEntityManager(); 
    $uow = $em->getUnitOfWork(); 
    $entity = $eventArgs->getEntity(); 

    $updateArray = $eventArgs->getEntityChangeSet(); 

    //Updates 
    if (($entity instanceof Bam) === false) { 
     $thing = new OtherThing(); 
     $thing->setFoo('bar'); 

     $uow->persist($thing); 
    } 

    $uow->computeChangeSets(); 
} 
+0

Co jest '$ historyRecord'? – cheesemacfly

+0

Typo, poprawione. – keybored

Odpowiedz

26

Najważniejsze jest, aby utrzymywać je po spłukiwania:

<?php 

namespace Comakai\CQZBundle\Handler; 

use Symfony\Component\DependencyInjection\ContainerInterface; 
use Doctrine\Common\EventSubscriber; 
use Doctrine\ORM\Event; 

/** 
* 
*/ 
class YourHandler implements EventSubscriber 
{ 
    protected $things = []; 

    public function getSubscribedEvents() 
    { 
     /** 
     * @todo Check if this is running in the console or what... 
     */ 
     if (isset($_SERVER['HTTP_HOST'])) { 

      return [ 
       'preUpdate', 
       'postFlush' 
      ]; 
     } 

     return []; 
    } 

    public function preUpdate(Event\LifecycleEventArgs $eventArgs) 
    { 
     $em = $eventArgs->getEntityManager(); 
     $uow = $em->getUnitOfWork(); 
     $entity = $eventArgs->getEntity(); 

     $updateArray = $eventArgs->getEntityChangeSet(); 

     //Updates 
     if (($entity instanceof Bam) === false) { 

      $thing = new OtherThing(); 
      $thing->setFoo('bar'); 

      $this->things[] = $thing; 
     } 
    } 

    public function postFlush(Event\PostFlushEventArgs $event) 
    { 
     if(!empty($this->things)) { 

      $em = $event->getEntityManager(); 

      foreach ($this->things as $thing) { 

       $em->persist($thing); 
      } 

      $this->things = []; 
      $em->flush(); 
     } 
    } 
} 
+1

Próbuję tego, ale mam "Błąd krytyczny PHP: osiągnięto maksymalny poziom zagnieżdżenia funkcji" 1000 "," –

+2

Wykryto błąd: Bardzo ważne jest ustawienie "$ this-> things = [];" przed "$ em-> flush();" –

+0

Przepraszam, też mi się to przytrafiło !, uaktualniając moją odpowiedź, dziękuję Igor. – coma

Powiązane problemy