I have a Product entity. My product can have several names in different languages. Name in French, name in English, etc. I do not want to use automatic translation.
The user will have to write down the names in the product form and select the appropriate language. He can add as many names as he wants thanks to the Add button.
All languages are created by admin (in a different form). Thus, Language also an entity that has a name (for example: English) and a code (for example: EN).
I created an Entity ProductName that has a name and language (which correspond to what the user writes in the form of the product).
In this case, I do not need to associate the Entity ProductName with the Entity Language . I just need a language code. So, in my ProductName object, I have this property:
/** * @ORM\Column(name="Language_Code", type="string", length=2) */ private $language;
In my product form (ProductType) there is a CollectionType field to add multiple names.
// Form/ProductType.php ->add('infos', CollectionType::class, array( 'entry_type' => ProductInfosType::class, 'allow_add' => true, 'allow_delete' => true, 'prototype' => true, 'label' => false, 'mapped' => false ))
And the ProductInfosType form has 2 fields:
// Form/ProductInfosType.php ->add('name', TextType::class, array( 'attr' => array('size' => 40) )) ->add('language', EntityType::class, array( 'placeholder' => '', 'class' => 'AppBundle:Language', 'choice_label' => 'code', 'attr' => array('class' => 'lang'), 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('l')->orderBy('l.code', 'ASC'); } ))
So, when I go to my form page, I have a block that contains an input text field (Name) and a selection field (language). The select field is as follows:
<select id="product_infos_0_language" required="required" name="product[infos][0][language]"> <option value=""></option> <option value="DE">DE</option> <option value="EN">EN</option> <option value="ES">ES</option> <option selected="selected" value="FR">FR</option> </select>
At this point, everything is working well. I created an add button so that the user can add other names, etc.
But when I submit the form, when I validate the form data in my ProductController, I noticed that this does not match what I want to store in the database.
print_r($form->get('infos')->getData());
I would like to:
Array ( [0] => AppBundle\Entity\ProductName Object ( [language:AppBundle\Entity\ProductName:private] => FR [name:AppBundle\Entity\ProductName:private] => Ceinture lombaire LombaSkin ) )
I do not want the language object, but directly indicated the language code !
That's why I think I should not use EntityField in the form of ProductNameType, but ChoiceType .
How to load all languages stored in db in the select box? I hope this explanation is more clear :-)