0

我想恢复写帖子的用户,只是我尝试这样做时出错“ 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
                }

如果我删除查询的用户实体,它可以工作并且我没有错误......我不明白它是什么原因,为什么它告诉我用户中没有方法“getIdPost”?这是正常的,因为它是“发布”的一种方法:x

4

1 回答 1

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);
于 2019-02-13T08:17:06.053 回答