简体   繁体   中英

symfony2 doctrine mongodb - how to check a field in document is updated

In doctrine mongodb of symfony2 , is there a way to catch an even postUpdate or onFlush or preUpdate to check if a field in a document is updated? Example that I have is a document Post which has fields: Id, name, description . I have a Listener service to catch the event when Post is updated.

<?php

namespace Acme\Bundle\PostBundle\Listener;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;

public function postUpdate(LifecycleEventArgs $eventArgs)
    {
        /* @var $odm DocumentManager */
        $odm = $eventArgs->getDocumentManager();
        $object = $eventArgs->getDocument();

        if ($object instanceOf Post) {
          // how to check if description field is updated
        }
    }

During preUpdate you can check document's change set:

public function preUpdate(PreUpdateEventArgs $e)
{
    $e->getDocumentChangeSet();
    $e->hasChangedField('description')
}

Changeset is associative tuple which looks like

[ 'description' => [ 'old value', 'new value' ] ]

For more available methods you can check out PreUpdateEventArgs class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM