[英]Creating/persisting new document while preUpdate event in doctrine-mongodb
[英]Doctrine-MongoDB postLoad triggers preUpdate when creating new document
我有一個帶有一些加密字段的模型,在postLoad上,我將這些字段解密(工作正常),然后嘗試創建一個新文檔(記錄日志以跟蹤讀數)。
我的問題是,如果將新的Log文檔刷新到此postLoad中,則會觸發模型的preUpdate,而我不明白為什么。 模型未更改(水合加密字段未保存),即使已更改,也已更改為postLoad,它是否不應觸發其他更新?
謝謝你的想法。
(帶有alcaeus / mongo-php-adapter的php 7.1)。
編輯:添加一些精度:
postLoadListener:
public function postLoad(LifecycleEventArgs $eventArgs) {
$document = $eventArgs->getDocument();
if ($document instanceof CryptedDocumentInterface) {
$dm = $eventArgs->getDocumentManager();
$this->cryptService->uncryptDocument($document);
$this->logManager->record($document->getUser(), $document->getCryptedType(), null, null, $document->getId());
$dm->flush();
}
}
uncryptDocument方法解密了加密參數的序列化json並用它水合文檔。 這些參數是@ODM \\ NotSaved。 所以不應該更新文檔。
logManager-> record創建一個新的Log文檔(whish沒有實現CryptedDocumentInterface)並保留它。 如您所見,它被刷新到postLoad中。
在日志中,我看到正確插入了日志文檔,之后,為我讀取的加密文檔觸發了preUpdate。 這是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);
$dm = $eventArgs->getDocumentManager();
$class = $dm->getClassMetadata(get_class($document));
$dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($class, $document);
$this->logManager->record($document->getUser(), $document->getCryptedType(), $values['oldValue'], $values['newValue'], $document->getId());
}
}
所以,我通過將我的Log持久/刷新到另一個DocumentManager實例來解決這個問題。 LogManager :: record創建DM,持久化並刷新我的日志。 這樣做不會影響應用程序DM。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.