view raw
Raphaël Malié Raphaël Malié - 5 months ago 27
PHP Question

Flush a single entity doesn't update relation

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 :

$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
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?


With $event->getEmails()->first()->setEmail(''); 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.