Create query in entity form field
Asked Answered
S

1

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?

Sciomancy answered 24/9, 2013 at 14:17 Comment(2)
The right way of translating select fields: https://mcmap.net/q/466506/-translate-select-options-in-symfony2-class-formsKillam
@Noy: He's talking about a much more advanced case, involving Gedmo translatable entities. Your suggested way is not applicable here.Athalla
C
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.

Cercaria answered 27/2, 2014 at 21:45 Comment(1)
Would be useful if this was part of the DoctrineExtensions library. I made mine a TranslatableEntityType.Athalla

© 2022 - 2024 — McMap. All rights reserved.