Coder Perfect

How to use findBy() in Doctrine to sort results

Problem

On a Doctrine repository, I’m using the findBy() method:

$entities = $repository->findBy(array('type'=> 'C12'));

What is the best way to sort the results?

Asked by Mirage

Solution #1

ORDER is the second argument of findBy.

$ens = $em->getRepository('AcmeBinBundle:Marks')
          ->findBy(
             array('type'=> 'C12'), 
             array('id' => 'ASC')
           );

Answered by xdazz

Solution #2

$ens = $em->getRepository('AcmeBinBundle:Marks')
              ->findBy(
                 array(), 
                 array('id' => 'ASC')
               );

Answered by Jethik

Solution #3

$cRepo = $em->getRepository('KaleLocationBundle:Country');

// Leave the first array blank
$countries = $cRepo->findBy(array(), array('name'=>'asc'));

Answered by Bhaktaraz

Post is based on https://stackoverflow.com/questions/12048452/how-to-order-results-with-findby-in-doctrine