如何使用knp实验室翻译原则行为来访问翻译的财产 [英] How to access translated property using knp labs translatable doctrine behaviors
本文介绍了如何使用knp实验室翻译原则行为来访问翻译的财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Scaleitem
{
/ **
*必须定义为翻译此实体
* /
使用ORMBehaviors\Translatable\Translatable;
/ **
* @ ORM\Column(type =integer)
* @ ORM\Id
* @ ORM\GeneratedValue =AUTO)
* /
protected $ id;
}
我有一个文件ScaleitemTranslation:
class ScaleitemTranslation
{
使用ORMBehaviors\Translatable\Translation;
/ **
* @ ORM\Column(type =string,length = 255)
* /
protected $ text;
/ **
*设置文本
*
* @param string $ text
* @return ScaleitemTranslation
* /
public function setText($ text)
{
$ this-> text = $ text;
return $ this;
}
/ **
*获取文本
*
* @return string
* /
public function getText )
{
return $ this-> text;
}
}
我想从控制器访问文本:
$ item = $ em-> getRepository('AppMyBundle:Scaleitem') - > find(1);
dump($ item-> getText());
这不行。有人为我的问题提示?
解决方案
如可翻译文档,您可以使用以下方式访问翻译:
-
$ item-> translate('en') - > getName();
当您想要特定语言 -
或添加
__调用
方法在Scaleitem
实体(而不是翻译实体):/ **
* @param $ method
* @param $ args
*
* @return mixed
* /
public function __call($ method,$ args)
{
if(!method_exists(self :: getTranslationEntityClass(),$ method)){
$ method ='得到'。 ucfirst($方法);
}
return $ this-> proxyCurrentLocaleTranslation($ method,$ args);
}
然后使用
$ item-> getName( );
,并始终检索当前区域设置中的任何属性。
I am using doctrine translatable and i have a entity which have an translatable attribute. This look like this.
class Scaleitem
{
/**
* Must be defined for translating this entity
*/
use ORMBehaviors\Translatable\Translatable;
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
And i have a file ScaleitemTranslation:
class ScaleitemTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @ORM\Column(type="string", length=255)
*/
protected $text;
/**
* Set text
*
* @param string $text
* @return ScaleitemTranslation
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
{
return $this->text;
}
}
I would like to access the text from a controller:
$item = $em->getRepository('AppMyBundle:Scaleitem')->find(1);
dump($item->getText());
This do not work. Have someone a hint for my issue?
解决方案
As showed in the translatable docs you can access the translation using:
$item->translate('en')->getName();
when you want a specific languageor adding the
__call
method in theScaleitem
entity (not on translated entity):/** * @param $method * @param $args * * @return mixed */ public function __call($method, $args) { if (!method_exists(self::getTranslationEntityClass(), $method)) { $method = 'get' . ucfirst($method); } return $this->proxyCurrentLocaleTranslation($method, $args); }
to then use
$item->getName();
and always retrieve any "property" in the current locale.
这篇关于如何使用knp实验室翻译原则行为来访问翻译的财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文