Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

symfony - How to print translatable data in sonata admin with DoctrineBehaviors from kpnlabs

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

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:

/**
 * @ORMEntity
 * @ORMTable(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.

UPDATE:

class Sport
{

    use KnpDoctrineBehaviorsModelTranslatableTranslatable;

    public function __call($method, $arguments)
    {
        return $this->proxyCurrentLocaleTranslation($method, $arguments);
    }   
    /**
     * @ORMColumn(type="integer")
     * @ORMId
     * @ORMGeneratedValue(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 ORMBehaviorsTranslatableTranslation;

    /**
     * @ORMColumn(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;
    }
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to use KnpDoctrineBehaviors magic proxy translations

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

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

Let's say the translatable property shall be description.


MyClass.php

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

class MyClass
{

    use KnpDoctrineBehaviorsModelTranslatableTranslatable;

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

    protected $nonTranslatableProperty;

    // ...

MyClassTranslation.php

use DoctrineORMMapping as ORM;

class MyClassTranslation
{
    use KnpDoctrineBehaviorsModelTranslatableTranslation;


    /**
     * @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;
    }

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 .

Solution:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...