2012-07-08 91 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; 
    } 
}