Symfony createQuery Winth多表

时间:2019-02-12 17:39:54

标签: sql symfony createquery

我想找回写这篇文章的用户,只有当我尝试写“ Attempted to call an undefined method named "getIdPost" of class "App\Entity\User".”时出现错误

$em = $this->getDoctrine()->getManager();
                $query = $em->createQuery( //creation de la requête
                    'SELECT p , u
                    FROM App\Entity\User u, App\Entity\Post p
                    WHERE p.Id_Post_Parent IS NULL
                    AND p.Id_User = u.idUser
                    ORDER BY p.Post_Date_Time DESC'
                )->setMaxResults(10);
                $posts = $query->getResult();

                $publicPosts = array();
                $comments = array();
                for($i = 0; $i<sizeof($posts) ; $i++){
                    $publicPosts[$i] = $posts[$i]->getArray();
                    //récupération des commentaires
                    $em = $this->getDoctrine()->getManager(); //on appelle Doctrine
                    $query = $em->createQuery( //creation de la requête
                        'SELECT p , u
                        FROM App\Entity\User u, App\Entity\Post p
                        WHERE p.Id_Post_Parent = :idParent
                        AND p.Id_User = u.idUser'
                    )->setParameter('idParent', $posts[$i]->getIdPost())
                    ->setMaxResults(10); //On limite à 10 commentaires par posts
                    $comments[$i] = $query->getResult(); //variable qui récupère la requête
                }

如果我删除了查询的User实体,并且没有错误……我不明白这是什么原因,为什么它告诉我user中没有方法“ getIdPost”?这是正常现象,因为它是“ Post”方法:x

1 个答案:

答案 0 :(得分:0)

您希望编写帖子的用户从查询中获取帖子有点奇怪,您是否没有$ post-> getUser()吗?我认为您应该参加会议以实现这一目标

       $query = $em->createQuery( //creation de la requête
           'SELECT u
            FROM App\Entity\User u, 
            LEFT JOIN App\Entity\Post p on p.Id_User = u.idUser
            WHERE p.Id_Post_Parent IS NULL
            ORDER BY p.Post_Date_Time DESC'
        )->setMaxResults(10);