Doctrine EventListener onFlush 获取旧 Document

Doctrine EventListener onFlush access to old Document

我可以在 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
        }       
    }

我如何才能访问旧文档而不只是更改集?

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

    /* ... */
}