根据这个应该使用 OrderBy 对 ArrayCollection 进行排序。在列出家庭的联系日期的集合时,以下内容以某种方式无法实现此结果:
家庭实体(片段)
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Mana\ClientBundle\Entity\Contact", mappedBy="household")
* @ORM\OrderBy({"contact_date" = "DESC"})
*/
private $contacts;
联系实体(片段)
/**
* Contact
*
* @ORM\Table(name="contact", indexes={@ORM\Index(name="idx_contact_household_idx", columns={"household_id"}), @ORM\Index(name="idx_contact_type_idx", columns={"contact_type_id"}), @ORM\Index(name="idx_contact_center_idx", columns={"center_id"})})
* @ORM\Entity
*/
class Contact
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="contact_date", type="date", nullable=true)
*/
private $contactDate;
...
}
模板(5 个最近的?)(片段)
{% for i in 0..4 %}
{% if household.contacts[i] is defined %}
<tr><td>{{ household.contacts[i].contactDate|date('m/d/Y') }}</td>
...{% endif %}
{% endfor %}
样本结果
Date
05/02/2012
05/23/2012
05/30/2012
06/26/2012
06/06/2012