Single and Multiple Ordering To-Many Associations in Doctrine2

Associations in Doctrine2 can be ordered by default without using an extra query:

 

This also works for multiple ordering:

 

Backlinks:

How to use orderBy param on find_method findAll with Doctrine ObjectSelect

The docs offer an example for an extended version of the DoctrineModule\Form\Element\ObjectSelect:

This find_method parameters allow you to order and sort the result of your repository method.

 

Unfortunately this will not work when using the findAll method. But why?

Looking at the source code of the Doctrine ORM you can see that unlike the findBy method findAll does not allow passing arguments for filtering, sorting nor limiting:

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityRepository.php#L157-182

 

But there is a workaround:

Use the findBy() method instead and simply pass an empty array for the criteria parameter.

 

That’s it!

 

Backlinks: