Doctrine-MongoDB postLoad triggers preUpdate when creating new document

by Spope   Last Updated March 18, 2019 09:26 AM

I have a model with some crypted fields, on postLoad I uncrypt the fields (works well) and I try to create a new document (a log to keep track of the reading).

My problem is, if I flush my new Log document into this postLoad, a preUpdate for my model is triggered, and I don't understand why. The model hasn't changed (the hydrated crypted fields are NotSaved), and even if it has changed, into a postLoad it should not trigger another update ?

Thanks for your ideas.

(php 7.1 with alcaeus/mongo-php-adapter).

EDIT : Adding some precisions :

The postLoadListener :

public function postLoad(LifecycleEventArgs $eventArgs) {
    $document = $eventArgs->getDocument();

    if ($document instanceof CryptedDocumentInterface) {
        $dm = $eventArgs->getDocumentManager();
        $this->logManager->record($document->getUser(), $document->getCryptedType(), null, null, $document->getId());

The uncryptDocument method uncrypt a serialized json of the crypted parameters and hydrate the document with it. Those parameters are @ODM\NotSaved. So the document should be updated.

The logManager->record create a new Log document (whish doesn't implement CryptedDocumentInterface) persist and persist it. As you can see it is flushed into the postLoad.

In the logs I see that the Log document is correctly inserted and after that, a preUpdate is triggered for the crypted document I read. Here is the preUpdate :

public function preUpdate(LifecycleEventArgs $eventArgs) {
    $document = $eventArgs->getDocument();

    if ($document instanceof CryptedDocumentInterface) {
        $this->monolog->debug(__METHOD__ . ' ' . get_class($document) . ' id : ' . $document->getId()); // The id of the document I read.
        $values = $this->cryptService->cryptDocument($document);

        $this->logManager->record($document->getUser(), $document->getCryptedType(), $values['oldValue'], $values['newValue'], $document->getId());

Related Questions

Updated March 24, 2019 21:26 PM

Updated February 28, 2019 08:26 AM

Updated June 12, 2017 16:26 PM

Updated October 23, 2016 08:11 AM