Symfony2从具有ManyToMany关系的倒置实体获取对象

Neo*_*vea 4 php orm entity-relationship symfony doctrine-orm

我正面临着我的学说实体关系的问题.这是事情:

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

我想从文章中获取分类,而从类别中获取文章.

我做了一个像这样的ManyToMany关系:

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

public function __construct(){
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
Run Code Online (Sandbox Code Playgroud)

和类别实体:

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

public function __construct(){
    $this->articles = new \Doctrine\Common\Collections\ArrayCollection();
Run Code Online (Sandbox Code Playgroud)

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

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

(第四行$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');
}
Run Code Online (Sandbox Code Playgroud)

并完成,从类别中获取我的文章:

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
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

如果有人可以提供帮助,我在这里有点迷失.

小智 5

如下的学说文档:

mappedBy:此选项指定targetEntity上的属性名称,该属性是此关系的拥有方.它是关系反面的必需属性.

inversedBy:inversedBy属性指定实体中的场,该场是关系的反面.

因此,尝试改变inversedBy="Article",以inversedBy="articles"第一条类,并mappedBy="Category"mappedBy="categories"在Category类.

另请参阅多对多双向示例.

希望这可以帮助.