title: Symfony2 and Doctrine, Column cannot be null with OneToOne relationships
body: <p>Here is an entity (<strong>EDITED: full file content</strong>)</p>
  
  <pre><code>// Eve\MoonBundle\Entity\MoonMaterial.php
  
  namespace Eve\MoonBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  //use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Table(name="_moon_material")
   * @ORM\Entity()
   */
  class MoonMaterial
  {
  
      public function __construct()
      {
          //$this-&gt;invTypes_byTypeID = new ArrayCollection();
      }
  
      // relations start
  
      /**
       * @ORM\OneToOne(targetEntity="Eve\DumpBundle\Entity\invTypes")
       * @ORM\JoinColumn(name="typeID", referencedColumnName="typeID")
       */
      private $invTypes_byTypeID;
  
      public function get_invTypes_byTypeID()
      {
          return $this-&gt;invTypes_byTypeID;
      }
  
      // relations end
  
      /**
       * @ORM\Column(name="userID", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $userID;
  
      /**
       * @ORM\Column(name="moonID", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $moonID;
  
      /**
       * @ORM\Column(name="typeID", type="integer", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $typeID;
  
      /**
       * Set userID
       *
       * @param integer $userID
       * @return MoonMaterial
       */
      public function setUserID($userID)
      {
          $this-&gt;userID = $userID;
  
          return $this;
      }
  
      /**
       * Get userID
       *
       * @return integer
       */
      public function getUserID()
      {
          return $this-&gt;userID;
      }
  
      /**
       * Set moonID
       *
       * @param integer $moonID
       * @return MoonMaterial
       */
      public function setMoonID($moonID)
      {
          $this-&gt;moonID = $moonID;
  
          return $this;
      }
  
      /**
       * Get moonID
       *
       * @return integer
       */
      public function getMoonID()
      {
          return $this-&gt;moonID;
      }
  
      /**
       * Set typeID
       *
       * @param integer $typeID
       * @return MoonMaterial
       */
      public function setTypeID($typeID)
      {
          $this-&gt;typeID = $typeID;
  
          return $this;
      }
  
      /**
       * Get typeID
       *
       * @return integer
       */
      public function getTypeID()
      {
          return $this-&gt;typeID;
      }
  
  }
  </code></pre>
  
  <p>code in controller (<strong>EDITED</strong>)</p>
  
  <pre><code>// Eve\MoonBundle\Controller\IndexController.php
  namespace Eve\MoonBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Eve\MoonBundle\Entity\MoonMaterial;
  
  class IndexController extends Controller
  {
  
      public function defaultAction($moonName)
      {
          // ...
          $dc = $this-&gt;getDoctrine();
          $form = $this
              -&gt;createFormBuilder()
              -&gt;add('typeID', 'choice', $choiceSettings)
              -&gt;getForm();
  
          if ($this-&gt;getRequest()-&gt;isMethod('post'))
          {
              $form-&gt;bind($this-&gt;getRequest());
              {
                  $data = $form-&gt;getData();
                  $typeID = $data['typeID'];
  
                  if (is_int($typeID))
                  {
                      $em = $dc-&gt;getEntityManager();
                      $mm = $em-&gt;getRepository('EveMoonBundle:MoonMaterial');
  
                      $result = $mm-&gt;findOneBy(array(
                              'userID' =&gt; $this-&gt;getUser()-&gt;getID(),
                              'typeID' =&gt; $typeID,
                              'moonID' =&gt; $moon-&gt;getItemID()));
  
                      if ($result)
                      {
                          $em-&gt;remove($result);
                          $em-&gt;flush();
                      }
  
                      $moonMaterial = new MoonMaterial();
                      $moonMaterial
                          -&gt;setUserID($this-&gt;getUser()-&gt;getID())
                          -&gt;setTypeID($typeID)
                          -&gt;setMoonID($moon-&gt;getItemID());
  
  
                      $em-&gt;persist($moonMaterial);
                      $em-&gt;flush();
                  }
              }
          }
  
          $twig = 'EveMoonBundle:Index:default.html.twig';
          return $this-&gt;render($twig, array(
                  'formAddMoonMaterial' =&gt; $form-&gt;createView()));
      }
  
  }
  </code></pre>
  
  <p>when i try this, i get error</p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO _moon_material (userID, moonID, typeID) VALUES (?, ?, ?)' with params {"1":38,"2":40001583,"3":null}:
  
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'typeID' cannot be null 
  </code></pre>
  
  <p>but when i comment the OneToOne (code above) relationship it work correctly, so main trouble is in description OneToOne relationship... pls help to deal with it</p>
  
  <p>what i want, i want rewrite entry if exist in table "_moon_material" or simply write it if not</p>
  
  <p>ps: i need that relation only for "read" (join name by id)</p>