I need to fetch a list of countries, sorted by alphabetical order. Since I have the entity translated in four languages (english, french, spanish and chinese), I've used gedmo doctrine extensions in order to manage the translation. The problem is when I fetch this list in a entity form field type:
$form = $builder->add('country', 'entity',
array('class' => 'GroupCommonBundle:Country',
'query_builder' => function(EntityRepository $er) {
$query = $er->createQueryBuilder('c')->orderBy('c.name');
}
the results are sorted as original entity defined (english) and not current locale (spanish or french), what is I really need. Actually I use $this->container->getParameter('locale')
I've tried to force a hook in the query, as explained here:
$query->getQuery()->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $this->container->getParameter('locale'));
but AFAIK, this only works when the query is written as dql:
$query = $this->getDoctrine()->getManager()->createQuery('
SELECT c
FROM GroupCommonBundle:Country c
ORDER BY c.name ASC');
$query->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, $this->container->getParameter('locale'));
which is not allowed by the entity form filed, because it's waiting for a queryBuilder object.
So, I need to get my collection translated and sorted in his current language in a form. Anyone knows how this can be achieved?