我正在创建一个 symfony2 项目,我得到了一个有两个可以翻译的字段的类。对于表单,我将 bundle a2lix 用于 symfony2 2.2 版
该部分工作正常,实体和对应的 entityTranslator 已填充到数据库中。但问题是当我尝试显示实体内容时,这些字段总是显示为默认语言环境而不是实际语言环境。
我的实体:
/**
* Product
*
* @ORM\Table(name="ecm_product")
* @ORM\Entity(repositoryClass="Entity\Repository\ProductRepository")
* @Gedmo\TranslationEntity(class="Entity\Translation\ProductTranslation")
*/
class Product{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Gedmo\Translatable
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255, nullable=true)
*/
protected $slug;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
* @Gedmo\Translatable
*/
protected $description;
/**
* @ORM\OneToMany(
* targetEntity="Entity\Translation\ProductTranslation",
* mappedBy="object",
* cascade={"persist", "remove"}
* )
* @Assert\Valid(deep = true)
*/
private $translations;
public function getTranslations() {
return $this->translations;
}
public function setTranslations($translations) {
foreach ($translations as $translation) {
$translation->setObject($this);
}
$this->translations = $translations;
return $this;
}
/**
* Remove translation
*
* @param Entity\Translation\ProductTranslation $translation
*/
public function removeTranslation(Entity\Translation\ProductTranslation $translation)
{
if ($this->translations->contains($translation)) {
$this->translations->removeElement($translation);
}
}
/**
* Add translation
*
* @param Entity\Translation\ProductTranslation $translation
* @return PrivacyPolicy
*/
public function addTranslation(Entity\Translation\ProductTranslation $translation)
{
if (!$this->translations->contains($translation)) {
$this->translations[] = $translation;
$translation->setObject($this);
}
return $this;
}
}
我的实体翻译:
/**
* Entity\Translation\ProductTranslation.php
* @ORM\Entity
* @ORM\Table(name="ecm_product_translations",
* uniqueConstraints={@ORM\UniqueConstraint(name="lookup_unique_idx", columns={
* "locale", "object_id", "field"
* })}
* )
*/
class ProductTranslation extends AbstractPersonalTranslation
{
/**
* @ORM\ManyToOne(targetEntity="Entity\Product", inversedBy="translations")
* @ORM\JoinColumn(name="object_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $object;
public function getObject() {
return $this->object;
}
public function setObject($object) {
$this->object = $object;
}
}
在我的存储库中,我总是添加这一行:
$qb->getQuery()->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
但是我的实体没有被翻译:s