2014-10-11 17 views
0

事件偵聽器示例對prePersist()postPersist()工作正常,但瀏覽器超時爲preUpdate()postUpdate()。任何人都知道這是爲什麼發生?對象的preUpdate()事件的事件偵聽器的一個非常簡單的示例掛起/失敗

注意:事件偵聽器是導致問題的原因,因爲控制器在單獨使用時工作正常。我檢查了數據庫。

錯誤:

Maximum execution time of 30 seconds exceeded in /var/www/html/local/listener/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 685 

事件監聽

class UserListener 
{ 
    //public function postUpdate(LifecycleEventArgs $args) 
    public function preUpdate(LifecycleEventArgs $args) 
    { 
     $entity = $args->getEntity(); 

     if ($entity instanceof User) { 
      $userLog = new UserLog(); 
      $userLog->setDescription('User Update.'); 

      $em = $args->getEntityManager(); 
      $em->persist($userLog); 
      $em->flush(); 
     } 
    } 
} 

控制器:

public function updateUser() 
{ 
    $repo = $this->getDoctrine()->getRepository('SiteFrontBundle:User'); 
    $user = $repo->findOneBy(array('id' => 1)); 
    $user->setLock(true); 

    $em = $this->getDoctrine()->getManager(); 
    $em->flush(); 
} 

Service.yml

services: 

    entity.event_listener.user: 
     class: Site\FrontBundle\EventListener\Entity\UserListener 
     tags: 
      - { name: doctrine.event_listener, event: preUpdate } 

回答

1

preUpdate非常有限。

來源:http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#preupdate

Any calls to EntityManager#persist() or EntityManager#remove(), 
even in combination with the UnitOfWork API are strongly discouraged and 
don’t work as expected outside the flush operation. 

可能需要使用onFlush,http://doctrine-orm.readthedocs.org/en/latest/reference/events.html#onflush

也可能想看看這些傢伙是怎麼做的:https://github.com/Atlantic18/DoctrineExtensions/tree/master/lib/Gedmo/Loggable

或者只安裝一個日誌實體管理器。

+0

OnFlush()現在會做。謝謝。 – BentCoder 2014-10-11 20:47:05