2017-03-31 92 views
1

我有问题要根据父子评论显示子评论。我不知道这是一个枝条还是查询问题或其他问题。根据子分评论根据子分评论显示

我有一个包含链接到的意见,我的意见表的字段(com_id)子评论回复表。

只有我的答复表的com_id领域也指同桌的reply_id场如果reply_level等于1(见图片,红线对角线)。

table comment

table reply

如何使(1),reply_id(6)的基础上reply_id父查询选择reply_level(reply_id(4)?

我的树枝代码:

<h3 class="titleComments">Comments</h3> 
 
    {% for comment in comments %} 
 
     <p> <strong>{{ comment.author }}</strong> {{ comment.content }}</p> 
 
     <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel0" value="{{ comment.id }}" > answer </button> 
 
    <div id="reply"> 
 
     {% if replys is defined %} 
 
      {% for reply in replys %} 
 
       {% if reply.getComParent() == comment.id and reply.level == 0%} 
 
        <p> <strong>{{ reply.author }}</strong> {{ reply.content }}</p> 
 
        <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel1" value="{{ reply.id }}" data-level="{{ reply.level1 }}" > answer </button> 
 
       {% endif %} 
 
       <div id="underReply1"> 
 
        {% if reply.getComParent() == reply.id and reply.level ==1 %} 
 
         <p> <strong>{{ reply.author }}</strong> {{ reply.content }}</p> 
 
         <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel2" value="{{ reply.id }}" data-level="{{ reply.level2 }}" > answer </button> 
 
        {% endif %} 
 
       </div> 
 
      {% endfor %} 
 
     {% endif %} 
 
     </div> 
 
    {% else %} 
 
     no comment(s). 
 
    {% endfor %}

结果:

result

子子如何不出现。看来,如果我添加第四个主要评论。它从分注意到评论,但我不能根据父子评论调用子注释。

谢谢

+0

你确定'reply_level'是一个整数? –

+0

是的,这是一个整数 – chk35

回答

1

递归性是一个神奇的词根据东西的深度做循环任意数量。

在你的情况,你可以尝试这样的事:

<h3 class="titleComments">Comments</h3> 

{# imports macros contained in this file inside "me" variable #} 
{% import _self as me %} 

{# makes the variable exists and loopable whatever the case #} 
{% set replies = replies|default([]) %} 

{% for comment in comments %} 
    <p><strong>{{ comment.author }}</strong> {{ comment.content }}</p> 
    <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel0" value="{{ comment.id }}"> answer</button> 

    {# fixes the non-unique id issue in your current code #} 
    <div id="reply-{{ comment.id }}"> 
     {% if replies|length %} 
      {% for reply in replies %} 
       {% if reply.getComParent() == comment.id and reply.level == 0 %} 
        <p> <strong>{{ reply.author }}</strong> {{ reply.content }}</p> 
        <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel{{ level }}" value="{{ reply.id }}" data-level="{{ reply.level1 }}" > answer </button> 

        {# here begins the magiC#} 
        {{ me.displayReplies(replies, reply, 1) }} 

       {% endif %} 
      {% endfor %} 
     {% else %} 
      no comment(s). 
     {% endif %} 
    </div> 
{% endfor %} 

{# macros are similar to functions in php and can call themselves recursivly #} 
{% macro displayReplies(replyParent, replies, level) %} 

    {# reimports macros to make this macro able to call himself #} 
    {% import _self as me %} 

    {% for replyChild in replies %} 

    <div id="underReply{{ replyParent.id }}"> 
     {% if replyChild.getComParent() == replyParent.id and replyChild.level == level %} 
      <p> <strong>{{ replyChild.author }}</strong> {{ replyChild.content }}</p> 
      <button type="button" class="btn-primary btn-xs buttonAnswer btnLevel2" value="{{ replyChild.id }}" data-level="{{ replyChild.level2 }}" > answer </button> 

      {# and we get deeper if needed #} 
      {{ me.displayReplies(replyChild, replies, level + 1) }} 

     {% endif %} 
    </div> 
    {% endfor %} 

{% endmacro %} 

顺便说一句,一个简单的方法应该是使用相同的表有关评论和回复:

考虑以下内容:

comments: 
    - 
     id: 1 
     parent_id: null 
     content: comment 1 
    - 
     id: 2 
     parent_id: null 
     content: comment 2 
    - 
     id: 3 
     parent_id: null 
     content: comment 3 
    - 
     id: 4 
     parent_id: 1 
     content: reply comment 1 
    - 
     id: 5 
     parent_id: 2 
     content: reply comment 2 
    - 
     id: 6 
     parent_id: 3 
     content: reply comment 3 
    - 
     id: 7 
     parent_id: 3 
     content: reply comment 3 
    - 
     id: 8 
     parent_id: 4 
     content: reply of reply comment 3 
    - 
     id: 9 
     parent_id: 8 
     content: reply of reply of reply comment 3 

而下面的小枝代码:

{% import _self as me %} 

{% macro displayComment(comments, parentComment, deepness = 0) %} 

    {% if parentComment.parent_id is null %} 
    Comment #{{ parentComment.id }} (deepness = {{ deepness }}): {{ parentComment.content }} 
    {% else %} 
    {% for i in 0..deepness %} {% endfor %}Reply #{{ parentComment.id }} of #{{ parentComment.parent_id }} (deepness = {{ deepness }}): {{ parentComment.content }} 
    {% endif %} 

    {% import _self as me %} 
    {% for childComment in comments if childComment.parent_id == parentComment.id %} 
    {{ me.displayComment(comments, childComment, deepness + 1) }} 
    {% endfor %} 

{% endmacro %} 

{% for comment in comments if comment.parent_id is null %} 
{{ me.displayComment(comments, comment) }} 
{% endfor %} 

您将结束:

Comment #1 (deepness = 0): comment 1 

    Reply #4 of #1 (deepness = 1): reply comment 1 

     Reply #8 of #4 (deepness = 2): reply of reply comment 3 

      Reply #9 of #8 (deepness = 3): reply of reply of reply comment 3 

Comment #2 (deepness = 0): comment 2 

    Reply #5 of #2 (deepness = 1): reply comment 2 

Comment #3 (deepness = 0): comment 3 

    Reply #6 of #3 (deepness = 1): reply comment 3 

    Reply #7 of #3 (deepness = 1): reply comment 3 

See fiddle

+0

感谢代码,它很复杂,我是否必须完全按照原样复制代码?因为它不起作用,所以出现此错误:article.html.twig第28行中的Twig_Error_Runtime: 由于数组为空(({%if answers.length%})),所以键“length”不存在。 我会继续寻求这个解决方案 – chk35

+0

对不起,这是'reply | length'。 –

+0

我没有更多的错误,但现在我只有主要评论出现:[链接](http://www.hostingpics.net/viewer.php?id=348279error.png) – chk35

0

你可以试试这个逻辑:

<div id="underReply1"> 
    {% if reply.getComParent() == comment.id and reply.level == 1 %} 
    ... 

我认为这就是你所需要的。由于回复ID可能与公司父母不匹配。我不确定它会解决问题,但请尝试一下,看看这些是否是您期望的结果。

+0

不,我已经尝试,这就是问题所在。如果我这样做,这个子注释将链接到父注释(主注释)而不是父子注释。 – chk35