“usort” a Doctrine\Common\Collections\ArrayCollection?

前端 未结 4 1521
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 16:51

In various cases I need to sort a Doctrine\\Common\\Collections\\ArrayCollection according to a property in the object. Without finding a method doing that righ

4条回答
  •  悲哀的现实
    2020-12-02 17:29

    If you have an ArrayCollection field you could order with annotations. eg:

    Say an Entity named Society has many Licenses. You could use

    /**
    * @ORM\OneToMany(targetEntity="License", mappedBy="society")
    * @ORM\OrderBy({"endDate" = "DESC"})
    **/
    private $licenses;
    

    That will order the ArrayCollection by endDate (datetime field) in desc order.

    See Doctrine documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#orderby

提交回复
热议问题