I have a particular table that I always want to have sorted in a specific way (say, the users table must be sorted by username, always).When I run this:
$this->getDoctrine()->getRepository('User')->findAll();
I want the returned list to be ordered by username, without having to pass this every single time in the controller.
According to this answer, the way to implement this is by "defining your own query in the entity repository". I am new to Doctrine and not sure exactly where this should be done. Please note that I am not using Symfony, only the Doctrine ORM.
If I understand correctly, I have to set this up:
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findAll()
{
return $this->findBy(array(), array('username' => 'ASC'));
}
}
My question is: in what file should I configure this? In which folder? I have found the original function in the main doctrine folder (vendor/doctrine/orm/lib/Doctrine/ORM), in a file called EntityRepository.php, but editing this file means that: a) changes will be overwritten upon updates to Doctrine, and b) this applies to ALL of my tables, not just to this one.