如何设置正确的 Doctrine 级联关系?
How to set correct Doctrine cascade relationship?
我有一个旧的 Page 实体和一个新的 Equivalent 实体。我的 Page.orm.xml 现已编辑为包含以下内容:
<one-to-many target-entity="AppBundle\Entity\Equivalent" field="equivalents">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
...我的 PageAdmin class 已编辑为包括以下内容:
->add('equivalents', 'sonata_type_collection', array(
'label' => "Equivalents",
'cascade_validation' => true,
'required' => false,
), array(
'edit' => 'inline',
'inline' => 'table',
'targetEntity' => 'AppBundle\Entity\Equivalent',
))
...我的等效实体定义包括以下内容:
/**
* @var \Application\Sonata\PageBundle\Entity\Page
* @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", cascade={"persist"})
*/
private $page;
到目前为止一切顺利。当我编辑页面时,我会得到一个灵活的界面,允许我以内联形式添加和编辑新的等效记录。非常好。
当我尝试保存页面实体时出现问题。我收到以下错误:
A new entity was found through the relationship
'Application\Sonata\PageBundle\Entity\Page#equivalents' that was not
configured to cascade persist operations for entity: some equivalent.
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"}).
我做错了什么?我现有的级联定义不应该涵盖这种行为吗?我该怎么做才能解决这个问题?
定义缺少 关联字段。将 mapped-by 添加到:
<one-to-many target-entity="AppBundle\Entity\Equivalent" field="equivalents" mapped-by="page">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
和inversedBy到:
/**
* @var \Application\Sonata\PageBundle\Entity\Page
* @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", inversedBy="equivalents", cascade={"persist"})
*/
private $page;
参考资料
我有一个旧的 Page 实体和一个新的 Equivalent 实体。我的 Page.orm.xml 现已编辑为包含以下内容:
<one-to-many target-entity="AppBundle\Entity\Equivalent" field="equivalents">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
...我的 PageAdmin class 已编辑为包括以下内容:
->add('equivalents', 'sonata_type_collection', array(
'label' => "Equivalents",
'cascade_validation' => true,
'required' => false,
), array(
'edit' => 'inline',
'inline' => 'table',
'targetEntity' => 'AppBundle\Entity\Equivalent',
))
...我的等效实体定义包括以下内容:
/**
* @var \Application\Sonata\PageBundle\Entity\Page
* @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", cascade={"persist"})
*/
private $page;
到目前为止一切顺利。当我编辑页面时,我会得到一个灵活的界面,允许我以内联形式添加和编辑新的等效记录。非常好。
当我尝试保存页面实体时出现问题。我收到以下错误:
A new entity was found through the relationship 'Application\Sonata\PageBundle\Entity\Page#equivalents' that was not configured to cascade persist operations for entity: some equivalent. 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"}).
我做错了什么?我现有的级联定义不应该涵盖这种行为吗?我该怎么做才能解决这个问题?
定义缺少 关联字段。将 mapped-by 添加到:
<one-to-many target-entity="AppBundle\Entity\Equivalent" field="equivalents" mapped-by="page">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
和inversedBy到:
/**
* @var \Application\Sonata\PageBundle\Entity\Page
* @ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", inversedBy="equivalents", cascade={"persist"})
*/
private $page;