2012-07-08 90 views
0

我的食譜實體:更新一個多對多的關係通過反側Doctrine2

/** 
* @ORM\ManyToMany(targetEntity="Category", inversedBy="recipes") 
*/ 
private $categories; 

我的類別實體:

/** 
* @ORM\ManyToMany(targetEntity="Recipe", inversedBy="categories") 
* @ORM\JoinTable(name="recipe_category") 
*/ 
private $recipes; 

確定這是從http://www.youtube.com/watch?v=kPrgoe3Jrjw&feature=related

有了這兩個擁有方都行得通。但是cli給出了錯誤:'名稱爲recipe_category的表已經存在。有沒有人有任何想法的最佳做法?

+0

你是什麼意思「CLI給出了錯誤」?當你只是「php app/console」,或者「php app/console doctrine:schema:update --force」...時,會發生這種情況嗎? – AdrienBrault 2012-07-08 19:38:02

+0

我的意思是PHP應用程序/控制檯原則:架構:更新 - 力量 – stwe 2012-07-09 15:36:09

回答

1

必須更新去像其他實體:

class Recipe 
{ 
    public function addCategory($cat) 
    { 
     $car->addRecipe($this); 
     $this->categories->add($cat); 

     return $this; 
    } 

    public function removeCategory($cat) 
    { 
     $cat->removeRecipe($this); 
     $this->categories->removeElement($cat); 

     return $this; 
    } 
}