1

我有使用DococineExtensions的Translatable字段的實體。這是它的外觀:使用DoctrineExtensions翻譯的克隆Doctrine實體可翻譯

/** 
* @ORM\Table 
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\SurveyAnswerRepository") 
* @Gedmo\Loggable 
*/ 
class SurveyAnswer 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

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

    /** 
    * @Gedmo\Locale 
    * Used locale to override Translation listener`s locale 
    * this is not a mapped field of entity metadata, just a simple property 
    * and it is not necessary because globally locale can be set in listener 
    */ 
    private $locale; 

    public function __clone() 
    { 
     if ($this->getId()) 
     { 
      $this->id = null; 
     } 
    } 

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

    public function getId() 
    { 
     return $this->id; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

} 

當我克隆這樣的實體,新建一個只包括翻譯在當前區域的Symfony應用程序設置。所有其他語言的翻譯缺失。

+0

如何存儲在翻譯?你有訪問他們嗎? –

+0

在單獨的表中,但我想避免手動複製(我的意思是寫一些額外的代碼來發現和完成缺失的翻譯),如果可能的話:) – Marek

回答

0

您可以使用Gedmo存儲庫函數來獲取所有可轉換字段,並將它們轉換爲新實體。

在你的控制器克隆功能(例如SurveyController :: duplicateAction):

$newEntity = clone $entity; 
$em = $this->getDoctrine()->getManager(); 
$transRepo = $em->getRepository('Gedmo\Translatable\Entity\Translation'); 

// Get all translations from original entity 
$translations = $transRepo->findTranslations($entity); 
foreach ($translations as $locale => $fields) { 
    foreach ($fields as $field => $value) { 
     // Add new translation for new entity, per locale, per field 
     $transRepo->translate($newEntity, $field, $locale, $value); 
    } 
} 
$em->flush();