2012-10-05 48 views
-2

你好,我腦水腫的問題這段代碼:監聽週期回調

ListenerCrud.php

<?php 

namespace mio\mioBundle; 

use Doctrine\ORM\Event\PreUpdateEventArgs; 
use Doctrine\ORM\EntityManager; 
use mio\mioBundle\Entity\Log; 
use Symfony\Component\HttpFoundation\Response; 

class ListenerCrud{ 

    public function preUpdate(PreUpdateEventArgs $eventArgs){ 

     if ($eventArgs->getEntity() instanceof Usuario){ 
     $entity = $eventArgs->getEntity(); 
     $entityManager = $eventArgs->getEntityManager(); 
     $log = new Log(); 
     $entityManager->persist($log); 
     $entityManager->flush(); 
     } 
    } 
} 
?> 

config.yml:

listenercrud: 
    class: mio\mioBundle\ListenerCrud 
    tags: 
     - { name: doctrine.event_listener, event: preUpdate} 

但沒有創建日誌數據庫。幫助我please.thanks

回答

3

在這種情況下$ entityManager-> persist()不起作用,因爲他不能在preUpdate事件中調用。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate

也許你可以使用postUpdate事件 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#postupdate-postremove-postpersist

+0

那麼,如何是否被允許同花順更新前更改保存到數據庫?我需要在PostUpdate中保存getOldValue($ fieldName)。謝謝。 – paradita