2015-09-08 149 views
5

這是我的消息實體。這是一個定義我的應用程序中的用戶之間的消息的類。在樹枝中顯示嵌套數組

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(); 
    } 

有什麼需要注意的是replyof變量,它會告訴什麼樣的信息是消息的父母。如果它是NULL,那麼消息不是回覆,而是父消息(根)。

messages變量,它是對消息進行回覆的消息數組。這些答覆本身可以有答覆。對於葉節點,此數組也可以爲NULL,因爲它們沒有任何回覆。

所有其他變量只包含一些字段,用於定義兩個用戶之間的實際消息。

我所試圖做的是在嫩枝我所有的消息顯示在樹狀的格式,比如:

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 

的問題是,嫩枝只支持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 %} 

這將顯示消息的每個回覆,但是如何以全深度顯示嵌套消息?

+3

你見過[樹枝 - 如何渲染樹](http://stackoverflow.com/questions/8326482/twig-how-to-render-a-樹)? – HPierce

回答

2

我沒有測試過,你應該可以循環播放re層數:

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

它將只顯示2個級別的答覆。你可以用一根樹枝macro定義一個可重複使用的功能應該遞歸顯示回覆:

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

根據您的查詢時,它可能會顯示錯誤的順序回答,你可能需要在查詢的降序排列回覆排序。

1

你可以做一個遞歸方法如下:

主樹枝

,您打印的主要信息,並在部分遞歸迭代如下:

## main twig 
Root message: 
    <ul> 
    <li> sent by: {{ message.sender }} </li> 
    <li> title: {{ message.title }} </li> 
    <li> content: {{ message.content }} </li> 
    <li> date: {{ message.sentAt|date('d-m-Y H:i:s') }} </li> 
    {{ include('AcmeDemoBundle:Message:_elem.html.twig', {'replies': message.replies) }} 
    </ul> 

## AcmeDemoBundle:Message:_elem.html.twig 
<ul> 
{% for reply in 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> 
    {{ include('AcmeDemoBundle:Message:_elem.html.twig', {'replies': reply.replies) }} 
{% endfor %} 
</ul> 

希望這可以幫到