2

I have set the personal traslation table with this article

I have a table 'AttrSchool' which has column named 'description' to be translated into three language en,ja,th.

and translated text are stored in AttrSchoolTranslation

Now I made the form to edit and update the 'AttrSchool' data.

public function editAttrSchoolAction(Request $request,$schoolId){

    $em = $this->getDoctrine()->getManager();
    $repository = $em->getRepository('UserBundle:AttrSchool');
    $attrSchool = $repository->findOneBy(array('id' => $schoolId));


    foreach ($attrSchool->getTranslations() as $trans){
        if($trans->getLocale() == 'ja'){
            $desc_ja = $trans->getContent();
        }
        if($trans->getLocale() == 'th'){
            $desc_th =  $trans->getContent();
        }
    }

    $form = $this->createFormBuilder($attrSchool)
    ->add('description','textarea',array('required'=> false,'attr' => array('rows' => '8','cols' => '60')))
    ->add('description_th','textarea',array('required'=> false,'data'=> $desc_th,'mapped' => false,'attr' => array('rows' => '8','cols' => '60')))
    ->add('description_ja','textarea',array('required'=> false,'data'=> $desc_ja,'mapped' => false,'attr' => array('rows' => '8','cols' => '60')))

    $form->handleRequest($request);

    if ($form->isValid()) {

        $attrSchool->setDescription($form->get('description_en')->getData());
        $attrSchool->addTranslation(new \Acme\UserBundle\Entity\AttrSchoolTranslation('ja','description',$form->get('description_ja')->getData()));
        $attrSchool->addTranslation(new \Acme\UserBundle\Entity\AttrSchoolTranslation('th','description',$form->get('description_th')->getData()));

However it looks very clumsy. and it inserts the new AttrSchoolTranslation column, not update. (That's of course because I called new \Acme\UserBundle\Entity\AttrSchoolTranslation.....)

however I don't know how to update the data in the translation table.

Does any one know the hint??

whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

0

Persisting multiple translations

Usually it is more convenient to persist more translations when creating or updating a record. Translatable allows to do that through translation repository. All additional translations will be tracked by listener and when the flush will be executed, it will update or persist all additional translations.

Note: these translations will not be processed as ordinary fields of your object, in case if you translate a slug additional translation will not know how to generate the slug, so the value as an additional translation should be processed when creating it.

Example of multiple translations:

<?php
// persisting multiple translations, assume default locale is EN
$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
// it works for ODM also
$article = new Article;
$article->setTitle('My article en');
$article->setContent('content en');

$repository->translate($article, 'title', 'de', 'my article de')
    ->translate($article, 'content', 'de', 'content de')
    ->translate($article, 'title', 'ru', 'my article ru')
    ->translate($article, 'content', 'ru', 'content ru')
;

$em->persist($article);
$em->flush();

// updating same article also having one new translation

$repo
    ->translate($article, 'title', 'lt', 'title lt')
    ->translate($article, 'content', 'lt', 'content lt')
    ->translate($article, 'title', 'ru', 'title ru change')
    ->translate($article, 'content', 'ru', 'content ru change')
    ->translate($article, 'title', 'en', 'title en (default locale) update')
    ->translate($article, 'content', 'en', 'content en (default locale) update')
;
$em->flush();

Did you tried to call the translate method of this $repository ?

$repository = $em->getRepository('Gedmo\\Translatable\\Entity\\Translation');
zilongqiu
  • 846
  • 5
  • 12