Symfony createQuery winth 多个 table

Symfony createQuery winth multiple table

我想恢复写 post 的用户,只是在尝试执行时出现错误“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 实体,它会工作并且我没有错误...我不明白这是什么原因,为什么它告诉我用户中没有方法 "getIdPost"?这是正常的,因为它是 "Post" :x

的方法

您希望编写 post 的用户从查询中获取它有点奇怪,您没有 $post->getUser() 吗?我认为你应该加入 table 以实现

       $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);