I am using tags on a form using tagsinput :
This plugin ends-up with a single text field containing tags separated by a comma (eg: tag1,tag2,...)
Those tags are currently managed on a non-mapped form field:
$builder
// ...
->add('tags', 'text', array(
'mapped' => false,
'required' => false,
))
;
And finally, they are stored on an ArrayCollection, as this is a bad practice to store multiple values in a database field:
/**
* @var ArrayCollection[FiddleTag]
*
* @ORM\OneToMany(targetEntity="FiddleTag", mappedBy="fiddle", cascade={"all"}, orphanRemoval=true)
*/
protected $tags;
To map my form to my entity, I can do some code in my controller like this:
$data->clearTags();
foreach (explode(',', $form->get('tags')->getData()) as $tag)
{
$fiddleTag = new FiddleTag();
$fiddleTag->setTag($tag);
$data->addTag($fiddleTag);
}
But this looks the wrong way at first sight.
I am wondering what is the best practice to map my entity to my form, and my form to my entity.