2

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

1

1 Answer 1

1

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();
                                          }))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.