Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using doctrine translatable together with Symfony 2 forms

I have Entity in my app, which has 2 translatable fields, using Doctrine Translatable Extension:

class Page implements Translatable
{
    /......

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

    /**
     * @var text $content
     * @Gedmo\Translatable  
     * @ORM\Column(name="content", type="text")
     */
    private $content; 

 /........
}

I use one table for multiple entities translations. Now i would like to use one form to get the original and translated (to 1 language) values for these attributes, so it should have 4 fields.

I've defined new form derivating from AbstractType and tried to add those 2 fields using FormBuilder, but it says that their corresponding entities do not contain these fields. I've tried to add these fields to entities, and declare getters for them, but the only way i know to get translations for entities is to use dedicated entity manager and AFAIK providing entity manager access to entity isn't good practice.

Is there a way to use forms to handle such thing?

like image 482
kars7e Avatar asked Nov 04 '22 13:11

kars7e


1 Answers

I know it's an old question, but anyway.

You could put the fields for translated content using FormBuilder with attribute:

array('mapped'=>false)

Take the data like this:

$form->get('field_name')->getData();

and then persist it like this

https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/translatable.md#multi-translations

I hope it helps somebody.

like image 57
ggavrilut Avatar answered Nov 15 '22 10:11

ggavrilut