Ordering To-Many Assocations ---------------------------- There are use-cases when you'll want to sort collections when they are retrieved from the database. In userland you do this as long as you haven't initially saved an entity with its associations into the database. To retrieve a sorted collection from the database you can use the ``@OrderBy`` annotation with an collection that specifies an DQL snippet that is appended to all queries with this collection. Additional to any ``@OneToMany`` or ``@ManyToMany`` annotation you can specify the ``@OrderBy`` in the following way: .. code-block:: php 10 However the following: .. code-block:: sql SELECT u, g FROM User u JOIN u.groups g WHERE u.id = 10 ...would internally be rewritten to: .. code-block:: sql SELECT u, g FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name ASC You can reverse the order with an explicit DQL ORDER BY: .. code-block:: sql SELECT u, g FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name DESC ...is internally rewritten to: .. code-block:: sql SELECT u, g FROM User u JOIN u.groups g WHERE u.id = 10 ORDER BY g.name DESC, g.name ASC