Symfony3 : choice type field filled with array of objects
<p>I have an entity <code>Product</code>. My product can have multiple names in different languages. A name in french, a name in english, etc. I don't want to use an automatic translation.</p>

<p>The user will have to write the names in the Product form and select the corresponding language. He can add so many names as he wants thanks to an Add button.</p>

<p>All the languages are created by the admin user (in another form). So, <code>Language</code> is also an Entity which have a name (ex: English) and a code (ex: EN).</p>

<p>I created the Entity <code>ProductName</code> which have a name and a language (that conform to what the user writes in the Product form).</p>

<p>In that case, I don't need to associate Entity <code>ProductName</code> with Entity <code>Language</code>. I just want the language code. So, in my <code>ProductName</code> entity, I have this property : </p>

<pre><code>/**
 * @ORM\Column(name="Language_Code", type="string", length=2)
 */
private $language;
</code></pre>

<p>My Product form (ProductType) has a CollectionType field in order to add several names.</p>

<pre><code>// Form/ProductType.php

    -&gt;add('infos',      CollectionType::class, array(
        'entry_type'    =&gt; ProductInfosType::class,
        'allow_add'     =&gt; true,
        'allow_delete'  =&gt; true,
        'prototype'     =&gt; true,
        'label'         =&gt; false,
        'mapped'        =&gt; false
    ))
</code></pre>

<p>And ProductInfosType form has 2 fields :</p>

<pre><code>// Form/ProductInfosType.php

        -&gt;add('name',           TextType::class, array(
            'attr'              =&gt; array('size' =&gt; 40)
        ))
        -&gt;add('language',       EntityType::class, array(
            'placeholder'       =&gt; '',
            'class'             =&gt; 'AppBundle:Language',
            'choice_label'      =&gt; 'code',
            'attr'              =&gt; array('class' =&gt; 'lang'),
            'query_builder'     =&gt; function (EntityRepository $er) {
                return $er-&gt;createQueryBuilder('l')-&gt;orderBy('l.code', 'ASC');
            }
        ))
</code></pre>

<p>So, when I go on my form page, I have a block which contains an input text field (Name) and a select field (language). The select field is like this :</p>

<pre><code>&lt;select id="product_infos_0_language" required="required" name="product[infos][0][language]"&gt;
    &lt;option value=""&gt;&lt;/option&gt;
    &lt;option value="DE"&gt;DE&lt;/option&gt;
    &lt;option value="EN"&gt;EN&lt;/option&gt;
    &lt;option value="ES"&gt;ES&lt;/option&gt;
    &lt;option selected="selected" value="FR"&gt;FR&lt;/option&gt;
&lt;/select&gt; 
</code></pre>

<p><strong>At this point, everything works well.</strong> I created an add button so that the user can add other names, etc...</p>

<p>But, when I submit the form, when I check form data in my ProductController, I noticed that it does not correspond to what I want to store in database.</p>

<pre><code>print_r($form-&gt;get('infos')-&gt;getData());

// returns :
Array
(
    [0] =&gt; AppBundle\Entity\ProductName Object
        ( 
            [language:AppBundle\Entity\ProductName:private] =&gt; AppBundle\Entity\Language Object
                (
                    [code:AppBundle\Entity\Language:private] =&gt; FR
                    [name:AppBundle\Entity\Language:private] =&gt; Français
                )

            [name:AppBundle\Entity\ProductName:private] =&gt; Ceinture lombaire LombaSkin
        )
)
</code></pre>

<p>What I would like is :</p>

<pre><code>Array
(
    [0] =&gt; AppBundle\Entity\ProductName Object
        ( 
            [language:AppBundle\Entity\ProductName:private] =&gt; FR    
            [name:AppBundle\Entity\ProductName:private] =&gt; Ceinture lombaire LombaSkin
        )
)
</code></pre>

<p>I don't want the language object but <strong>directly the language code</strong> !</p>

<p>That's why I think that I should not use <code>EntityField</code> in ProductNameType form but <code>ChoiceType</code> field.</p>

<p><strong>How can I load all the languages stored in db in the choice field ?</strong>
I hope that this explanation is more understandable ;-)</p>