Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 & Translatable : entity's locale is empty
    text
    copied!<p>I am trying to display a list of categories with Translatable behavior. My default locale is 'fr'.</p> <p>In my 'ext_translations' table, I have all records needed for locale 'en'.</p> <p>My controller :</p> <pre><code> .... $this-&gt;get('session')-&gt;setLocale('en'); $categories = $this-&gt;getDoctrine()-&gt;getRepository('MyBundle:Category')-&gt;findAll(); .... </code></pre> <p>The problem is that when I display all retrieved categories, I get the 'fr' translations instead of the 'en'.</p> <p>I tried to display the $locale variable from my Category entity, and it is <strong>empty</strong>.</p> <p>The only solution I have is to add this in my controller :</p> <pre><code> .... $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); foreach($categories as $cat){ $cat-&gt;setTranslatableLocale($this-&gt;get('session')-&gt;getLocale()); $em-&gt;refresh($cat); } .... </code></pre> <p>But of course, it is not a good solution.</p> <p>Any help ? Why is the $locale variable of my entity empty ?</p> <p>Thanks for your help,</p> <p>Regards,</p> <p>Aurel</p> <p><strong>EDIT</strong></p> <p>My Entity :</p> <pre><code>&lt;?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-&gt;locale = $locale; } public function getLocale(){ return $this-&gt;locale; } /* ... all getters and setters ... */ /** * Set title * * @param string $title */ public function setTitle($title) { $this-&gt;title = $title; } /** * Get title * * @return string */ public function getTitle() { return $this-&gt;title; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload