將實體傳遞給flush()方法允許Doctrine只更新此實體,這對優化很有用。但是當我這樣做的時候,似乎沒有更新關係。刷新單個實體不更新關係
實施例:
$event->getEmails()->first()->setEmail('[email protected]');
$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;
我學說代碼內部檢查,並且在這裏是我發現:在內部,當我刷新一個單一的實體,該方法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.
* ...
*/
根據第一條規則,還會處理集合中的更改。所以我做錯了什麼,或者這是一個學說的錯誤?
我遇到過這種情況只能使用雙向關聯。我記得在文檔(或某處)中閱讀,如果它們是單向的,級聯將僅適用於關聯。 –
我通常發現單個實體刷新不可靠,並已經離開它。 – Tom
,這就是爲什麼應該拋棄使用單個實體的'flush'調用,https://github.com/doctrine/doctrine2/issues/6118當涉及級聯操作時它不能管理實體 – Federkun