How to access translatable property using knp labs translatable doctrine skills

I use doctrine translatable and I have an entity that has a translatable attribute. It looks 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 ScaleitemTranslation file:

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 the controller:

$item = $em->getRepository('AppMyBundle:Scaleitem')->find(1);
dump($item->getText());

This does not work. Will someone tell me my problem?

+4
source share
1 answer

As shown in the translated documents , you can access the translation using:

  • $item->translate('en')->getName();when you want to specify a specific language
  • or adding a method __callto an object Scaleitem(and not to a translated object):

    /**
     * @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(); "" .

+3

All Articles