There are Service and Tag entities with ManyToMany relationship.
class Service {
/**
* @ORM\ManyToMany(targetEntity="Tag", mappedBy="serviceList",cascade={"persist"})
*/
private $tagList;
}
class Tag{
/**
* @var string
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Service", inversedBy="tagList")
* @ORM\JoinTable(name="tags_services")
*/
private $serviceList;
}
Here is ServiceType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array(
'label' => 'Title',
))
->add('tags', TextType::class, array(
'label' => 'Tags',
'mapped' => false
))
;
}
When editing Service entity, how can I get service tags names separated by comma in tags text form field?
For example I have 'Math tutor' service with tagList: 'Math', 'Tutor', 'Lessons'. And when I try to edit it, the form should look like:
| title | Math tutor | <- text field
| tags | Math, Tutor, Lessons | <- text field
You should use DataTransformer: http://symfony.com/doc/current/form/data_transformers.html
This is a small example for a simpler case: