title: symfony2 : unable to save parent id in embedded object
body: <p>I'm trying tu use my first embedded form (SF2.1 + Doctrine2)</p>
  
  <p>the entities : </p>
  
  <pre><code>&lt;?php
  namespace Chris\BabelBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Entity
   */
  class Needs
  {
      /**
       * @ORM\GeneratedValue
       * @ORM\Id
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
           * @ORM\ManyToOne(targetEntity="Projects")
           * @Assert\NotBlank()
           */
      private $relatedproject;
  
      /**
           * @ORM\ManyToOne(targetEntity="Activities")
           * @Assert\NotBlank()
           */
      private $relatedactivity;
  
      /**
           * @ORM\ManyToOne(targetEntity="Jobs")
           * @Assert\NotBlank()
           */
      private $relatedjob;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank()
       */
      private $firstname;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank()
       */
      private $lastname;
  
      /**
           * @ORM\ManyToOne(targetEntity="Experiences")
           * @Assert\NotBlank()
           */
      private $relatedexperience;
  
      /**
           * @ORM\ManyToOne(targetEntity="Plants")
           * @Assert\NotBlank()
           */
      private $maintrainingplant;
  
      /**
           * @ORM\Column(type="date")
           * @Assert\NotBlank()
           */
      private $trainingneedstart;
  
      /**
           * @ORM\Column(type="date")
           * @Assert\NotBlank()
           */
      private $readytowork;
  
      /**
       * @var ArrayCollection $trainings
       *
       * @ORM\OneToMany(targetEntity="Trainings", mappedBy="training", cascade={"persist", "remove", "merge"})
       */
      private $trainings;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;trainings = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set firstname
       *
       * @param string $firstname
       * @return Needs
       */
      public function setFirstname($firstname)
      {
          $this-&gt;firstname = $firstname;
  
          return $this;
      }
  
      /**
       * Get firstname
       *
       * @return string 
       */
      public function getFirstname()
      {
          return $this-&gt;firstname;
      }
  
      /**
       * Set lastname
       *
       * @param string $lastname
       * @return Needs
       */
      public function setLastname($lastname)
      {
          $this-&gt;lastname = $lastname;
  
          return $this;
      }
  
      /**
       * Get lastname
       *
       * @return string 
       */
      public function getLastname()
      {
          return $this-&gt;lastname;
      }
  
      /**
       * Set trainingneedstart
       *
       * @param \DateTime $trainingneedstart
       * @return Needs
       */
      public function setTrainingneedstart($trainingneedstart)
      {
          $this-&gt;trainingneedstart = $trainingneedstart;
  
          return $this;
      }
  
      /**
       * Get trainingneedstart
       *
       * @return \DateTime 
       */
      public function getTrainingneedstart()
      {
          return $this-&gt;trainingneedstart;
      }
  
      /**
       * Set readytowork
       *
       * @param \DateTime $readytowork
       * @return Needs
       */
      public function setReadytowork($readytowork)
      {
          $this-&gt;readytowork = $readytowork;
  
          return $this;
      }
  
      /**
       * Get readytowork
       *
       * @return \DateTime 
       */
      public function getReadytowork()
      {
          return $this-&gt;readytowork;
      }
  
      /**
       * Set relatedproject
       *
       * @param \Chris\BabelBundle\Entity\Projects $relatedproject
       * @return Needs
       */
      public function setRelatedproject(\Chris\BabelBundle\Entity\Projects $relatedproject = null)
      {
          $this-&gt;relatedproject = $relatedproject;
  
          return $this;
      }
  
      /**
       * Get relatedproject
       *
       * @return \Chris\BabelBundle\Entity\Projects 
       */
      public function getRelatedproject()
      {
          return $this-&gt;relatedproject;
      }
  
      /**
       * Set relatedactivity
       *
       * @param \Chris\BabelBundle\Entity\Activities $relatedactivity
       * @return Needs
       */
      public function setRelatedactivity(\Chris\BabelBundle\Entity\Activities $relatedactivity = null)
      {
          $this-&gt;relatedactivity = $relatedactivity;
  
          return $this;
      }
  
      /**
       * Get relatedactivity
       *
       * @return \Chris\BabelBundle\Entity\Activities 
       */
      public function getRelatedactivity()
      {
          return $this-&gt;relatedactivity;
      }
  
      /**
       * Set relatedjob
       *
       * @param \Chris\BabelBundle\Entity\Jobs $relatedjob
       * @return Needs
       */
      public function setRelatedjob(\Chris\BabelBundle\Entity\Jobs $relatedjob = null)
      {
          $this-&gt;relatedjob = $relatedjob;
  
          return $this;
      }
  
      /**
       * Get relatedjob
       *
       * @return \Chris\BabelBundle\Entity\Jobs 
       */
      public function getRelatedjob()
      {
          return $this-&gt;relatedjob;
      }
  
      /**
       * Set relatedexperience
       *
       * @param \Chris\BabelBundle\Entity\Experiences $relatedexperience
       * @return Needs
       */
      public function setRelatedexperience(\Chris\BabelBundle\Entity\Experiences $relatedexperience = null)
      {
          $this-&gt;relatedexperience = $relatedexperience;
  
          return $this;
      }
  
      /**
       * Get relatedexperience
       *
       * @return \Chris\BabelBundle\Entity\Experiences 
       */
      public function getRelatedexperience()
      {
          return $this-&gt;relatedexperience;
      }
  
      /**
       * Set maintrainingplant
       *
       * @param \Chris\BabelBundle\Entity\Plants $maintrainingplant
       * @return Needs
       */
      public function setMaintrainingplant(\Chris\BabelBundle\Entity\Plants $maintrainingplant = null)
      {
          $this-&gt;maintrainingplant = $maintrainingplant;
  
          return $this;
      }
  
      /**
       * Get maintrainingplant
       *
       * @return \Chris\BabelBundle\Entity\Plants 
       */
      public function getMaintrainingplant()
      {
          return $this-&gt;maintrainingplant;
      }
  
      /**
       * Add trainings
       *
       * @param \Chris\BabelBundle\Entity\Trainings $trainings
       * @return Needs
       */
      public function addTraining(\Chris\BabelBundle\Entity\Trainings $trainings)
      {
          $this-&gt;trainings[] = $trainings;
  
          return $this;
      }
  
      /**
       * Remove trainings
       *
       * @param \Chris\BabelBundle\Entity\Trainings $trainings
       */
      public function removeTraining(\Chris\BabelBundle\Entity\Trainings $trainings)
      {
          $this-&gt;trainings-&gt;removeElement($trainings);
      }
  
      /**
       * Get trainings
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getTrainings()
      {
          return $this-&gt;trainings;
      }
      public function __toString() {return $this-&gt;getFirstname();}
  }
  
  
  
  &lt;?php
  namespace Chris\BabelBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  /**
   * @ORM\Entity
   */
  class Trainings
  {
      /**
       * @ORM\GeneratedValue
       * @ORM\Id
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
           * @ORM\ManyToOne(targetEntity="Plants")
           * @Assert\NotBlank()
           */
      private $trainingplant;
  
      /**
           * @ORM\Column(type="date")
           * @Assert\NotBlank()
           */
      private $trainingstart;
  
      /**
           * @ORM\Column(type="date")
           * @Assert\NotBlank()
           */
      private $trainingend;
  
      /**
       * @var Needs $trainings
       *
       * @ORM\ManyToOne(targetEntity="Needs", inversedBy="trainings", cascade={"persist", "remove", "merge"})
       * @ORM\JoinColumns({
       *  @ORM\JoinColumn(name="Needs_id", referencedColumnName="id")
       * })
       */
      private $training;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set trainingstart
       *
       * @param \DateTime $trainingstart
       * @return Trainings
       */
      public function setTrainingstart($trainingstart)
      {
          $this-&gt;trainingstart = $trainingstart;
  
          return $this;
      }
  
      /**
       * Get trainingstart
       *
       * @return \DateTime 
       */
      public function getTrainingstart()
      {
          return $this-&gt;trainingstart;
      }
  
      /**
       * Set trainingend
       *
       * @param \DateTime $trainingend
       * @return Trainings
       */
      public function setTrainingend($trainingend)
      {
          $this-&gt;trainingend = $trainingend;
  
          return $this;
      }
  
      /**
       * Get trainingend
       *
       * @return \DateTime 
       */
      public function getTrainingend()
      {
          return $this-&gt;trainingend;
      }
  
      /**
       * Set trainingplant
       *
       * @param \Chris\BabelBundle\Entity\Plants $trainingplant
       * @return Trainings
       */
      public function setTrainingplant(\Chris\BabelBundle\Entity\Plants $trainingplant = null)
      {
          $this-&gt;trainingplant = $trainingplant;
  
          return $this;
      }
  
      /**
       * Get trainingplant
       *
       * @return \Chris\BabelBundle\Entity\Plants 
       */
      public function getTrainingplant()
      {
          return $this-&gt;trainingplant;
      }
  
      /**
       * Set training
       *
       * @param \Chris\BabelBundle\Entity\Needs $training
       * @return Trainings
       */
      public function setTraining(\Chris\BabelBundle\Entity\Needs $training = null)
      {
          $this-&gt;training = $training;
          return $this;
      }
  
      /**
       * Get training
       *
       * @return \Chris\BabelBundle\Entity\Needs 
       */
      public function getTraining()
      {
          return $this-&gt;training;
      }
  }
  </code></pre>
  
  <p>The 2 Controllers :</p>
  
  <pre><code>&lt;?php
  
  namespace Chris\BabelBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  use Chris\BabelBundle\Entity\Needs;
  use Chris\BabelBundle\Form\NeedsType;
  
  /**
   * Needs controller.
   *
   */
  class NeedsController extends Controller
  {
      /**
       * Lists all Needs entities.
       *
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('ChrisBabelBundle:Needs')-&gt;findAll();
  
          return $this-&gt;render('ChrisBabelBundle:Needs:index.html.twig', array(
              'entities' =&gt; $entities,
          ));
      }
  
      /**
       * Finds and displays a Needs entity.
       *
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Needs')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Needs entity.');
          }
  
          return $this-&gt;render('ChrisBabelBundle:Needs:show.html.twig', array(
              'entity'      =&gt; $entity,        ));
      }
  
      /**
       * Displays a form to create a new Needs entity.
       *
       */
      public function newAction()
      {
          $entity = new Needs();
          $form   = $this-&gt;createForm(new NeedsType(), $entity);
  
          return $this-&gt;render('ChrisBabelBundle:Needs:new.html.twig', array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          ));
      }
  
      /**
       * Creates a new Needs entity.
       *
       */
      public function createAction(Request $request)
      {
          $entity  = new Needs();
  
          $form = $this-&gt;createForm(new NeedsType(), $entity);
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('needs_show', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return $this-&gt;render('ChrisBabelBundle:Needs:new.html.twig', array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          ));
      }
  
      /**
       * Displays a form to edit an existing Needs entity.
       *
       */
      public function editAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Needs')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Needs entity.');
          }
  
          $editForm = $this-&gt;createForm(new NeedsType(), $entity);
  
          return $this-&gt;render('ChrisBabelBundle:Needs:edit.html.twig', array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
          ));
      }
  
      /**
       * Edits an existing Needs entity.
       *
       */
      public function updateAction(Request $request, $id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Needs')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Needs entity.');
          }
  
          $editForm = $this-&gt;createForm(new NeedsType(), $entity);
          $editForm-&gt;bind($request);
  
          if ($editForm-&gt;isValid()) {
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('needs_edit', array('id' =&gt; $id)));
          }
  
          return $this-&gt;render('ChrisBabelBundle:Needs:edit.html.twig', array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
          ));
      }
  
      /**
       * Deletes a Needs entity.
       *
       */
      public function deleteAction($id)
      {
          $em = $this-&gt;container-&gt;get('doctrine')-&gt;getEntityManager();
          $metier = $em-&gt;find('ChrisBabelBundle:Needs', $id);
          if (!$metier)
          {
              throw new NotFoundHttpException("Need not found");
          }
          $em-&gt;remove($metier); $em-&gt;flush();
          return $this-&gt;redirect($this-&gt;generateUrl('needs'));
      }
  }
  
  
  
  
  
  
  &lt;?php
  
  namespace Chris\BabelBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  
  use Chris\BabelBundle\Entity\Trainings;
  use Chris\BabelBundle\Form\TrainingsType;
  
  /**
   * Trainings controller.
   *
   */
  class TrainingsController extends Controller
  {
      /**
       * Lists all Trainings entities.
       *
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('ChrisBabelBundle:Trainings')-&gt;findAll();
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:index.html.twig', array(
              'entities' =&gt; $entities,
          ));
      }
  
      /**
       * Finds and displays a Trainings entity.
       *
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Trainings')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Trainings entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:show.html.twig', array(
              'entity'      =&gt; $entity,
              'delete_form' =&gt; $deleteForm-&gt;createView(),        ));
      }
  
      /**
       * Displays a form to create a new Trainings entity.
       *
       */
      public function newAction()
      {
          $entity = new Trainings();
          $form   = $this-&gt;createForm(new TrainingsType(), $entity);
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:new.html.twig', array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          ));
      }
  
      /**
       * Creates a new Trainings entity.
       *
       */
      public function createAction(Request $request)
      {
          $entity  = new Trainings();
          $form = $this-&gt;createForm(new TrainingsType(), $entity);
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('trainings_show', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:new.html.twig', array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          ));
      }
  
      /**
       * Displays a form to edit an existing Trainings entity.
       *
       */
      public function editAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Trainings')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Trainings entity.');
          }
  
          $editForm = $this-&gt;createForm(new TrainingsType(), $entity);
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:edit.html.twig', array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          ));
      }
  
      /**
       * Edits an existing Trainings entity.
       *
       */
      public function updateAction(Request $request, $id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('ChrisBabelBundle:Trainings')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find Trainings entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
          $editForm = $this-&gt;createForm(new TrainingsType(), $entity);
          $editForm-&gt;bind($request);
  
          if ($editForm-&gt;isValid()) {
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('trainings_edit', array('id' =&gt; $id)));
          }
  
          return $this-&gt;render('ChrisBabelBundle:Trainings:edit.html.twig', array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          ));
      }
  
      /**
       * Deletes a Trainings entity.
       *
       */
      public function deleteAction(Request $request, $id)
      {
          $form = $this-&gt;createDeleteForm($id);
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $entity = $em-&gt;getRepository('ChrisBabelBundle:Trainings')-&gt;find($id);
  
              if (!$entity) {
                  throw $this-&gt;createNotFoundException('Unable to find Trainings entity.');
              }
  
              $em-&gt;remove($entity);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirect($this-&gt;generateUrl('trainings'));
      }
  
      private function createDeleteForm($id)
      {
          return $this-&gt;createFormBuilder(array('id' =&gt; $id))
              -&gt;add('id', 'hidden')
              -&gt;getForm()
          ;
      }
  }
  </code></pre>
  
  <p>and finally the forms :</p>
  
  <pre><code>&lt;?php
  
  namespace Chris\BabelBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class NeedsType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('firstname')
              -&gt;add('lastname')
              -&gt;add('trainingneedstart')
              -&gt;add('readytowork')
              -&gt;add('relatedproject')
              -&gt;add('relatedactivity')
              -&gt;add('relatedjob')
              -&gt;add('relatedexperience')
              -&gt;add('maintrainingplant')
  // Ajout du formulaire imbriqué des trainings en collection
              -&gt;add('trainings', 'collection', array('type' =&gt; new TrainingsType(),
                  'allow_add' =&gt; true,
                  'allow_delete' =&gt; true,
                  'by_reference' =&gt; false,
                  ))
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Chris\BabelBundle\Entity\Needs'
          ));
      }
  
      public function getName()
      {
          return 'chris_babelbundle_needstype';
      }
  }
  
  
  
  &lt;?php
  
  namespace Chris\BabelBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class TrainingsType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('trainingstart')
              -&gt;add('trainingend')
              -&gt;add('trainingplant')
              -&gt;add('training')
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Chris\BabelBundle\Entity\Trainings'
          ));
      }
  
      public function getName()
      {
          return 'chris_babelbundle_trainingstype';
      }
  }
  </code></pre>
  
  <p>most of this is working except that the trainings saved do not contain the needs ID in the DB so, when editing a need I cant retrieve the several trainings.</p>