doujiang1913 2016-11-30 14:43
浏览 16
已采纳

刷新单个实体不会更新关系

Passing an entity to the flush() method allow Doctrine to only update this entity, which is great for optimization. But it seems that the relations are not updated when I'm doing this.

Example :

    $event->getEmails()->first()->setEmail('mynewemail@email.com');
    $em->flush($event); // Emails wont be updated
    $em->flush(); // Emails will be updated

The mapping:

class Event
{
    /**
     * @var ArrayCollection|Email[]
     *
     * @ORM\OneToMany(targetEntity="Email", mappedBy="event", cascade={"all"}, orphanRemoval=true)
     * @ORM\OrderBy({"id"="asc"})
     */
    protected $emails;

I checked inside Doctrine code, and here is what I found : internally, when I flush a single entity, the method computeSingleEntityChangeSet is called. The comment above this method is the following:

/**
 * Only flushes the given entity according to a ruleset that keeps the UoW consistent.
 *
 * 1. All entities scheduled for insertion, (orphan) removals and changes in collections are processed as well!
 * 2. Read Only entities are skipped.
 * 3. Proxies are skipped.
 * 4. Only if entity is properly managed.
 * ...
 */

According to first rule, changes in collections are processed as well. So am I doing something wrong, or is this a bug of Doctrine?

  • 写回答

1条回答 默认 最新

  • dougan4884 2016-11-30 14:51
    关注

    With $event->getEmails()->first()->setEmail('mynewemail@email.com'); you're not updating the collection, but one Entity in the collection. It's normal that the single entity flush does not update the Email entity.

    If you do write $event->addEmail($aNewEmailEntity); (same with remove), then you'll see that the collection is indeed updated when calling the single entity flush.

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题