Symfony2&可翻译:实体的区域设置为空 [英] Symfony2 & Translatable : entity's locale is empty
问题描述
我正在尝试显示具有可翻译行为的类别列表。
我的默认语言环境是'fr'。
在我的'ext_translations'表中,我有locale'en'所需的所有记录。 b
$ b
我的控制器:
....
$ this->获得( '会议') - >的setLocale( '恩');
$ categories = $ this-> getDoctrine() - > getRepository('MyBundle:Category') - > findAll();
....
问题是当我显示所有检索到的类别时,我得到'fr'翻译而不是'en'。
我试图从我的类别实体显示$ locale变量,它是空。
唯一的解决方案是在我的控制器中添加:
....
$ em = $ this-> getDoctrine() - > getEntityManager();
foreach($ categories as $ cat){
$ cat-> setTranslatableLocale($ this-> get('session') - > getLocale());
$ em-> refresh($ cat);
}
....
但当然不是好的解决方案。
任何帮助?
为什么我的实体的$ locale变量为空?
感谢您的帮助,
请问,
Aurel
编辑
我的实体:
<?php
命名空间Acme\MyBundle \实体;
使用Gedmo\Mapping\Annotation作为Gedmo;
使用Doctrine\ORM\Mapping作为ORM;
使用Gedmo\Translatable\Translatable;
/ **
* Acme\MyBundle\Entity\Category
*
* @ ORM\Table(name =category)
* @ ORM\Entity(repositoryClass =Acme\MyBundle\Repository\CategoryRepository)
* /
类别实现可翻译
{
/ **
* @var smallint $ id
*
* @ ORM\Column(name =id,type =smallint,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* /
private $ id;
/ **
* @var string $ title
*
* @ Gedmo\Translatable
* @ ORM\Column(name = title,type =string,length = 255,nullable = false)
* /
private $ title;
/ **
* @ Gedmo\Locale
*使用的区域设置来覆盖翻译监听器的区域设置
*这不是实体元数据的映射字段,只是一个简单的属性
* /
private $ locale;
public function setTranslatableLocale($ locale)
{
$ this-> locale = $ locale;
}
public function getLocale(){
return $ this-> locale;
}
/ * ...所有的getter和setter ... * /
/ **
*设置标题
*
* @param string $ title
* /
public function setTitle($ title)
{
$ this-> title = $ title;
}
/ **
*获取标题
*
* @return string
* /
public function getTitle )
{
return $ this-> title;
}
}
我找到了一个解决方案,但这意味着StofDoctrineExtensionsBundle无法正常工作。
我不得不编辑我的config.yml文件来显式添加LocaleListener: / p>
my_translatable_locale_listener:
class:Stof\DoctrineExtensionsBundle\EventListener\LocaleListener
arguments:[@stof_doctrine_extensions .listener.translatable]
标签:
- {name:kernel.event_listener,event:kernel.request,方法:onKernelRequest}
如果有人有更好的解决方案...
Aurel
I am trying to display a list of categories with Translatable behavior. My default locale is 'fr'.
In my 'ext_translations' table, I have all records needed for locale 'en'.
My controller :
....
$this->get('session')->setLocale('en');
$categories = $this->getDoctrine()->getRepository('MyBundle:Category')->findAll();
....
The problem is that when I display all retrieved categories, I get the 'fr' translations instead of the 'en'.
I tried to display the $locale variable from my Category entity, and it is empty.
The only solution I have is to add this in my controller :
....
$em = $this->getDoctrine()->getEntityManager();
foreach($categories as $cat){
$cat->setTranslatableLocale($this->get('session')->getLocale());
$em->refresh($cat);
}
....
But of course, it is not a good solution.
Any help ? Why is the $locale variable of my entity empty ?
Thanks for your help,
Regards,
Aurel
EDIT
My Entity :
<?php
namespace Acme\MyBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
/**
* Acme\MyBundle\Entity\Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="Acme\MyBundle\Repository\CategoryRepository")
*/
class Category implements Translatable
{
/**
* @var smallint $id
*
* @ORM\Column(name="id", type="smallint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $title
*
* @Gedmo\Translatable
* @ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* @Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getLocale(){
return $this->locale;
}
/* ... all getters and setters ... */
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
I found a solution, but it means that the StofDoctrineExtensionsBundle is not working properly.
I had to edit my config.yml file to add the LocaleListener explicitly :
my_translatable_locale_listener:
class: Stof\DoctrineExtensionsBundle\EventListener\LocaleListener
arguments: [@stof_doctrine_extensions.listener.translatable]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
If somebody has a better solution...
Aurel
这篇关于Symfony2&可翻译:实体的区域设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!