【问题标题】:Symfony2 get object from inverted entity with ManyToMany relationSymfony2 从具有多对多关系的倒置实体中获取对象
【发布时间】:2014-05-09 01:49:48
【问题描述】:

我的 Doctrine 实体关系遇到问题。 事情是这样的:

我有 2 个实体:文章和类别 文章是主人,品类是奴隶

我想从文章中获取类别,反之,从类别中获取文章。

我做了一个这样的多对多关系:

class Article
{
    /**
     * @ORM\ManyToMany(targetEntity="Alpha\BlogBundle\Entity\Category", cascade={"persist"}, inversedBy="Article")
     * @ORM\JoinTable(name="article_category")
     */
    private $categories;

public function __construct(){
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection();

和类别中的实体:

class Category
{
    /**
     * @ORM\ManyToMany(targetEntity="Alpha\BlogBundle\Entity\Article", cascade={"persist"}, mappedBy="Category")
     */
    private $articles;

public function __construct(){
    $this->articles = new \Doctrine\Common\Collections\ArrayCollection();

在我的文章实体中,我还添加了以下内容:

public function addCategory(\Alpha\BlogBundle\Entity\Category $categories)
{
    $this->categories[] = $categories;
    $categories->addArticle($this);
    return $this;
}

(第四行,$categories->addArticle($this);

在我的控制器中:

public function ajouterAction($data = null, $id = null) {

    // On récupère l'EM pour enregistrer en BDD
    $em = $this->getDoctrine()->getManager();

    // On définit une nouvel objet Article avec de nouveaux attributs
    $article = new Article;
    $article->setTitle('1er article !');
    $article->setContent('Cupcake ipsum dolor sit amet ice cream tiramisu unerdwear.com. Caramels halvah lollipop apple pie soufflé. Tart lollipop soufflé candy tootsie roll sweet donut. Lemon drops danish I love icing I love. Candy canes cheesecake I love. I love tiramisu applicake. I love gingerbread soufflé sweet roll muffin. Cupcake liquorice gummi bears muffin chocolate jelly-o.');
    $article->setAuthor('Toto');

    // On définit une nouvel objet Category avec de nouveaux attributs
    $category = new Category;
    $category->setName('Poney');

    $article->addCategory($category);

    $em->persist($category);
    $em->persist($article);

    $em->flush();

    return $this->render('AlphaBlogBundle:Blog:ajouter.html.twig');
}

最后,从类别中获取我的文章:

public function categoryAction($cat = null) {

    $em = $this->getDoctrine()->getManager();

    // Si cat est vide, on renvoit la liste complète des catégories
    if (!isset($cat) || empty($cat) || $cat == null) {

        $categories = $em->getRepository('AlphaBlogBundle:Category')->findAll();

        return $this->render('AlphaBlogBundle:Blog:categories.html.twig', array(
            'categories' => $categories
        ));
    }
    // Sinon on renvoit la liste des articles de la catégorie
    else {
        $category = $em->getRepository('AlphaBlogBundle:Category')->findOneBy(array('name' => $cat));
        $articles = $category->getArticles();

        return $this->render('AlphaBlogBundle:Blog:category.html.twig', array(
            'articles' => $articles,
            'category' => $category
            //'name' => $name
        ));
    }
}

在我看来,我可以看到我的类别的名称,但文章没有显示,我有这个错误消息:

ContextErrorException: Notice: Undefined index: Category in /home/franck/www/alpha/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1036

如果有人可以帮忙,我有点迷路了。

【问题讨论】:

    标签: php symfony orm doctrine-orm entity-relationship


    【解决方案1】:

    如下来自doctrine documentation

    mappedBy:此选项指定 targetEntity 上的属性名称 那是这种关系的拥有方。它是必需的属性 关系的反面。

    inversedBy:inversedBy 属性指定实体中的字段 这是关系的反面。

    因此,尝试将 Article 类中的 inversedBy="Article" 更改为 inversedBy="articles" 并将 Category 类中的 mappedBy="Category" 更改为 mappedBy="categories"

    另见this多对多双向示例。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      好吧,我听从了您的建议,将文章更改为文章,将类别更改为类别。我还更改了主端的映射,文章,就像这样:

      /**
       * @ORM\ManyToMany(
       *    targetEntity="Alpha\BlogBundle\Entity\Category",
       *    cascade={"persist"},
       *    inversedBy="articles"
       * )
       * @ORM\JoinTable(name="article_category",
       *      joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")},
       *      inverseJoinColumns={
       *          @ORM\JoinColumn(name="category_id", referencedColumnName="id")
       *      }
       * )
       */
      

      它就像一个魅力。我不太明白,但我会研究一下:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2015-12-12
        • 2015-08-25
        相关资源
        最近更新 更多