title: Symfony Edit and persist multiple entities on one form
body: <p>I have an entity called Objective and its property called 'weight' and I can edit one objective at a time.. and now i want to edit all weights in a single form and persist them in DB using Doctrine..</p>
  
  <p>This issue helped me in bringing all objective weights on a single form..
  <a href="https://stackoverflow.com/questions/31532695/edit-multiple-entities-in-one-form">Edit multiple entities in one form</a> </p>
  
  <p>Now I am trickling around on how to save all weight values from the form in DB with a single click of save button..</p>
  
  <p>Below is  my code so far.. </p>
  
  <p>This is the  controller action:-</p>
  
  <pre><code>/**
   * @Route("/mou/objectives/manage", name="objective_manage")
   */
  public function manageAction(Request $request){  
  
      $objs = $this-&gt;getDoctrine()
      -&gt;getRepository('AppBundle:Objective')
      -&gt;findAll();
  
      $form = $this-&gt;createFormBuilder()
      -&gt;add('weight', 'collection', array(
          'type' =&gt; new WeightType() ,
          'allow_add' =&gt; false,
          'allow_delete' =&gt; false,
          'label' =&gt; false))
      -&gt;add('save', 'submit')
      -&gt;getForm();
  
      $form-&gt;setData(array('weight' =&gt; $objs));
  
      /* Till here things are fine */
  
      $form-&gt;handleRequest($request);
  
      if($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()){
          /* **Here I need help how to persist all weight values.. maybe inside a loop.. hence This is the missing piece** */
  
          foreach($objs as $obj){
              $obj =new Objective();
              $obj = $em-&gt;getRepository('AppBundle:Objective')-&gt;find($obj);
              $obj-&gt;setWeight($obj); 
              $em-&gt;persist($obj);
  
          }
          $em-&gt;flush();    
          return $this-&gt;redirectToRoute('objective_manage');
      } 
  
      return $this-&gt;render('keyobjective/manage.html.twig', array(
          'objs' =&gt; $objs,
          'form' =&gt; $form-&gt;createView()
          ));
  }
  </code></pre>
  
  <p>This is the FormType:-</p>
  
  <pre><code>class WeightType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('weight','text', array('label' =&gt; false));
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\Objective'
              //'data_class' =&gt; null
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'appbundle_objective';
      }
  }
  </code></pre>
  
  <p>and this is twig template:-</p>
  
  <pre><code>{% extends 'base.html.twig' %}
  {% block body %}
      {{form_start(form)}}
  
      {{form_label(form.weight,'Enter Weight')}}
      {{form_widget(form.weight)}}
  
      {{form_end(form)}}
  {% endblock %}
  </code></pre>
  
  <p>Any suggested approach..!!</p>