【问题标题】:How to print comments for every topic?如何打印每个主题的评论?
【发布时间】:2017-10-16 13:35:00
【问题描述】:

我有 2 个表主题和 cmets:在 cmets 表中有一个名为 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 中使用此功能显示当前选定主题的 cmets:

     /**
     * @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 %}

【问题讨论】:

    标签: php symfony doctrine-orm doctrine twig


    【解决方案1】:

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

    一些解决方案是,

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

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

      {% 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 %}
      

    【讨论】:

    • 很好的描述和非常有用的例子。谢谢!
    【解决方案2】:

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

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

    然后在模板中:

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

    【讨论】:

    • 非常简单易行的解决方案。帮了大忙!
    • 感谢 RumenPanchev 照顾@DarkBee 其他答案的宝贵提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2022-08-07
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2017-10-31
    相关资源
    最近更新 更多