php - Edit multiple entities in one form -


i have entity question , form type editing question questiontype. can successfull edit 1 question. create link editing questions.

i edit questions in 1 form, how can handle this? try use collections, don't know how map single question in form collection , i'm not sure if that's right approach.

my questiontype like:

public function buildform(formbuilderinterface $builder, array $options)     {         $categories = $this->entitymanager->getrepository('myappbundle:questioncategory')->findbyisactive(true);          $builder->add('category', 'entity', array(                         'class' => 'myappbundle:questioncategory',                         'choices' => $categories,                         'label' => 'category',                         'translation_domain' => 'messages',                         'multiple' => false,                         'empty_value' => 'msg.pleaseselect',                         'expanded' => false))                 ->add('translations', 'a2lix_translations', array(                             'fields' => array(                                 'text' => array(                                     'field_type' => 'textarea',                                     'label' => 'hinttext',                                     'attr' => array('class' => 'rte')                                 ),                                 'explanation' => array(                                     'field_type' => 'textarea',                                     'label' => 'title',                                     'attr' => array('class' => '')                                 )                             )                 ));     }      public function setdefaultoptions(optionsresolverinterface $resolver)     {         $resolver->setdefaults(array(             'data_class' => 'my\appbundle\entity\question',         ));     } 

my controller action edit questions, like:

$em = $this->getdoctrine()->getmanager(); $questions = $em->getrepository('myappbundle:question')->findall();  /**   * -- here problem , how can $questions form? ---  **/           $form = $this->createformbuilder()     ->add('questions', 'collection', array(             'type' => new questiontype() ,             'allow_add' => false,             'allow_delete' => false,             'label' => false)     )     ->add('save', 'submit', array('label' => 'create'))     ->getform();   $form->handlerequest($request);  if ($form->isvalid()) { }  return $this->render('myappbundle:question:editallquestions.html.twig', array("form" => $form->createview())); 

has hint or approach?

you're on right track, want is:

$form->setdata(array('questions' => $yourquestionarray)); 

just after ->getform() before ->handlerequest($request).

or pass option createformbuilder() so:

$form = $this->createformbuilder(array('questions' => $yourquestionarray))     ->add(...) 

they both same thing.

ordinarily when create form class (which looks you're doing questiontype), provide data_class configuration option (see documentation) defines form uses model.

what you're doing in controller creating kind of "anonymous" form doesn't have associated data class (documentation, looks you're doing in controller). in case data array (similarly if called $form->getdata() you'd array back).


Comments