8

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?

2
  • 1
    The right way of translating select fields: stackoverflow.com/a/14150093/1232526 Commented Nov 27, 2013 at 22:02
  • @Noy: He's talking about a much more advanced case, involving Gedmo translatable entities. Your suggested way is not applicable here. Commented Apr 13, 2016 at 14:01

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Would be useful if this was part of the DoctrineExtensions library. I made mine a TranslatableEntityType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.