title: symfony2 doctrine exception insert
body: <p>In symfony2 when I insert data into to database I receive an exception:  </p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO practice_user (user_id, practice_id, practice_text, type_id) VALUES (?, ?, ?, ?)' with params [null, "1", "test", 2]:
  
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null
  </code></pre>
  
  <p>PracticeUser.php:</p>
  
  <pre><code>&lt;?php
  
  namespace fl\PracticeBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="practice_user")
   */
  class PracticeUser
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $submit_practice_id;
  
      /**
       * @ORM\Column(type="integer")
       */
      protected $user_id;
  
      /**
       * @ORM\Column(type="integer")
       */
      protected $practice_id;
  
      /**
       * @ORM\Column(type="text")
       */
      protected $practice_text;
  
      /**
       * @ORM\Column(type="integer")
       */
      protected $type_id;
  
      //many-to-one fos_user
      /**     
       * @ORM\ManyToOne(targetEntity="fl\UserBundle\Entity\User", inversedBy="practices")
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)) 
       */
      protected $user;
  
      /**
       * Get submit_practice_id
       *
       * @return integer 
       */
      public function getSubmitPracticeId()
      {
          return $this-&gt;submit_practice_id;
      }
  
      /**
       * Set user_id
       *
       * @param integer $userId
       * @return PracticeUser
       */
      public function setUserId($userId)
      {
          $this-&gt;user_id = $userId;
  
          return $this;
      }
  
      /**
       * Get user_id
       *
       * @return integer 
       */
      public function getUserId()
      {
          return $this-&gt;user_id;
      }
  
      /**
       * Set practice_id
       *
       * @param integer $practiceId
       * @return PracticeUser
       */
      public function setPracticeId($practiceId)
      {
          $this-&gt;practice_id = $practiceId;
  
          return $this;
      }
  
      /**
       * Get practice_id
       *
       * @return integer 
       */
      public function getPracticeId()
      {
          return $this-&gt;practice_id;
      }
  
      /**
       * Set practice_text
       *
       * @param string $practiceText
       * @return PracticeUser
       */
      public function setPracticeText($practiceText)
      {
          $this-&gt;practice_text = $practiceText;
  
          return $this;
      }
  
      /**
       * Get practice_text
       *
       * @return string 
       */
      public function getPracticeText()
      {
          return $this-&gt;practice_text;
      }
  
      /**
       * Set type_id
       *
       * @param integer $typeId
       * @return PracticeUser
       */
      public function setTypeId($typeId)
      {
          $this-&gt;type_id = $typeId;
  
          return $this;
      }
  
      /**
       * Get type_id
       *
       * @return integer 
       */
      public function getTypeId()
      {
          return $this-&gt;type_id;
      }
  
      /**
       * Set user
       *
       * @param \fl\UserBundle\Entity\User $user
       * @return PracticeUser
       */
      public function setUser(\fl\UserBundle\Entity\User $user)
      {
          $this-&gt;user = $user;
  
          return $this;
      }
  
      /**
       * Get user
       *
       * @return \fl\UserBundle\Entity\User 
       */
      public function getUser()
      {
          return $this-&gt;user;
      }
  }
  </code></pre>
  
  <p>User.php </p>
  
  <pre><code>&lt;?php
  
  namespace fl\UserBundle\Entity;
  
  use FOS\UserBundle\Entity\User as BaseUser;
  use Doctrine\ORM\Mapping as ORM;
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="fos_user")
   */
  class User extends BaseUser
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      //one-to-one user_detail
      /**
       * @ORM\OneToOne(targetEntity="fl\UserBundle\Entity\UserDetail", mappedBy="user") 
       */
      protected $user_detail; 
  
      //one-to-one account
      /**
       * @ORM\OneToOne(targetEntity="fl\UserBundle\Entity\Account", mappedBy="user") 
       */
       protected $account;
  
       //one-to-many language_user
      /**
       * @ORM\OneToMany(targetEntity="fl\UserBundle\Entity\LanguageUser", mappedBy="user") 
       */
      protected $languageusers;
  
      //one-to-many practice_user
      /**
       * @ORM\OneToMany(targetEntity="fl\PracticeBundle\Entity\PracticeUser", mappedBy="user")
       */
      protected $practices;
  
      //one-to-many practice_review
      /**
       * @ORM\OneToMany(targetEntity="fl\PracticeBundle\Entity\PracticeReview", mappedBy="user") 
       */
      protected $reviews;
  
      public function __construct()
      {
          parent::__construct();
          // your own logic
  
          //one to many with language_user table
          $this-&gt;languageusers = new ArrayCollection();
  
          //one-to-many practice_user
          $this-&gt;practices = new ArrayCollection();
  
          //one-to-many practice_review
          $this-&gt;reviews = new ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set user_detail
       *
       * @param \fl\UserBundle\Entity\UserDetail $userDetail
       * @return User
       */
      public function setUserDetail(\fl\UserBundle\Entity\UserDetail $userDetail = null)
      {
          $this-&gt;user_detail = $userDetail;
  
          return $this;
      }
  
      /**
       * Get user_detail
       *
       * @return \fl\UserBundle\Entity\UserDetail 
       */
      public function getUserDetail()
      {
          return $this-&gt;user_detail;
      }
  
      /**
       * Set account
       *
       * @param \fl\UserBundle\Entity\Account $account
       * @return User
       */
      public function setAccount(\fl\UserBundle\Entity\Account $account = null)
      {
          $this-&gt;account = $account;
  
          return $this;
      }
  
      /**
       * Get account
       *
       * @return \fl\UserBundle\Entity\Account 
       */
      public function getAccount()
      {
          return $this-&gt;account;
      }
  
      /**
       * Add languageusers
       *
       * @param \fl\UserBundle\Entity\LanguageUser $languageusers
       * @return User
       */
      public function addLanguageuser(\fl\UserBundle\Entity\LanguageUser $languageusers)
      {
          $this-&gt;languageusers[] = $languageusers;
  
          return $this;
      }
  
      /**
       * Remove languageusers
       *
       * @param \fl\UserBundle\Entity\LanguageUser $languageusers
       */
      public function removeLanguageuser(\fl\UserBundle\Entity\LanguageUser $languageusers)
      {
          $this-&gt;languageusers-&gt;removeElement($languageusers);
      }
  
      /**
       * Get languageusers
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getLanguageusers()
      {
          return $this-&gt;languageusers;
      }
  
      /**
       * Add practices
       *
       * @param \fl\PracticeBundle\Entity\PracticeUser $practices
       * @return User
       */
      public function addPractice(\fl\PracticeBundle\Entity\PracticeUser $practices)
      {
          $this-&gt;practices[] = $practices;
  
          return $this;
      }
  
      /**
       * Remove practices
       *
       * @param \fl\PracticeBundle\Entity\PracticeUser $practices
       */
      public function removePractice(\fl\PracticeBundle\Entity\PracticeUser $practices)
      {
          $this-&gt;practices-&gt;removeElement($practices);
      }
  
      /**
       * Get practices
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getPractices()
      {
          return $this-&gt;practices;
      }
  
      /**
       * Add reviews
       *
       * @param \fl\PracticeBundle\Entity\PracticeReview $reviews
       * @return User
       */
      public function addReview(\fl\PracticeBundle\Entity\PracticeReview $reviews)
      {
          $this-&gt;reviews[] = $reviews;
  
          return $this;
      }
  
      /**
       * Remove reviews
       *
       * @param \fl\PracticeBundle\Entity\PracticeReview $reviews
       */
      public function removeReview(\fl\PracticeBundle\Entity\PracticeReview $reviews)
      {
          $this-&gt;reviews-&gt;removeElement($reviews);
      }
  
      /**
       * Get reviews
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getReviews()
      {
          return $this-&gt;reviews;
      }
  }
  </code></pre>
  
  <p>code when I try insert to the DataBase:  </p>
  
  <pre><code>   $user_id = $user-&gt;getId();
     $submit_dictation = new PracticeUser();
     $submit_dictation-&gt;setUserId($user_id);
     $submit_dictation-&gt;setPracticeId($dictation_id);
     $submit_dictation-&gt;setPracticeText($get_user_answer);
     $submit_dictation-&gt;setTypeId(self::PRACTICE_DICTATION);
  
     $em = $this-&gt;getDoctrine()-&gt;getManager();
     $em-&gt;persist($submit_dictation);
     $em-&gt;flush();
  </code></pre>
  
  <p>I also checked(with <code>var_dump</code>)the variable <code>$user_id</code> but is not null, I receive the user id.  </p>
  
  <p>Thanks in advance</p>