how to sort an entity's arrayCollection in symfony2

后端 未结 4 1721
借酒劲吻你
借酒劲吻你 2021-02-01 15:33

I have an entity \"container\" with this property

/**
 * @ORM\\OneToMany(targetEntity=\"BizTV\\ContentManagementBundle\\Entity\\Content\", mappedBy=\"container\"         


        
4条回答
  •  迷失自我
    2021-02-01 16:20

    You should be able to use the @ORM\OrderBy statement which allows you to specify columns to order collections on:

    /**
     * @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
     * @ORM\OrderBy({"sort_order" = "ASC"})
     */
    private $content;
    

    In fact this may be a duplicate of How to OrderBy on OneToMany/ManyToOne

    Edit

    Checking for implementation advice it appears that you must fetch the tables with a join query to the collection in order for the @ORM\OrderBy annotation to work: http://www.krueckeberg.org/notes/d2.html

    This means that you must write a method in the repository to return the container with the contents table joined.

提交回复
热议问题