2
例如,我有以下兩個類(getter/setter方法不再贅述),其在映射鏈接是雙向的:我該如何讓Doctrine 2忽略通過關係找到的新實體?
class Form
{
private $elements = array();
public function addElement($element)
{
$this->elements[] = $element
$element->setForm($this);
}
}
class Element
{
private $form;
private $name;
}
<one-to-many field="elements" target-entity="Element" mapped-by="form"/>
<many-to-one field="form" target-entity="Form" inversed-by="elements">
<join-column name="formId" referenced-column-name="id" on-delete="CASCADE" on-update="CASCADE"/>
</many-to-one>
如果我執行以下操作;將兩個元素添加到表單中,但只保留一個元素,我想要發生的是未實體元素完全被實體管理器忽略,但要插入數據庫的其他元素和表單:
$form = new Form;
$em->persist($form);
$element = new Element;
$element->setName('firstName');
$form->addElement($element);
$em->persist($element);
$element2 = new Element;
$element2->setName('lastName');
$form->addElement($element2);
$em->flush();
目前得到以下錯誤:
exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'Form#elements' that was not configured to cascade persist operations for entity [email protected] To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'Element#__toString()' to get a clue
據我可以告訴有沒有級聯選項忽略新的實體(http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations),並使用更新前的生命週期回調從$刪除有問題的實體元素數組也不起作用,因爲在回調運行之前拋出異常。
有沒有辦法解決這個問題?
這裏的答案是同樣的問題http://stackoverflow.com/questions/7319370/doctrine-2-saving-entity-in-complex-關係 – maximkou
這不一樣 - 他想自動保存新的實體。我希望它被忽略,而不是保存。 – Gnuffo1
試圖在joinColumn上設置nullable = true? – alex88