Reputation: 5180
Is there a way to sort a Doctrine 2 EntityRepository
Object with specific criteria or do I have to use a DQL query if I want to pass specific sorting parameters?
Upvotes: 2
Views: 4144
Reputation: 5225
You can return an ordered set of elements by passing a second argument into the find___ methods, this argument must be an associative array where the key is the name of the field you want to order and the value is either 'ASC' or 'DESC'.
// query for one product matching be name and price
$product = $repository->findOneBy(array('name' => 'foo', 'price' => 19.99));
// query for all products matching the name, ordered by price
$product = $repository->findBy(
array('name' => 'foo'),
array('price' => 'ASC')
);
http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database
Of course, if you want a most sophisticated behavior you should implement a custom repository.
Upvotes: 5