douji1999 2014-05-29 07:23
浏览 117

在doctrine-mongodb中的preUpdate事件中创建/持久保存新文档

I'm using doctrine-mongodb-odm-1.0.0-BETA10 and trying to provide some custom logic based on \InitialDocument while preUpdate event running.

Lets say \InitialDocument obtained some state which must behave as initial one for new \StateDocument. I'm doing something like this:

class InitDocListener implements \Doctrine\Common\EventSubscriber {
    public function getSubscribedEvents()
    {
        return [
            Events::preUpdate
        ];
    }

    public function preUpdate($args){
        $document = $args->getDocument();
        if($document instanceOf InitialDocument && $document->getState() == 'mine'){
            $stateDocument = new \StateDocument();
            $stateDocument->setInitDocument($document);
            $args->getDocumentManager()->persist($stateDocument);
            //no flush cause recursion happens
        }
    }

}

prePersist event by \StateDocument happens, but it will not persist new document in db. and postPersist event accordingly will never be fired.

There is some more custom logic but all in event scope. At some point that logic may throw an Exception which must stop update event of InitialDocument so InitialDocument state depends of \StateDocument creation process at business scope.

How can I solve this problem? preFlush event to run before changeSet recalculation do not determine InitialDocument instance. So it is some kind of trick to "search" updates at preFlush and cause me to think it is not proper way. Please advise me in proper one. Thanks.

  • 写回答

1条回答 默认 最新

  • douyan2680 2014-05-29 15:29
    关注

    I created a test case for your use case here. One thing that stood out from the code in your question was that you weren't calling recomputeSingleDocumentChangeSet() on the document you were modifying during the lifecycle callback, as is mentioned in the preUpdate documentation. But even with that call, the new document will not be inserted. This is due to the fact that UnitOfWork executes updates after insertions and upserts. The full order can be seen in UnitOfWork's commit() method:

    • Document upserts
    • Document inserts
    • Document updates
    • Extra updates (scheduled internally by persister classes)
    • Collection deletions
    • Collection updates
    • Document deletions

    When the preUpdate event is dispatched, upserts/inserts for new documents have already happened. Even with a call to recomputeSingleDocumentChangeSet(), you end up scheduling the document for insertion but UnitOfWork ignores this and ultimately unsets it when clearing out all of the schedule queues here.

    While a simple solution would be for ODM to check for additional inserts after processing updates, that could lead to an infinite loop in some situations. The UnitOfWork ordering predates my work on the project, but I the risk of looping might have been a concern when the original implementation was conceived.

    As a work-around, you may want to have the listener dump the new documents to be inserted into some other container (or the listener itself) and then check after the fact for additional documents to be persisted/flushed.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)