mongodb - Doctrine EventListener onFlush 访问旧文档

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

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

最佳答案

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

    /* ... */
}

关于mongodb - Doctrine EventListener onFlush 访问旧文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40573882/

相关文章:

facebook - FOFacebookBundle 和 FOSUserBundle 协同工作

php - 为什么 Symfony2 在基准测试中表现如此糟糕,这很重要吗?

php - 使用 Doctrine 和 Symfony2 createQueryBuilder leftJoin

node.js - 如何使用 Fixie Socks(Heroku 附加组件)通过 mongoose 连接到 mongodb

c# - 从 MongoDB 数组中删除元素

node.js - Mongo 查询(带有位置运算符)在 mongoose 中不起作用

php - Symfony2 Twig 渲染函数中的内部路由

node.js - MongoDB - 如何通过find().count()传递数据?

php - Symfony2 : Displaying session variable in a twig throw exception

mysql - Doctrine 级联更新 - 在坚持更新之前重置子实体