title: Symfony persists fail with relation cascade
body: <p>Hello i have this error when i try to persist my entity :</p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO item_details (item_id, item_type,    name, start_date, end_date, description, pre_requisites, picture, acquired_knowledges,   status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, "ertret",   "09\/01\/2014", "17\/01\/2014", "ter", "trtre", {}, "trtre", null]:
  
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'item_id' cannot be null
  </code></pre>
  
  <p><strong>Course entity code :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Mooc\AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Course
   *
   * @ORM\Table(name="course")
   * @ORM\Entity
   */
  class Course
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="language_id", type="integer", nullable=false)
       */
      private $languageId;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="category_id", type="integer", nullable=false)
       */
      private $categoryId;
  
      /**
       * @ORM\OneToMany(targetEntity="ItemDetails", mappedBy="course",cascade={"all"})
       **/
  
      private $details;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;details = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set languageId
       *
       * @param integer $languageId
       * @return Course
       */
      public function setLanguageId($languageId)
      {
          $this-&gt;languageId = $languageId;
  
          return $this;
      }
  
      /**
       * Get languageId
       *
       * @return integer 
       */
      public function getLanguageId()
      {
          return $this-&gt;languageId;
      }
  
      /**
       * Set categoryId
       *
       * @param integer $categoryId
       * @return Course
       */
      public function setCategoryId($categoryId)
      {
          $this-&gt;categoryId = $categoryId;
  
          return $this;
      }
  
      /**
       * Get categoryId
       *
       * @return integer 
       */
      public function getCategoryId()
      {
          return $this-&gt;categoryId;
      }
  
      /**
       * Add details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
       * @return Course
       */
      public function addDetail(\Mooc\AdminBundle\Entity\ItemDetails $details)
      {
          $this-&gt;details[] = $details;
  
          return $this;
      }
  
      /**
       * Add details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
       * @return Course
       */
      public function setDetails(\Mooc\AdminBundle\Entity\ItemDetails $details)
      {
          $this-&gt;details[] = $details;
  
          return $this;
      }    
  
      /**
       * Remove details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $details
       */
      public function removeDetail(\Mooc\AdminBundle\Entity\ItemDetails $details)
      {
          $this-&gt;details-&gt;removeElement($details);
      }
  
      /**
       * Get details
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getDetails()
      {
          return $this-&gt;details;
      }
  }
  </code></pre>
  
  <p><strong>Item details entity code :</strong>
      
  
  <pre><code>namespace Mooc\AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * ItemDetails
   *
   * @ORM\Table(name="item_details")
   * @ORM\Entity
   */
  class ItemDetails
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="item_id", type="integer", nullable=false)
       */
      private $itemId;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="item_type", type="integer", nullable=false)
       */
      private $itemType;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255, nullable=false)
       */
      private $name;
  
      /**
       * @var string
       *
       * @ORM\Column(name="start_date", type="string", length=60, nullable=false)
       */
      private $startDate;
  
      /**
       * @var string
       *
       * @ORM\Column(name="end_date", type="string", length=60, nullable=false)
       */
      private $endDate;
  
      /**
       * @var string
       *
       * @ORM\Column(name="description", type="text", nullable=false)
       */
      private $description;
  
      /**
       * @var string
       *
       * @ORM\Column(name="pre_requisites", type="text", nullable=false)
       */
      private $preRequisites;
  
      /**
       * @var string
       *
       * @ORM\Column(name="picture", type="string", length=255, nullable=false)
       */
      private $picture;
  
      /**
       * @var string
       *
       * @ORM\Column(name="acquired_knowledges", type="text", nullable=false)
       */
      private $acquiredKnowledges;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="status", type="integer", nullable=false)
       */
      private $status;
  
      /**
       * @ORM\ManyToOne(targetEntity="Course", inversedBy="details")
       * @ORM\JoinColumn(name="item_id", referencedColumnName="id")
       **/
  
      private $course;
  
      /**
       * @ORM\OneToMany(targetEntity="Video", mappedBy="itemDetails",cascade={"all"})
       **/
  
      private $video;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;video = new \Doctrine\Common\Collections\ArrayCollection();
      }
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set itemId
       *
       * @param integer $itemId
       * @return ItemDetails
       */
      public function setItemId($itemId)
      {
          $this-&gt;itemId = $itemId;
  
          return $this;
      }
  
      /**
       * Get itemId
       *
       * @return integer 
       */
      public function getItemId()
      {
          return $this-&gt;itemId;
      }
  
      /**
       * Set itemType
       *
       * @param integer $itemType
       * @return ItemDetails
       */
      public function setItemType($itemType)
      {
          $this-&gt;itemType = $itemType;
  
          return $this;
      }
  
      /**
       * Get itemType
       *
       * @return integer 
       */
      public function getItemType()
      {
          return $this-&gt;itemType;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return ItemDetails
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set startDate
       *
       * @param string $startDate
       * @return ItemDetails
       */
      public function setStartDate($startDate)
      {
          $this-&gt;startDate = $startDate;
  
          return $this;
      }
  
      /**
       * Get startDate
       *
       * @return string 
       */
      public function getStartDate()
      {
          return $this-&gt;startDate;
      }
  
      /**
       * Set endDate
       *
       * @param string $endDate
       * @return ItemDetails
       */
      public function setEndDate($endDate)
      {
          $this-&gt;endDate = $endDate;
  
          return $this;
      }
  
      /**
       * Get endDate
       *
       * @return string 
       */
      public function getEndDate()
      {
          return $this-&gt;endDate;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return ItemDetails
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Set preRequisites
       *
       * @param string $preRequisites
       * @return ItemDetails
       */
      public function setPreRequisites($preRequisites)
      {
          $this-&gt;preRequisites = $preRequisites;
  
          return $this;
      }
  
      /**
       * Get preRequisites
       *
       * @return string 
       */
      public function getPreRequisites()
      {
          return $this-&gt;preRequisites;
      }
  
      /**
       * Set picture
       *
       * @param string $picture
       * @return ItemDetails
       */
      public function setPicture($picture)
      {
          $this-&gt;picture = $picture;
  
          return $this;
      }
  
      /**
       * Get picture
       *
       * @return string 
       */
      public function getPicture()
      {
          return $this-&gt;picture;
      }
  
      /**
       * Set acquiredKnowledges
       *
       * @param string $acquiredKnowledges
       * @return ItemDetails
       */
      public function setAcquiredKnowledges($acquiredKnowledges)
      {
          $this-&gt;acquiredKnowledges = $acquiredKnowledges;
  
          return $this;
      }
  
      /**
       * Get acquiredKnowledges
       *
       * @return string 
       */
      public function getAcquiredKnowledges()
      {
          return $this-&gt;acquiredKnowledges;
      }
  
      /**
       * Set status
       *
       * @param integer $status
       * @return ItemDetails
       */
      public function setStatus($status)
      {
          $this-&gt;status = $status;
  
          return $this;
      }
  
      /**
       * Get status
       *
       * @return integer 
       */
      public function getStatus()
      {
          return $this-&gt;status;
      }
  
  
  
      /**
       * Set course
       *
       * @param \Mooc\AdminBundle\Entity\Course $course
       * @return ItemDetails
       */
      public function setCourse(\Mooc\AdminBundle\Entity\Course $course = null)
      {
          $this-&gt;course = $course;
  
          return $this;
      }
  
      /**
       * Get course
       *
       * @return \Mooc\AdminBundle\Entity\Course 
       */
      public function getCourse()
      {
          return $this-&gt;course;
      }
  
      /**
       * Add details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
       * @return Course
       */
      public function addVideo(\Mooc\AdminBundle\Entity\Video $video)
      {
          $this-&gt;details[] = $video;
  
          return $this;
      }
  
      /**
       * Add details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
       * @return Course
       */
      public function setVideo(\Mooc\AdminBundle\Entity\Video $video)
      {
          $this-&gt;video[] = $video;
  
          return $this;
      }    
  
      /**
       * Remove details
       *
       * @param \Mooc\TeacherBundle\Entity\ItemDetails $video
       */
      public function removeVideo(\Mooc\AdminBundle\Entity\Video $video)
      {
          $this-&gt;video-&gt;removeElement($video);
      }
  
      /**
       * Get details
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getVideo()
      {
          return $this-&gt;video;
      }
  }
  </code></pre>
  
  <p><strong>Video entity code :</strong>
      
  
  <pre><code>namespace Mooc\AdminBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Video
   *
   * @ORM\Table(name="video")
   * @ORM\Entity
   */
  class Video
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="course_id", type="integer", nullable=false)
       */
      private $courseId;
  
      /**
       * @var string
       *
       * @ORM\Column(name="video", type="string", length=255, nullable=false)
       */
      private $video;
  
      /**
       * @var string
       *
       * @ORM\Column(name="description", type="text", nullable=false)
       */
      private $description;
  
      /**
       * @ORM\ManyToOne(targetEntity="ItemDetails")
       * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
       **/
  
      private $itemDetails;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set courseId
       *
       * @param integer $courseId
       * @return Video
       */
      public function setCourseId($courseId)
      {
          $this-&gt;courseId = $courseId;
  
          return $this;
      }
  
      /**
       * Get courseId
       *
       * @return integer 
       */
      public function getCourseId()
      {
          return $this-&gt;courseId;
      }
  
      /**
       * Set video
       *
       * @param string $video
       * @return Video
       */
      public function setVideo($video)
      {
          $this-&gt;video = $video;
  
          return $this;
      }
  
      /**
       * Get video
       *
       * @return string 
       */
      public function getVideo()
      {
          return $this-&gt;video;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Video
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Set itemDetails
       *
       * @param \Mooc\AdminBundle\Entity\ItemDetails $itemDetails
       * @return Video
       */
      public function setItemDetails(\Mooc\AdminBundle\Entity\ItemDetails $itemDetails = null)
      {
          $this-&gt;itemDetails = $itemDetails;
  
          return $this;
      }
  
      /**
       * Get itemDetails
       *
       * @return \Mooc\AdminBundle\Entity\ItemDetails 
       */
      public function getItemDetails()
      {
          return $this-&gt;itemDetails;
      }
  }
  </code></pre>
  
  <p><strong>Course form code :</strong>
      
  
  <pre><code>namespace Mooc\AdminBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  use Symfony\Component\HttpFoundation\Session\Session;
  use Doctrine\ORM\EntityRepository;
  
  class CourseType extends AbstractType
  {
      private $languageId;
  
      public function __construct($languageId) {
  
          $this-&gt;languageId=$languageId;
  
      }
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $session = new Session();
  
          $languageId=$session-&gt;get('languageId');              
          $builder
              -&gt;add('languageId', 'entity', array(
                  'class' =&gt; 'MoocAdminBundle:Language',
                  'property' =&gt; 'name',
                  'preferred_choices' =&gt; array((object)$languageId),
              ))
              -&gt;add('categoryId', 'entity', array(
                  'class' =&gt; 'MoocAdminBundle:CourseCategory',
                  'query_builder' =&gt; function(EntityRepository $er) {
                      return $er-&gt;createQueryBuilder('u')
                          -&gt;where('u.languageId ='.$this-&gt;languageId);
                  },
              ))
              -&gt;add('details', new ItemDetailsType())
              -&gt;add('submit', 'submit', array('attr' =&gt; array('class' =&gt; 'btn btn-success btn-large')));
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Mooc\AdminBundle\Entity\Course'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'mooc_adminbundle_course';
      }
  }
  </code></pre>
  
  <p>ItemDetails form :
      
  
  <pre><code>namespace Mooc\AdminBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class ItemDetailsType extends AbstractType
  {
  
  
          /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
              -&gt;add('startDate')
              -&gt;add('endDate')
              -&gt;add('description')
              -&gt;add('preRequisites')
              -&gt;add('picture','file')
              -&gt;add('acquiredKnowledges')
              -&gt;add('status')
              -&gt;add('video', new VideoType())
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Mooc\AdminBundle\Entity\ItemDetails'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'mooc_adminbundle_itemdetails';
      }
  }
  </code></pre>
  
  <p><strong>video  Form :</strong></p>
  
  <pre><code>&lt;?php
  
  namespace Mooc\AdminBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class VideoType extends AbstractType
  {
          /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('courseId')
              -&gt;add('video','file')
              -&gt;add('description','textarea')
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Mooc\AdminBundle\Entity\Video'
          ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'mooc_adminbundle_video';
      }
  }
  </code></pre>
  
  <p><strong>Code in controller :</strong></p>
  
  <pre><code>public function courseaddAction() {
  
      $locale=$this-&gt;get('session')-&gt;get('_locale');
  
      $language=$this-&gt;get('doctrine.orm.entity_manager')-&gt;getRepository('MoocAdminBundle:Language')-&gt;findBy(array('locale'=&gt;$locale));
      $languageId=$language[0]-&gt;getId();
  
      $form=$this-&gt;createForm(new CourseType($languageId));         
  
      if ($this-&gt;getRequest()-&gt;isMethod("POST")) {
          $form-&gt;HandleRequest($this-&gt;getRequest());    
          //if ($form-&gt;isValid()) {                
              $course = $form-&gt;getData();
              $this-&gt;get('mooc.course')-&gt;addCourse($course);                
          //}
  
      }
  
      return $this-&gt;render('MoocTeacherBundle:Default:courseadd.html.twig', array('form'=&gt;$form-&gt;createView(),'languageId'=&gt;$languageId));
  }
  </code></pre>
  
  <p><strong>Code in service :</strong> </p>
  
  <pre><code> public function addCourse($course) {
      $details=$course-&gt;getDetails();
  
      $this-&gt;em-&gt;persist($course);
      /*
      foreach($course-&gt;getDetails() as $details) {
          $details-&gt;setItemType('course');
          $this-&gt;em-&gt;persist($details);
  
           foreach($details-&gt;getVideo() as $video) {                
              $this-&gt;em-&gt;persist($video);
          }        
      }  
     */
      $this-&gt;em-&gt;flush();
  }
  </code></pre>