l3pp4rd/DoctrineExtensions — Issue #261: Translatable: how to return records from translation table
Author: booradleys
Created: 2012-02-05 12:17:36+00:00
State: open

Hi there i don't succeed to return records from translationh table.

$evm = new \Doctrine\Common\EventManager();
$translatableListener = new \Gedmo\Translatable\TranslatableListener();
$translatableListener->setTranslatableLocale('fr_fr');
$translatableListener->setTranslationFallback(true);
$evm->addEventSubscriber($translatableListener);

Entity/Country.php

namespace Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
* Country
*
* @ORM\Table(name="country")
* @ORM\Entity(repositoryClass="Repository\CountryRepository")
* @Gedmo\TranslationEntity(class="Entity\CountryTranslation")
*/
class Country
{
/**
* @var string $code
*
* @ORM\Column(name="code", type="string", length=2, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $code;

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

/**
* @ORM\OneToMany(
* targetEntity="CountryTranslation",
* mappedBy="object",
* cascade={"persist", "remove"}
* )
*/
private $translations;

/**
* @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 __construct()
{
$this->translations = new ArrayCollection();
}

public function getTranslations()
{
return $this->translations;
}

public function addTranslation(CountryTranslation $t)
{
//if (!$this->translations->contains($t)) {
$this->translations[] = $t;
$t->setObject($this);
//}
}

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

Entity/CountryTranslation.php:

namespace Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonalTranslation;

/**
* @ORM\Entity
* @ORM\Table(name="country_translations",
* uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_id", "field"
* })}
* )
*/
class CountryTranslation extends AbstractPersonalTranslation
{
/**
* Convinient constructor
*
* @param string $locale
* @param string $field
* @param string $value
*/
public function __construct($locale, $field, $value)
{
$this->setLocale($locale);
$this->setField($field);
$this->setContent($value);
}

/**
* @ORM\ManyToOne(targetEntity="Country", inversedBy="translations")
* @ORM\JoinColumn(name="object_id", referencedColumnName="code", onDelete="CASCADE")
*/
protected $object;
}

CREATE TABLE IF NOT EXISTS `country_translations` (
`locale` char(5) NOT NULL,
`object_id` char(2) NOT NULL,
`field` varchar(30) NOT NULL,
`content` varchar(255) NOT NULL,
UNIQUE KEY `lookup_unique_idx` (`locale`,`object_id`,`field`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `country_translations` (`locale`, `object_id`, `field`, `content`) VALUES
('en_en', 'AF', 'name', 'Afganistan');

Repository/CountryRepository.php:

class CountryRepository extends EntityRepository
{
function getListePays() {
$em = $this->_em;
$qb = $em->createQueryBuilder();
$qb->select("cou")
->from('Entity\Country','cou');
$qb->orderBy("cou.name","ASC");

$q = $qb->getQuery();
$q->setHint(\Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE, 'en_en');
$results = $q->getResult();
$q->free();
return $results;
}
}

Calling getListePays returns all countries in french (in country table).
"Afganistan" (in country_translation table) is not returned.