dongwa3808 2017-05-17 08:01
浏览 37
已采纳

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

I have 2 tables topics and comments: In the comments table a have column named topic_id where the id is the number, which corresponds to the commented topic from the user. I list the topic details with the following function in the 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
        ));
    }

Now im trying to display the comments for the current selected topic with this function in the CommentController:

     /**
     * @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
        ));

    }

After all, when i try to print all the data from specific topic in the twig, i got the following exception:

Variable "topicComments" does not exist. Im sure the problem isn't big and can be solved, but not sure what i'm missing. Here's my twig template:

{% extends 'base.html.twig' %}

{% block body %}
    <div class="container">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title"><a href="/topic/{{ topic.id }}">{{ topic.title }}</a></h3>
            </div>
            <br>
            <div class="container">
                <h3>Description:</h3>
           <div class="well">
               <div class="panel-body">
                        {{ topic.description }}
                   </div>
               </div>
           </div>
            <hr>
            <div class="panel-body">
                <div class="well">
                    <b>
                        Category: {{ topic.category }} <br>
                        Created: {{ topic.dateCreated|date("m/d/Y  H:i:s") }}
                    </b>
                </div>
            </div>
        </div>
    <div class="container">
        <div class="panel panel-primary">
            <div class="panel-body">

            </div>
        </div>
    </div>
        <a href="/{{ topic.id }}/comment/add" class="btn btn-lg btn-default">Leave a Comment</a>
    </div>
    <div class="container">
        {% for comment in topicComments %}
            {{ comment.description }}
        {% endfor %}
    </div>
{% endblock %}
  • 写回答

2条回答 默认 最新

  • dongyimo1293 2017-05-17 08:08
    关注

    You could continue passing the topic variable instance and navigate the relation in the twig, so render the controller:

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

    Then in the template:

       {% for comment in topic.comments %}
            {{ comment.description }}
        {% endfor %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值