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?
I had the same problem as you. I don't know if there is a better solution to solve the problem but it works for me. The solution is override the entity form type.
forms.xml
<service id="acme.form.type.entity" class="Acme\AcmeBundle\Form\Type\EntityType">
<tag name="form.type" alias="entity" />
<argument type="service" id="doctrine" />
</service>
EntityType.php
<?php
namespace Acme\AcmeBundle\Form\Type;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Bridge\Doctrine\Form\Type\DoctrineType;
use Acme\AcmeBundle\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
class EntityType extends DoctrineType
{
public function getLoader(ObjectManager $manager, $queryBuilder, $class)
{
return new ORMQueryBuilderLoader($queryBuilder, $manager, $class);
}
public function getName()
{
return 'entity';
}
}
ORMQueryBuilderLoader.php
namespace Acme\AcmeBundle\Doctrine\Form\ChoiceList;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader as BaseORMQueryBuilderLoader;
class ORMQueryBuilderLoader extends BaseORMQueryBuilderLoader
{
private $queryBuilder;
public function __construct($queryBuilder, $manager = null, $class = null)
{
parent::__construct($queryBuilder, $manager, $class);
$this->queryBuilder = $queryBuilder($manager->getRepository($class));
}
public function getEntities()
{
$query = $this->queryBuilder->getQuery();
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
return $query->execute();
}
}
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With