加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:46:53 所属栏目:PHP教程 来源:网络整理
导读:我正面临着我的学说实体关系的问题. 这是事情: 我有2个实体:文章和类别 文章是主人,类别是奴隶 我想从文章中获取分类,而从类别中获取文章. 我做了一个像这样的ManyToMany关系: class Article{ /** * @ORMManyToMany(targetEntity="AlphaBlogBundleEnti
我正面临着我的学说实体关系的问题.
这是事情:

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

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

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

class Article
{
    /**
     * @ORMManyToMany(targetEntity="AlphaBlogBundleEntityCategory",cascade={"persist"},inversedBy="Article")
     * @ORMJoinTable(name="article_category")
     */
    private $categories;

public function __construct(){
    $this->categories = new DoctrineCommonCollectionsArrayCollection();

和类别实体:

class Category
{
    /**
     * @ORMManyToMany(targetEntity="AlphaBlogBundleEntityArticle",mappedBy="Category")
     */
    private $articles;

public function __construct(){
    $this->articles = new DoctrineCommonCollectionsArrayCollection();

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

public function addCategory(AlphaBlogBundleEntityCategory $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:注意:未定义的索引:/home/franck/www/alpha/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php第1036行中的类别

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

解决方法

如下: doctrine documentation:

mappedBy: This option specifies the property name on the targetEntity
that is the owning side of this relation. Its a required attribute for
the inverse side of a relationship.

inversedBy: The inversedBy attribute designates the ?eld in the entity
that is the inverse side of the relationship.

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

另请参见this多对多双向示例.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读