如何使用 knp 实验室可翻译学说行为访问已翻译 属性
How to access translated property using knp labs translatable doctrine behaviors
我正在使用可翻译的学说,并且我有一个具有可翻译属性的实体。这看起来像这样。
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;
}
我有一个文件 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;
}
}
我想从控制器访问文本:
$item = $em->getRepository('AppMyBundle:Scaleitem')->find(1);
dump($item->getText());
这行不通。有人对我的问题有提示吗?
如 translatable docs 所示,您可以使用以下方式访问翻译:
$item->translate('en')->getName();
当您需要特定语言时
或在 Scaleitem
实体中添加 __call
方法(而不是在已翻译的实体上):
/**
* @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);
}
然后使用 $item->getName();
并始终检索当前语言环境中的任何 "property"。
我正在使用可翻译的学说,并且我有一个具有可翻译属性的实体。这看起来像这样。
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;
}
我有一个文件 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;
}
}
我想从控制器访问文本:
$item = $em->getRepository('AppMyBundle:Scaleitem')->find(1);
dump($item->getText());
这行不通。有人对我的问题有提示吗?
如 translatable docs 所示,您可以使用以下方式访问翻译:
$item->translate('en')->getName();
当您需要特定语言时或在
Scaleitem
实体中添加__call
方法(而不是在已翻译的实体上):/** * @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); }
然后使用
$item->getName();
并始终检索当前语言环境中的任何 "property"。