Doctrine EventListener onFlush访问旧文档



我可以访问更新的文档在onFlush eventlistener的Doctrine2。我想要完整的旧文档以旧状态存储在其他地方。

   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
        }       
    }

如何访问旧文档而不只是更改设置?

使用preUpdate事件。例子:

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
    }
    /* ... */
}

相关内容

  • 没有找到相关文章

最新更新