Doctrine EventListener onFlush访问旧Document

时间:2016-11-13 12:34:34

标签: mongodb symfony doctrine-orm event-listener odm

我可以访问Doctrine2的onFlush eventListerner中的更新文档。 我希望完整的旧文档将其存储在其他地方作为旧状态。

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

如何访问旧文档而不仅仅是更改集?

1 个答案:

答案 0 :(得分:2)

只需使用 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
    }

    /* ... */
}