2017-03-31 125 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