2014-02-11 15 views
0

我在使用$em->flush()時遇到問題; 對象$的教育持續拋出$em->persist ($education)。 該位置是我項目中的一個實體,與教育實體有關,可以拋出多對一的關係。

錯誤盒子裏裝的:

A new entity was found through the relationship 'XBundle\Entity\Education#location' that was not configured to cascade persist operations for entity: NewYork. 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"}). 

我怎樣才能解決這個問題呢?

回答

0

關係上使用cascade={"persist"}。例如:

/** 
* @ORM\OneToOne(targetEntity="Foo\Bundle\Entity\User", mappedBy="bar", cascade={"persist"}) 
*/ 
protected $foo; 
+0

我很抱歉,但即使我添加級聯= {問題仍然存在:如果您使用的docblock批註指定您的數據庫架構,這是通過將級聯屬性您@ManyToOne協會實現「堅持」}在我的實體文件中。 – user3110618

0

在Doctrine 2中,級聯持久性不會自動發生。相反,您需要明確指出您需要它。

<?php 
namespace XBundle\Entity; 
/** 
* @Entity 
*/ 
class Education 
{ 
    //... 

    /**   
    * @ManyToOne(targetEntity="Location", cascade={"persist"}) 
    */ 
    protected $location; 

    //... 
} 
+0

感謝您的回答,但我在項目中添加了cascade = {「persist」},問題依然存在。 – user3110618

+0

/** * @var \地點 * * @ORM \多對一(targetEntity = 「位置」,級聯= { 「持續」}) * @ORM \ JoinColumns({ * @ORM \ JoinColumn(名稱= 「location」,referencedColumnName =「id」,cascade = {「persist」}) *}) */ private $ location; – user3110618

+0

除**教育**之外,您的**位置**實體是否還有另一個關聯?如果是這樣,你可以粘貼**位置**的代碼嗎? – slashCoder