doufu1970 2016-12-04 19:12
浏览 20
已采纳

在Doctrine ODM PreUpdate事件中更改另一个文档

Fairly new at MongoDB and Doctrine both. I'm currently setting up a model system for my framework, and trying to implement the events.

The idea is this: When a certain model gets updated, a new user (also a model) should be created/updated. I do this all in the PreUpdate event so I can catch the changes made (check if the e-mail address on the model has been changed).

Creating and persisting a new model in this event works like a charm, however when I try to update a user I'm unable to save the data. I tried flushing but this causes an infinite loop (which I can understand why, as he tries to flush the same document again).

Any ideas on what way I should get this done? Tried recomputeSingleDocumentChangeSet on the unit of work for the user, but this doesn't seem to save anything.

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-12-05 17:50
    关注

    Tried recomputeSingleDocumentChangeSet on the unit of work for the user, but this doesn't seem to save anything.

    This is exactly what you should do (after modifying the user field)

    $user->setSomething(true);
    $uow->recomputeSingleDocumentChangeSet(/* ... */);
    

    and nothing else, especially don't use flush as you are inside flush already. The recomputeSingleDocumentChangeSet makes Doctrine recount changes which will be later included in the update query of document.

    Creating and persisting a new model in this event works like a charm

    This however sounds strange as new objects persisted at the time of preUpdate event should not reach database (unless you have issued another flush which may have unwanted side effects at this point) as insertions are handled before updates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?