How to sort the findAll Doctrine method

I read the Doctrine documentation, but I could not find a way to find findAll () Results.

I am using symfony2 + doctrine, this is the statement I am using inside my controller:

$this->getDoctrine()->getRepository('MyBundle:MyTable')->findAll();

but I want the results sorted in ascending usernames.

I am trying to pass an array as an argument as follows:

findAll( array('username' => 'ASC') );

but it does not work (he does not complain either).

Is there a way to do this without creating a DQL query?

+74
symfony doctrine2
Jun 14 '13 at 20:38
source share
10 answers

Like @Lighthart, as shown, yes, it is possible, although it adds significant fat to the controller and is NOT DRY.

You really need to define your own query in the object repository, this is a simple and best practice.

 use Doctrine\ORM\EntityRepository; class UserRepository extends EntityRepository { public function findAll() { return $this->findBy(array(), array('username' => 'ASC')); } } 

Then you must tell your entity to search for queries in the repository:

 /** * @ORM\Table(name="User") * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\Repository\UserRepository") */ class User { ... } 

Finally, in your controller:

 $this->getDoctrine()->getRepository('AcmeBundle:User')->findAll(); 
+152
Jun 15 '13 at 4:09
source share
 $this->getDoctrine()->getRepository('MyBundle:MyTable')->findBy([], ['username' => 'ASC']); 
+54
02 Sep '14 at 10:18
source share

It’s useful sometimes to look at the source code.

For example, the implementation of findAll very simple ( vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php ):

 public function findAll() { return $this->findBy(array()); } 

So, we look at findBy and find what we need ( orderBy )

 public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) 
+13
Nov 12 '15 at 10:03
source share

Plain:

 $this->getDoctrine()->getRepository('AcmeBundle:User')->findBy( array(), array('username' => 'ASC') ); 
+6
Apr 04 '17 at 9:58 on
source share

You need to use criteria, for example:

 <?php namespace Bundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Doctrine\Common\Collections\Criteria; /** * Thing controller */ class ThingController extends Controller { public function thingsAction(Request $request, $id) { $ids=explode(',',$id); $criteria = new Criteria(null, <<DQL ordering expression>>, null, null ); $rep = $this->getDoctrine()->getManager()->getRepository('Bundle:Thing'); $things = $rep->matching($criteria); return $this->render('Bundle:Thing:things.html.twig', [ 'entities' => $things, ]); } } 
+5
Jun 14 '13 at 20:47
source share

This works for me:

 $entities = $em->getRepository('MyBundle:MyTable')->findBy(array(),array('name' => 'ASC')); 

Saving the first empty array returns all the data, it worked in my case.

+4
Feb 25 '15 at
source share

Take a look at the Doctrine API source code:

 class EntityRepository{ ... public function findAll(){ return $this->findBy(array()); } ... } 
+4
Mar 02 '16 at 12:24
source share

You can sort an existing ArrayCollection array using an array iterator.

Assuming $ collection is your ArrayCollection returned by findAll ()

 $iterator = $collection->getIterator(); $iterator->uasort(function ($a, $b) { return ($a->getPropery() < $b->getProperty()) ? -1 : 1; }); $collection = new ArrayCollection(iterator_to_array($iterator)); 

This can easily be turned into a function that you can put in your repository to create the findAllOrderBy () method.

+2
Jun 15 '13 at 12:10
source share

I am using an alternative to the solution that nifr wrote.

 $resultRows = $repository->fetchAll(); uasort($resultRows, function($a, $b){ if ($a->getProperty() == $b->getProperty()) { return 0; } return ($a->getProperty()< $b->getProperty()) ? -1 : 1; }); 

This is faster than the ORDER BY clause, and without the overhead of Iterator.

+2
Dec 30 '15 at 11:53
source share

Try the following:

 $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('MyBundle:MyTable')->findBy(array(), array('username' => 'ASC')); 
+2
Jun 24 '15 at 18:57
source share



All Articles