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,