简体   繁体   中英

Symfony2 & Translatable : entity's locale is empty

I am trying to display a list of categories with Translatable behavior. My default locale is 'fr'.

In my 'ext_translations' table, I have all records needed for locale 'en'.

My controller :

    ....
    $this->get('session')->setLocale('en');
    $categories = $this->getDoctrine()->getRepository('MyBundle:Category')->findAll();
    ....

The problem is that when I display all retrieved categories, I get the 'fr' translations instead of the 'en'.

I tried to display the $locale variable from my Category entity, and it is empty .

The only solution I have is to add this in my controller :

    ....
    $em = $this->getDoctrine()->getEntityManager();
    foreach($categories as $cat){
        $cat->setTranslatableLocale($this->get('session')->getLocale());
        $em->refresh($cat);
    }
    ....

But of course, it is not a good solution.

Any help ? Why is the $locale variable of my entity empty ?

Thanks for your help,

Regards,

Aurel

EDIT

My Entity :

<?php

namespace Acme\MyBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;

/**
 * Acme\MyBundle\Entity\Category
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="Acme\MyBundle\Repository\CategoryRepository")
 */
class Category implements Translatable
{

    /**
     * @var smallint $id
     *
     * @ORM\Column(name="id", type="smallint", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string $title
     *
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
     */
    private $title;

    /**
     * @Gedmo\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     */
    private $locale;

    public function setTranslatableLocale($locale)
    {
        $this->locale = $locale;
    }

    public function getLocale(){
        return $this->locale;
    }

    /* ... all getters and setters ... */

    /**
     * Set title
     *
     * @param string $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

}

I found a solution, but it means that the StofDoctrineExtensionsBundle is not working properly.

I had to edit my config.yml file to add the LocaleListener explicitly :

my_translatable_locale_listener:
    class: Stof\DoctrineExtensionsBundle\EventListener\LocaleListener
    arguments: [@stof_doctrine_extensions.listener.translatable]
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

If somebody has a better solution...

Aurel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM