title: many to one relationship: form validation doesn't create foreign key in database
body: <p>I have a Many to one relationship between PostCategory &amp; SubCategory like this:</p>
  
  <p>SubCategory Entity:</p>
  
  <pre><code>namespace VisualImmersion\AdminBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * SubCategory
   *
   * @ORM\Table(name="post_category_subcategory")
   * @ORM\Entity(repositoryClass="VisualImmersion\AdminBundle\Entity\Repository\SubCategoryRepository")
   */
  class SubCategory
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="ID", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\ManyToOne(targetEntity="\VisualImmersion\AdminBundle\Entity\PostCategory", cascade={"remove", "persist"}, inversedBy="subCategories")
       * @ORM\JoinColumn(referencedColumnName="ID")
       */
      private $postCategory;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return SubCategory
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set category
       *
       * @param \VisualImmersion\AdminBundle\Entity\PostCategory $category
       */
      public function setPostCategory(\VisualImmersion\AdminBundle\Entity\PostCategory $category)
      {
          $this-&gt;postCategory = $category;
      }
  
      /**
       * Get category
       *
       * @return \VisualImmersion\AdminBundle\Entity\PostCategory
       */
      public function getPostCategory()
      {
          return $this-&gt;postCategory;
      }
  }
  </code></pre>
  
  <p>PostCategory entity:</p>
  
  <pre><code>&lt;?php
  
  namespace VisualImmersion\AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * PostCategory
   *
   * @ORM\Table(name="post_category")
   * @ORM\Entity(repositoryClass="VisualImmersion\AdminBundle\Entity\Repository\PostCategoryRepository")
   */
  class PostCategory
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="ID", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=45, nullable=false)
       */
      private $name;
  
      /**
       * @ORM\ManyToMany(targetEntity="Post", mappedBy="posts")
       */
      private $posts;
  
      /**
       * @ORM\OneToMany(targetEntity="SubCategory", mappedBy="postCategory", cascade={"remove", "persist"})
       */
      protected $subCategories;
  
  
      public function __construct()
      {
          $this-&gt;posts = new \Doctrine\Common\Collections\ArrayCollection();
          $this-&gt;subCategories = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      public function __toString()
      {
          return (String) $this-&gt;id;
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return PostCategory
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Add post
       *
       * @param \VisualImmersion\AdminBundle\Entity\Post $post
       */
      public function addPost(\VisualImmersion\AdminBundle\Entity\Post $post)
      {
          $this-&gt;posts[] = $post;
      }
  
      /**
       * Get posts
       *
       * @return \Doctrine\Common\Collections\ArrayCollection()
       */
      public function getPosts()
      {
          return $this-&gt;posts-&gt;toArray();
      }
  
      /**
       * Remove post
       *
       * @param \VisualImmersion\AdminBundle\Entity\Post $post
       */
      public function removePost(\VisualImmersion\AdminBundle\Entity\Post $post)
      {
          $this-&gt;posts-&gt;removeElement($post);
      }
  
  
      /**
       * Add subCategory
       *
       * @param \VisualImmersion\AdminBundle\Entity\SubCategory $subCategory
       */
      public function addSubCategory(\VisualImmersion\AdminBundle\Entity\SubCategory $subCategory)
      {
          $this-&gt;subCategories[] = $subCategory;
          $subCategory-&gt;setPostCategory($this);
      }
  
      /**
       * Get subCategories
       *
       * @return \Doctrine\Common\Collections\ArrayCollection()
       */
      public function getSubCategories()
      {
          return $this-&gt;subCategories;
      }
  
      /**
       * Set subCategories
       *
       * @param \Doctrine\Common\Collections\ArrayCollection $subCategories
       */
      public function setSubCategories(\Doctrine\Common\Collections\ArrayCollection $subCategories)
      {
          foreach ($subCategories as $subCategory) {
               $subCategory-&gt;setPostCategory($this);
          }
  
          $this-&gt;subCategories = $subCategories;
      }
  
      /**
       * Remove subCategory
       *
       * @param \VisualImmersion\AdminBundle\Entity\SubCategory $subCategory
       */
      public function removeSubCategory(\VisualImmersion\AdminBundle\Entity\SubCategory $subCategory)
      {
          $this-&gt;subCategories-&gt;removeElement($subCategory);
      }
  }
  </code></pre>
  
  <p>I have embedded forms to add collection of SubCategories on PostCategory form:</p>
  
  <p>SubCategoryType:</p>
  
  <pre><code>&lt;?php
  
  namespace VisualImmersion\AdminBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class SubCategoryType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
  
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'VisualImmersion\AdminBundle\Entity\SubCategory'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'visualimmersion_adminbundle_subcategorytype';
      }
  }
  </code></pre>
  
  <p>PostCategoryType:</p>
  
  <pre><code>&lt;?php
  
  namespace VisualImmersion\AdminBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class PostCategoryType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
              -&gt;add('subCategories', 'collection', array(
                  'type' =&gt; new SubCategoryType(),
                  'required' =&gt;false,
                  'allow_add' =&gt; true,
                  'allow_delete' =&gt; true,
                  'prototype' =&gt; true,
                  'prototype_name' =&gt;'subCategory__name__',
                  'by_reference' =&gt; 'false'
              ));
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'VisualImmersion\AdminBundle\Entity\PostCategory'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'visualimmersion_adminbundle_postcategorytype';
      }
  }
  </code></pre>
  
  <p>My problem is in subCategory table in database, the postCategory foreign key was not added:</p>
  
  <p>I try desperately to find a solution to this problem, so I ask for your help.</p>
  
  <p>Edit:</p>
  
  <p>My controllers:
  PostCategory</p>
  
  <pre><code>&lt;?php
  
  namespace VisualImmersion\AdminBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use VisualImmersion\AdminBundle\Entity\PostCategory;
  use VisualImmersion\AdminBundle\Form\PostCategoryType;
  
  /**
   * PostCategory controller.
   *
   * @Route("/postcategory")
   */
  class PostCategoryController extends Controller
  {
  
      /**
       * Lists all PostCategory entities.
       *
       * @Route("/", name="postcategory")
       * @Method("GET")
       * @Template()
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('VisualImmersionAdminBundle:PostCategory')-&gt;findAll();
  
          return array(
              'entities' =&gt; $entities,
          );
      }
      /**
       * Creates a new PostCategory entity.
       *
       * @Route("/create", name="postcategory_create")
       * @Method("POST")
       * @Template("VisualImmersionAdminBundle:PostCategory:new.html.twig")
       */
      public function createAction(Request $request)
      {
          $entity  = new PostCategory();
          $form = $this-&gt;createForm(new PostCategoryType(), $entity);
          $form-&gt;submit($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('postcategory_index', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to create a new PostCategory entity.
       *
       * @Route("/new", name="postcategory_new")
       * @Method("GET")
       * @Template()
       */
      public function newAction()
      {
          $entity = new PostCategory();
          $form   = $this-&gt;createForm(new PostCategoryType(), $entity);
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Finds and displays a PostCategory entity.
       *
       * @Route("/{id}", name="postcategory_show")
       * @Method("GET")
       * @Template()
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:PostCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find PostCategory entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to edit an existing PostCategory entity.
       *
       * @Route("/edit/{id}", name="postcategory_edit")
       * @Method("GET")
       * @Template()
       */
      public function editAction($id)
      {
  
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:PostCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find PostCategory entity.');
          }
  
          $editForm = $this-&gt;createForm(new PostCategoryType(), $entity);
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Edits an existing PostCategory entity.
       *
       * @Route("/update/{id}", name="postcategory_update")
       * @Method("PUT")
       * @Template("VisualImmersionAdminBundle:PostCategory:edit.html.twig")
       */
      public function updateAction(Request $request, $id)
      {
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:PostCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find PostCategory entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
          $editForm = $this-&gt;createForm(new PostCategoryType(), $entity);
  
          $editForm-&gt;submit($request);
          if ($editForm-&gt;isValid()) {
  
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('postcategory_edit', array('id' =&gt; $id)));
          }
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
      /**
       * Deletes a PostCategory entity.
       *
       * @Route("/delete/{id}", name="postcategory_delete")
       * @Method("DELETE")
       */
      public function deleteAction(Request $request, $id)
      {
          $form = $this-&gt;createDeleteForm($id);
          $form-&gt;submit($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:PostCategory')-&gt;find($id);
  
              if (!$entity) {
                  throw $this-&gt;createNotFoundException('Unable to find PostCategory entity.');
              }
  
              $em-&gt;remove($entity);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirect($this-&gt;generateUrl('postcategory_index'));
      }
  
      /**
       * Creates a form to delete a PostCategory entity by id.
       *
       * @param mixed $id The entity id
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createDeleteForm($id)
      {
          return $this-&gt;createFormBuilder(array('id' =&gt; $id))
              -&gt;add('id', 'hidden')
              -&gt;getForm()
          ;
      }
  }
  </code></pre>
  
  <p>SubCategory:</p>
  
  <pre><code>&lt;?php
  
  namespace VisualImmersion\AdminBundle\Controller;
  
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use VisualImmersion\AdminBundle\Entity\SubCategory;
  use VisualImmersion\AdminBundle\Form\SubCategoryType;
  
  /**
   * SubCategory controller.
   *
   * @Route("/subcategory")
   */
  class SubCategoryController extends Controller
  {
  
      /**
       * Lists all SubCategory entities.
       *
       * @Route("/", name="subcategory")
       * @Method("GET")
       * @Template()
       */
      public function indexAction()
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entities = $em-&gt;getRepository('VisualImmersionAdminBundle:SubCategory')-&gt;findAll();
  
          return array(
              'entities' =&gt; $entities,
          );
      }
      /**
       * Creates a new SubCategory entity.
       *
       * @Route("/", name="subcategory_create")
       * @Method("POST")
       * @Template("VisualImmersionAdminBundle:SubCategory:new.html.twig")
       */
      public function createAction(Request $request)
      {
          $entity  = new SubCategory();
          $form = $this-&gt;createForm(new SubCategoryType(), $entity);
          $form-&gt;submit($request);
  
          $logger = $this-&gt;get('logger');
          $logger-&gt;info('Nous sommes dans le Create Action de SubCategory');
  
          if ($form-&gt;isValid()) {
              $logger-&gt;info('Le formulaire semble valide dans le Create Action de SubCategory');
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('subcategory_show', array('id' =&gt; $entity-&gt;getId())));
          }
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to create a new SubCategory entity.
       *
       * @Route("/new", name="subcategory_new")
       * @Method("GET")
       * @Template()
       */
      public function newAction()
      {
          $logger = $this-&gt;get('logger');
          $logger-&gt;info('Nous sommes dans le New Action de SubCategory');
          $entity = new SubCategory();
          $form   = $this-&gt;createForm(new SubCategoryType(), $entity);
  
          return array(
              'entity' =&gt; $entity,
              'form'   =&gt; $form-&gt;createView(),
          );
      }
  
      /**
       * Finds and displays a SubCategory entity.
       *
       * @Route("/{id}", name="subcategory_show")
       * @Method("GET")
       * @Template()
       */
      public function showAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:SubCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find SubCategory entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Displays a form to edit an existing SubCategory entity.
       *
       * @Route("/{id}/edit", name="subcategory_edit")
       * @Method("GET")
       * @Template()
       */
      public function editAction($id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:SubCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find SubCategory entity.');
          }
  
          $editForm = $this-&gt;createForm(new SubCategoryType(), $entity);
          $deleteForm = $this-&gt;createDeleteForm($id);
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
  
      /**
       * Edits an existing SubCategory entity.
       *
       * @Route("/{id}", name="subcategory_update")
       * @Method("PUT")
       * @Template("VisualImmersionAdminBundle:SubCategory:edit.html.twig")
       */
      public function updateAction(Request $request, $id)
      {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
  
          $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:SubCategory')-&gt;find($id);
  
          if (!$entity) {
              throw $this-&gt;createNotFoundException('Unable to find SubCategory entity.');
          }
  
          $deleteForm = $this-&gt;createDeleteForm($id);
          $editForm = $this-&gt;createForm(new SubCategoryType(), $entity);
          $editForm-&gt;submit($request);
  
          if ($editForm-&gt;isValid()) {
              $em-&gt;persist($entity);
              $em-&gt;flush();
  
              return $this-&gt;redirect($this-&gt;generateUrl('subcategory_edit', array('id' =&gt; $id)));
          }
  
          return array(
              'entity'      =&gt; $entity,
              'edit_form'   =&gt; $editForm-&gt;createView(),
              'delete_form' =&gt; $deleteForm-&gt;createView(),
          );
      }
      /**
       * Deletes a SubCategory entity.
       *
       * @Route("/{id}", name="subcategory_delete")
       * @Method("DELETE")
       */
      public function deleteAction(Request $request, $id)
      {
          $form = $this-&gt;createDeleteForm($id);
          $form-&gt;submit($request);
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $entity = $em-&gt;getRepository('VisualImmersionAdminBundle:SubCategory')-&gt;find($id);
  
              if (!$entity) {
                  throw $this-&gt;createNotFoundException('Unable to find SubCategory entity.');
              }
  
              $em-&gt;remove($entity);
              $em-&gt;flush();
          }
  
          return $this-&gt;redirect($this-&gt;generateUrl('subcategory'));
      }
  
      /**
       * Creates a form to delete a SubCategory entity by id.
       *
       * @param mixed $id The entity id
       *
       * @return \Symfony\Component\Form\Form The form
       */
      private function createDeleteForm($id)
      {
          return $this-&gt;createFormBuilder(array('id' =&gt; $id))
              -&gt;add('id', 'hidden')
              -&gt;getForm()
          ;
      }
  }
  </code></pre>