Reputation: 1423
I have a symfony2 form with several type of fields. also there is a multiple choice checkbox that generating form entity type with multiple =>true and expanded=>true.
What is the best way to store selected value in database? and how selected data populate in form when editing?
Appreciate your thoughts.
Thanks
Upvotes: 2
Views: 1322
Reputation: 351
I think the best is to have a many to many relationship between your entities and to add a link to your target entity in your "formType", you can call the query builder to filter your options for checkboxes (here "Active Tags"). This Class will work for add and edit Here is an example with 2 Entities : Product and Tag
Entity/Product
/**
* @ORM\ManyToMany(targetEntity="Name\NameBundle\Entity\Tag",cascade={"persist"})
* @ORM\JoinTable(name="product_tags")
*/
private $tags;
Form/ProductType
->add('tags','entity',array("label"=>" your tags",'attr'=>array('class'=>'form-control'),
'class'=>"Name\NameBundle\Entity\Tag",
'property'=>'name',
'multiple'=>true,
'expanded'=>true,
'required'=>false,
'query_builder'=>function(ER $er){
return $er->getActiveTags();
}))
Upvotes: 1