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

时间:2017-05-17 08:01:51

标签: php symfony doctrine-orm doctrine twig

我有2个表主题和注释:在注释表中有一个名为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
        ));
    }

现在我试图在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
        ));

    }

毕竟,当我尝试打印树枝中特定主题的所有数据时,我得到以下异常:

  

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

{% 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 个答案:

答案 0 :(得分:3)

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

有些解决方案,

  1. 在控制器中提供空数组:

    return $this->render('topics/topic.details.html.twig', array(
        'topic' => $topic
        'topicComments' => [],
    ));
    
  2. 测试var是否已定义

    {% if topicComments is defined and not topicComments  is empty %}
    <div class="container">
        {% for comment in topicComments %}
            {{ comment.description }}
        {% endfor %}
    </div>
    {% endif %}
    
  3. 使用过滤器default

    {% for comment in topicComments|default([]) %}
        {{ comment.description }}
    {% endfor %}
    

答案 1 :(得分:2)

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

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

然后在模板中:

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