简体   繁体   中英

Symfony2 associative array in entity

i have entity languages with format

id; name

en; English

pl; Polish

etc...

And I have some Entity translations , that in one column has an associative array: {pl: "Some txt in PL", en: "Some txt in EN",...}

Everything is almost perfect, but i don't know how to create an form for editing such a thing :DI tried almost everything.

Translation.orm.yml :

...
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
            unique: true
        value:
            type: array
...

For new Entity you must prepare your array in Controller ( before creating form). Something like that:

$value = array('en' => 'here en', 'pl' => 'here pl');
$translation->setValue($value);
$form = $this->createForm(new TranslationType()...

In Your FormType:

 $builder->add('value', 'collection', array(
        'type' => 'text',
        'options' => array(
            'required'  => true,
        ),
     )
 );

That's all. If you have {{ form_rest(form) }} in your twig, you'll see two additional fields for translations in your form and it will be work. Additional information about collection field type is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM