2016-11-13 54 views
2

我可以訪問Doctrine2的onFlush eventListerner中的更新文檔。 我想完整的舊文檔將它作爲舊狀態存儲在別處。Doctrine EventListener onFlush訪問舊文檔

public function onFlush(OnFlushEventArgs $eventArgs) 
    { 
     $dm = $eventArgs->getDocumentManager(); 

     $uow = $dm->getUnitOfWork(); 
     foreach ($uow->getScheduledDocumentUpdates() as $document) { 
      // $document is updated document 
      // $changeSet contains only new and old values 
      $changeSet = $uow->getDocumentChangeSet($document); 

      // I want the whole old document object as $oldDocument 
     }  
    } 

如何訪問舊文檔而不僅僅是更改集?

+0

你應該聽['preUpdate'(http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate)事件:在那裏你可以得到你需要的東西。 – DonCallisto

回答

2

只需使用預更新事件。例如:

public function preUpdate(PreUpdateEventArgs $event) 
{ 
    $entity = $event->getEntity(); // the whole entity 
    $changeSet = $event->getEntityChangeSet(); // only changed properties 

    // check if password has been changed 
    if ($event->hasChangedField('password')) { 
     // do stuff 
    } 

    /* ... */ 
}