Using Synfony2 with Doctrine, I have an Entity (say Country) with a ManyToOne (say City) relation to another. I use the @ORM\OrderBy annotation to tell Doctrine how to fetch the association by default.
I want to display the cities ordered by name in a page, and by size in another, so I want the orderBy being different in the two controllers. Is there a way to tell Doctrine the orderby rule the proxy object should use when hydrating at run time ?
My current working solution is to have two different methods in the repository to fetch the City and set the ORDER BY in DQL. The drawback of this solution is that I have to give Country and City array as two parameters in my twig template, and if the designer use country.cities instead of the cities parameter it will fail:
$country = ...
$cities = $this->getDoctrine()->getRepository("MyBundle:City")->findFromCountryOrderedBySize($country);
return $this->render('MyBundle:Section1:index.html.twig', array(
'country'=>$country,
'cities'=>$cities
));
$country->setCities($cities);in the controller or service then tell your designer to always use$country->getCities(). It also might make it more flexible for the future should your order requirements change. - AlexP