How to sort findAll Doctrine's method? How to sort findAll Doctrine's method? symfony symfony

How to sort findAll Doctrine's method?


As @Lighthart as shown, yes it's possible, although it adds significant fat to the controller and isn't DRY.

You should really define your own query in the entity repository, it's 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 look 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();


$this->getDoctrine()->getRepository('MyBundle:MyTable')->findBy([], ['username' => 'ASC']);


Simple:

$this->getDoctrine()->getRepository('AcmeBundle:User')->findBy(    array(),    array('username' => 'ASC'));