title: Symfony2 How to process dynamic embed forms collection?
body: <p>I try this cookbook about embed form:<br>
  <strong><a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">http://symfony.com/doc/current/cookbook/form/form_collections.html</a></strong>  </p>
  
  <p>But the embed foreign key (task_id field in Tag table) is not save, always NULL</p>
  
  <p>Here the complete code: <strong><a href="https://gist.github.com/1755140" rel="nofollow">https://gist.github.com/1755140</a></strong></p>
  
  <p>Do you know why?
  Thank</p>
  
  <p><strong>Edit:</strong>:
  My trouble was in process form action. Like the tag form is embed dynamically, so i don't know how many tag(s) i will have. If i add in createAction</p>
  
  <pre><code>$tag1 = new Tag();
  $task-&gt;addTags($tag1);
  </code></pre>
  
  <p>only the first embed form was correctly save! How to save the other tags?</p>
  
  <pre><code>public function createAction(Request $request) 
  { 
      $task = new Task();
      $tag1 = new Tag();
      $task-&gt;addTags($tag1);
  
      $form    = $this-&gt;createForm(new TaskType(), $task);        
      $form-&gt;bindRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $em-&gt;persist($task);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('new_task', array('id' =&gt; $task-&gt;getId())));   
      }
  
      return array(
          'form'   =&gt; $form-&gt;createView()
      ); 
  }
  </code></pre>
  
  <p><strong>Edit2:</strong></p>
  
  <p>My solution which resolve the trouble, what do you think about it? Better?</p>
  
  <pre><code>public function createAction(Request $request)
  { 
          $task = new Task();
          $tasks = $request-&gt;request-&gt;get('task', array());
          if (isset($tasks['tags'])) {
              $tags = $tasks['tags'];
              foreach($tags as $tag) {
                  $tag = new Tag();
                  $task-&gt;addTags($tag);
              }
          }
  
          $form = $this-&gt;createForm(new TaskType(), $task);        
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $em-&gt;persist($task);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('new_task', array('id' =&gt; $task-&gt;getId())));   
          }
  
          return array(
              'form'   =&gt; $form-&gt;createView()
          );
  }
  </code></pre>
  
  <p><strong>Edit3:</strong></p>
  
  <p>A much better alternative (not tested again)<br>
  <a href="http://www.siteduzero.com/tutoriel-3-523899-creer-des-formulaires-avec-symfony2.html#ss_part_2" rel="nofollow">http://www.siteduzero.com/tutoriel-3-523899-creer-des-formulaires-avec-symfony2.html#ss_part_2</a></p>
  
  <pre><code>public function createAction(Request $request)
  { 
      $task = new Task();
      $form = $this-&gt;createForm(new TaskType(), $task);        
      $form-&gt;bindRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $em-&gt;persist($task);
          foreach($task-&gt;getTags() as $tag) {
              $em-&gt;persist($tag);
          }
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('new_task', array('id' =&gt; $task-&gt;getId())));   
      }
  
      return array(
          'form'   =&gt; $form-&gt;createView()
      );
  }
  </code></pre>