Php 如何打印每个主题的评论?

Php 如何打印每个主题的评论?,php,symfony,doctrine-orm,doctrine,twig,Php,Symfony,Doctrine Orm,Doctrine,Twig,我有两个表topics和comments:在comments表中有一个名为topic_id的列,其中id是数字,它对应于用户评论的主题。我在TopicController中使用以下功能列出主题详细信息: /** * @Route("/topic/{id}", name="topic_details") * @param $id * @return \Symfony\Component\HttpFoundation\Response */ publ

我有两个表topics和comments:在comments表中有一个名为topic_id的列,其中id是数字,它对应于用户评论的主题。我在TopicController中使用以下功能列出主题详细信息:

 /**
     * @Route("/topic/{id}", name="topic_details")
     * @param $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function topicDetailsAction($id)
    {
        $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);

        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    }
     /**
     * @Route("/topic/{id}", name="topic_details")
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function listCommentsAction($id)
    {
        $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
        $topicComments = $topic->getComments();

        return $this->render('topics/topic.details.html.twig', array(
            'topicComments' => $topicComments
        ));

    }
return $this->render('topics/topic.details.html.twig', array(
    'topic' => $topic
    'topicComments' => [],
));
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
    ));
现在,我尝试在CommentController中使用此功能显示当前选定主题的注释:

 /**
     * @Route("/topic/{id}", name="topic_details")
     * @param $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function topicDetailsAction($id)
    {
        $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);

        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    }
     /**
     * @Route("/topic/{id}", name="topic_details")
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function listCommentsAction($id)
    {
        $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
        $topicComments = $topic->getComments();

        return $this->render('topics/topic.details.html.twig', array(
            'topicComments' => $topicComments
        ));

    }
return $this->render('topics/topic.details.html.twig', array(
    'topic' => $topic
    'topicComments' => [],
));
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
    ));
毕竟,当我尝试打印细枝中特定主题的所有数据时,我得到了以下异常:

变量“topicComments”不存在。 我肯定这个问题不大,可以解决,但不确定我错过了什么。 这是我的小树枝模板:

   {% for comment in topic.comments %}
        {{ comment.description }}
    {% endfor %}
{%extends'base.html.twig%}
{%block body%}

说明: {{topic.description}}
类别:{{topic.Category}}
创建:{{topic.dateCreated}date(“m/d/Y H:i:s”)} {topicComments%中的注释为%s} {{comment.description}} {%endfor%} {%endblock%}
您在两个操作中使用相同的模板,但仅在其中一个操作中提供变量
topicComments

一些解决办法是

  • 在控制器中提供空阵列:

     /**
         * @Route("/topic/{id}", name="topic_details")
         * @param $id
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function topicDetailsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
    
            return $this->render('topics/topic.details.html.twig', array(
                'topic' => $topic
            ));
        }
    
         /**
         * @Route("/topic/{id}", name="topic_details")
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function listCommentsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
            $topicComments = $topic->getComments();
    
            return $this->render('topics/topic.details.html.twig', array(
                'topicComments' => $topicComments
            ));
    
        }
    
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
        'topicComments' => [],
    ));
    
        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    
  • 测试是否定义了var

    {% if topicComments is defined and not topicComments  is empty %}
    <div class="container">
        {% for comment in topicComments %}
            {{ comment.description }}
        {% endfor %}
    </div>
    {% endif %}
    

  • 您在两个操作中使用相同的模板,但仅在其中一个操作中提供变量
    topicComments

    一些解决办法是

  • 在控制器中提供空阵列:

     /**
         * @Route("/topic/{id}", name="topic_details")
         * @param $id
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function topicDetailsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
    
            return $this->render('topics/topic.details.html.twig', array(
                'topic' => $topic
            ));
        }
    
         /**
         * @Route("/topic/{id}", name="topic_details")
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function listCommentsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
            $topicComments = $topic->getComments();
    
            return $this->render('topics/topic.details.html.twig', array(
                'topicComments' => $topicComments
            ));
    
        }
    
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
        'topicComments' => [],
    ));
    
        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    
  • 测试是否定义了var

    {% if topicComments is defined and not topicComments  is empty %}
    <div class="container">
        {% for comment in topicComments %}
            {{ comment.description }}
        {% endfor %}
    </div>
    {% endif %}
    

  • 您可以继续传递主题变量实例并在细枝中导航关系,因此呈现控制器:

     /**
         * @Route("/topic/{id}", name="topic_details")
         * @param $id
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function topicDetailsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
    
            return $this->render('topics/topic.details.html.twig', array(
                'topic' => $topic
            ));
        }
    
         /**
         * @Route("/topic/{id}", name="topic_details")
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function listCommentsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
            $topicComments = $topic->getComments();
    
            return $this->render('topics/topic.details.html.twig', array(
                'topicComments' => $topicComments
            ));
    
        }
    
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
        'topicComments' => [],
    ));
    
        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    
    然后在模板中:

       {% for comment in topic.comments %}
            {{ comment.description }}
        {% endfor %}
    

    您可以继续传递主题变量实例并在细枝中导航关系,因此呈现控制器:

     /**
         * @Route("/topic/{id}", name="topic_details")
         * @param $id
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function topicDetailsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
    
            return $this->render('topics/topic.details.html.twig', array(
                'topic' => $topic
            ));
        }
    
         /**
         * @Route("/topic/{id}", name="topic_details")
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function listCommentsAction($id)
        {
            $topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
            $topicComments = $topic->getComments();
    
            return $this->render('topics/topic.details.html.twig', array(
                'topicComments' => $topicComments
            ));
    
        }
    
    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
        'topicComments' => [],
    ));
    
        return $this->render('topics/topic.details.html.twig', array(
            'topic' => $topic
        ));
    
    然后在模板中:

       {% for comment in topic.comments %}
            {{ comment.description }}
        {% endfor %}
    

    很好的描述和非常有用的例子。谢谢很好的描述和非常有用的例子。谢谢非常简单的解决方案。帮了很多忙!感谢鲁门潘切夫,请注意@darkbeee另一个答案的宝贵提示非常简单和容易的解决方案。帮了很多忙!谢谢鲁门潘切夫,注意@DarkBee的另一个答案的宝贵提示