【问题标题】:Update a ManyToMany Relationship Through the Inverse Side in Doctrine2通过 Doctrine2 中的反面更新多对多关系
【发布时间】:2012-07-08 18:59:43
【问题描述】:

我的食谱实体:

/**
 * @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 的表已经存在。有没有人知道最佳实践如何?

【问题讨论】:

  • “cli 给出错误”是什么意思?当您只是“php app/console”或“php app/console dictionary:schema:update --force”时会发生这种情况吗?
  • 我的意思是 php 应用程序/控制台原则:schema:update --force

标签: symfony doctrine-orm many-to-many


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多