3

I have a "Category" entity containing

/**
 * @ORM\Column(type="string", length=255)
 */
protected $nameFr;

/**
 * @ORM\Column(type="string", length=255)
 */
protected $nameEn;

Now, I'm trying to display the localized name in the view, I can display one or the other using:

{{ categories.nameFr }} and {{ categories.nameEn }}

So I made a method called getName() so I can use {{ categories.name }}

I only needed to access the locale so I added a protected $locale property to the entity with the setter and getter and I set the locale before calling the view (I use @Template for the return by the way):

$locale = $this->getRequest()->getLocale();
$categories->setLocale($locale);

return array(
    'categories' => $categories
);

This was working, but now I implemented a pagination bundle KnpLabs/KnpPaginatorBundle which requires to send a query instead of the entity:

$em = $this->getDoctrine()->getManager();

$categoriesQuery = $em->createQueryBuilder()
    ->select('category')
    ->from('OylexCategoryBundle:Category', 'category')
;

$locale = $this->getRequest()->getLocale();

$categoriesQuery->setLocale($locale);

$paginator  = $this->get('knp_paginator');
$categoriesPagination = $paginator->paginate(
    $categoriesQuery,
    $this->get('request')->query->get('page', 1),
    30
);

return array(
    'categoriesPagination' => $categoriesPagination
);

This fails with the error message: FatalErrorException: Error: Call to undefined method Doctrine\ORM\QueryBuilder::setLocale().

If I try the method setLocale() on $categoriesPagination instead, it fails with the error message: FatalErrorException: Error: Call to undefined method Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination::setLocale()

Is there a way to pass the locale to the entity? or Is there even a better way to handle this situation?

Thanks,

Oylex
  • 314
  • 5
  • 20

1 Answers1

5

You shouldn't do something like propertyEn and propertyFr in general.

Just store your translations together with a locale property which makes fetching them from database in a query as easy as:

// example findByLocale($locale) method

->select('entity.property')
->from('AcmeMyBundle:Entity', 'entity')   
// or inside a repository $this->createQueryBuilder('entity')

->where('entity.locale = :locale')
->setParameter('locale', $locale)

But this has all been done before ...

You should either use Gemo\DoctrineExtensions\Translatable, which can easily be integrated with symfony2 using Stof\DoctrineExtensionsBundle

... or my tip if using PHP 5.4+ with traits available KnpLabs\DoctrineBehaviors\Translatable.

In order to integrate these nicely with your forms use a2lix\TranslationFormBundle.

See my answer here for a quick insight on using DoctrineBehaviors\Translatable and the current locale proxy which i found to be a really comfortable solution.

Just create class Entity and EntityTranslation, include the proxy lines ... call $entity->getProperty()

-> current locale applied automatically. as easy as it can be :-)

Community
  • 1
  • 1
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • Thanks, I chose the traits method and it's working, but now I am trying to use a translated field as the property of a form entity field type: `'property' => 'name'` I get the error: "Neither property "name" nor method "getName()" nor method "isName()" exists in class", I tried with the query_builder option, but it says that the entity has no field or association named locale, I supose I have to join the EntityTranslation, I'll find out how to do that, is this the way or is there a better one? – Oylex Jul 11 '13 at 01:39
  • Update: I tried a few things with the query_builder option and no matter what I do, "name" is never a valid value for the property option, but I can call getName();exit; in the query_builder closure and it works, it just never leaves the closure – Oylex Jul 11 '13 at 02:19