【发布时间】:2016-11-30 14:43:20
【问题描述】:
将实体传递给 flush() 方法允许 Doctrine 仅更新此实体,这对于优化非常有用。但是当我这样做时,似乎关系没有更新。
例子:
$event->getEmails()->first()->setEmail('mynewemail@email.com');
$em->flush($event); // Emails wont be updated
$em->flush(); // Emails will be updated
映射:
class Event
{
/**
* @var ArrayCollection|Email[]
*
* @ORM\OneToMany(targetEntity="Email", mappedBy="event", cascade={"all"}, orphanRemoval=true)
* @ORM\OrderBy({"id"="asc"})
*/
protected $emails;
我检查了 Doctrine 代码,这是我发现的:在内部,当我刷新单个实体时,会调用方法 computeSingleEntityChangeSet。该方法上面的注释如下:
/**
* 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.
* ...
*/
根据第一条规则,集合中的更改也会被处理。那么是我做错了什么,还是 Doctrine 的错误?
【问题讨论】:
-
我只遇到过双向关联。我记得在文档(或其他地方)中读到级联仅适用于单向关联。
-
我通常发现单实体刷新不可靠并且已经远离它。
-
这就是为什么
flush使用单个实体调用应该被弃用,github.com/doctrine/doctrine2/issues/6118它在涉及级联操作时无法管理实体
标签: php symfony doctrine-orm