我有一个问题是根据父子评论显示子评论。我不知道它是否是一个树枝或查询问题或其他。
我有一个回复表,其中包含使用字段(com_id)链接到我的评论表中的评论的子评论。
如果reply_level等于1,则只有我的回复表的com_id字段也引用同一个表的reply_id字段(参见图像,红线对角线)。
如何根据reply_id父级(reply_id(4)?
进行查询以选择reply_level(1),reply_id(6)我的枝条代码:
<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 %}
&#13;
结果:
子子如何不出现。看来,如果我添加第四个主要评论。它从子评论到评论工作,但我不能根据父子评论调用子评论。
谢谢
答案 0 :(得分:2)
递归是根据某事物的深度做任意数量的循环的神奇词。
在你的情况下,你可以尝试这样的事情:
<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
答案 1 :(得分:0)
你能试试这个逻辑吗?
<div id="underReply1">
{% if reply.getComParent() == comment.id and reply.level == 1 %}
...
我认为这就是你所需要的。由于回复ID可能与com父匹配。我不确定它会解决问题,但是请尝试一下,看看这些是你期望的结果。