如何使用 kpnlabs 的 DoctrineBehaviors 在奏鸣曲管理中打印可翻译数据 [英] How to print translatable data in sonata admin with DoctrineBehaviors from kpnlabs

查看:23
本文介绍了如何使用 kpnlabs 的 DoctrineBehaviors 在奏鸣曲管理中打印可翻译数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在寻找 knplabs 使用 DoctrineBehaviors 的好方法.

I'm just looking the good way to use DoctrineBehaviors by knplabs.

我已经在这个bundle的帮助下在sonata admin bundle中渲染了一个表单:https://github.com/a2lix/TranslationFormBundle

I have allready render a form in sonata admin bundle with the help of this bundle : https://github.com/a2lix/TranslationFormBundle

现在,我想在管理列表中添加我的翻译字段.

Now, i want to have my translated field in the admin list.

这个时候就用这个方法:

At this time, it's work with this method:

/**
 * @ORM\Entity
 * @ORM\Table(name="sport") 
 */

class Sport
{
...

    public function getNom(){
        return $this->translate()->getNom();
    }
}

这是可行的,但是,我必须重新映射原始实体中的所有翻译字段.我很确定我遗漏了一些东西,尤其是代理翻译的魔法.

It's work but, i have to remap all the translated field in the original entity. I'm pretty sure i'm missing something, particularly with the magic of the proxy translations.

更新:

class Sport
{

    use \Knp\DoctrineBehaviors\Model\Translatable\Translatable;

    public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }   
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    // Need this method for the admin list template
    public function getNom(){
         return $this->translate()->getNom();
    }

    // Work even the precedent method not here, the proxy call work fine.
    public function __toString(){
         return $this->getNom();
    }
}

class SportTranslation
{
    use ORMBehaviors\Translatable\Translation;

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

    /**
     * @return string
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * @param  string
     * @return null
     */
    public function setNom($nom)
    {
        $this->nom = $nom;
    }
}

感谢您的快速回复@nifr!代理方法在控制器中工作(我尝试使用 __toString 运动方法,它工作正常).

Thanks for your fast reply @nifr! The proxy Method work in a controller (i try on the __toString Method of sport, it's work fine).

但问题显然来自奏鸣曲管理包,我检查了模板代码,不知道为什么它不起作用.

But the issue apparently coming from sonata admin bundle, i check the template code, don't know why it doesn't work.

我会保留丑陋的方法,直到找到更好的解决方案.

I will keep the ugly method until i finda better solution.

此时它是在管理列表模板中打印值的唯一方法.

At this time it's the only way to print value in the admin list template.

如果我找到更好的东西,我会更新这篇文章.

If i find something better i will update this post.

推荐答案

如何使用 Knp\DoctrineBehaviors 魔法代理翻译

假设您有 MyClassMyClassTranslation 遵循命名约定(翻译类后缀为 Translation ).

How to use Knp\DoctrineBehaviors magic proxy translations

Given you have MyClass and MyClassTranslation following the naming convention ( translation class suffixed with Translation ).

只有不需要翻译的属性存在于MyClass中,所有可翻译的属性存在于MyClassTranslation中.

Only the properties which don't need to be translated live in MyClass and all translatable properties live in MyClassTranslation.

假设可翻译属性应为 description.

Let's say the translatable property shall be description.

MyClass.php

注意:MyClassdescription 的属性description 和getter/setter 都没有......否则__call() 不会被正确调用!

Attention: Neither property description nor getters/setters for description in MyClass .... otherwise __call() won't be invoked properly!

class MyClass
{

    use \Knp\DoctrineBehaviors\Model\Translatable\Translatable;

    public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }

    protected $nonTranslatableProperty;

    // ...

<小时>

MyClassTranslation.php

use Doctrine\ORM\Mapping as ORM;

class MyClassTranslation
{
    use \Knp\DoctrineBehaviors\Model\Translatable\Translation;


    /**
     * @var string
     */
    protected $description;

    /**
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param string $description
     *
     * @return MyClassTranslation
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

<小时>

现在调用 MyClass::getDescription() 将调用魔术方法 __call() 它将使用当前语言环境返回翻译,因为没有 getDescription() 方法在 MyClass .


Now calling MyClass::getDescription() will invoke the magic method __call() which will return the translation using the current locale because there is no getDescription() method in MyClass .

解决方案:

您应该从 Sport 类中删除 SportTranslation 中存在的所有可翻译的 getter/setter/properties,并添加魔法 __call()方法.

You should remove all the translatable getters/setters/properties present in SportTranslation from your Sport class and instead add the magic __call() method.

这篇关于如何使用 kpnlabs 的 DoctrineBehaviors 在奏鸣曲管理中打印可翻译数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆