在树枝中显示嵌套数组

Geo*_*iuc 6 php arrays symfony twig

这是我的Message实体.它是一个在我的应用程序中定义用户之间的消息的类.

class Message
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @Assert\NotBlank(message="private_message.title.blank")
     * @ORM\Column(name="title", type="string", length=50)
     */
    protected $title;

    /**
     * @Assert\NotBlank(message="private_message.receiver.blank")
     * @ORM\ManyToOne(targetEntity="MedAppBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $receiver;
    /**
     * @ORM\ManyToOne(targetEntity="MedAppBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $sender;

    /**
     * @var string
     * @Assert\NotBlank(message="private_message.content.blank")
     * @ORM\Column(name="content", type="string")
     */
    protected $content;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="sentAt", type="datetime")
     */
    protected $sentAt;


    /**
     * @var boolean
     *
     * @ORM\Column(name="isSpam", type="boolean")
     */
    protected $isSpam = false;


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="seenAt", type="datetime",nullable=true)
     */
    protected $seenAt = null;

    /**
     * @ORM\ManyToOne(targetEntity="PrivateMessageBundle\Entity\Message")
     * @ORM\JoinColumn(referencedColumnName="id",nullable=true)
     */
    protected $replyof;

    /**
     * @ORM\OneToMany(targetEntity="PrivateMessageBundle\Entity\Message", mappedBy="replyof")
     **/
    private $replies;

    public function __construct() {
        $this->replies = new ArrayCollection();
    }
Run Code Online (Sandbox Code Playgroud)

需要注意的是replyof变量,它告诉消息是消息的父节点.如果它为NULL,则消息不是回复,它是父消息(根).

messages变量,它是一个回复消息的消息数组.这些回复本身可以有回复.对于叶节点,此数组也可以为NULL,因为它们没有任何回复.

所有其他变量只包含一些字段,用于定义两个用户之间的实际消息.

我想要做的是在Twig中以树状格式显示我的所有消息,如下所示:

message1 - root message, reply of none, but has replies
   reply1 - first reply of message 1
      reply1 first reply of reply 1 of message 1, leaf with no further replies
   reply2 - second reply of message 1, leaf with no further replies

message2 - root message, no replies and a reply of none
Run Code Online (Sandbox Code Playgroud)

问题是Twig只支持foreach循环,我不知道如果它具有更大的深度,大于2,我将不知道如何显示这种格式.

{% for reply in message.replies %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    <hr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这将显示消息的每个回复,但如何全面显示嵌套消息?

A.L*_*A.L 3

我没有测试过你应该能够循环回复:

{% for reply in message.replies %}
    {% if loop.first %}<ul>{% endif %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    {% for reply in reply.replies %}
        {% if loop.first %}<li><ul>{% endif %}
        <li> sent by: {{ reply.sender }} </li>
        <li> title: {{ reply.title }} </li>
        <li> content: {{ reply.content }} </li>
        <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
        {% if loop.last %}</ul></li>{% endif %}
    {% endfor %}
    {% if loop.last %}</ul>{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

它只会显示 2 级回复。您可以使用 Twig来定义应递归显示回复的可重用函数:

{# define the macro #}
{% macro displayReply(reply) %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    {% for reply in reply.replies %}
        {% if loop.first %}<li><ul>{% endif %}
        {{ displayReply(reply) }}
        {% if loop.last %}</ul></li>{% endif %}
    {% endfor %}
{% endmacro %}

{# use the macro #}
{% for reply in message.replies %}
    {% if loop.first %}<ul>{% endif %}
    {{ displayReply(reply) }}
    {% if loop.last %}</ul>{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

根据您的查询,它可能会以错误的顺序显示回复,您可能需要在查询中按降序对回复进行排序。