title: Symfony2, doctrine ManyToOne relationships error
body: <p>I am trying to build a log of some action performed on some site using Symfony2 and Doctrine. I have 2 tables Sites and Logs. The Logs table will contain a siteid which is a foreign key to the id column of sites table. The Logs table can have multiple logs for same site.</p>
  
  <p>When I try to insert an entry in the log table I get siteid is null error.</p>
  
  <p>Here is my code:</p>
  
  <p>Sites Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace A\SHB\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Sites
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="A\SHB\Entity\SitesRepository")
   */
  class Sites
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var ArrayCollection $siteLog
       *
       * @ORM\OneToMany(targetEntity="Logs", mappedBy="log", cascade={"persist"})
       * @ORM\OrderBy({"siteid" = "ASC"})
       * @ORM\JoinColumns({
       *   @ORM\JoinColumn(name="log_id", referencedColumnName="siteid")
       * })
       */
      private $siteLog;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;siteLog = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Add siteLog
       *
       * @param \A\SHB\Entity\SiteLog $siteLog
       * @return Sites
       */
      public function addSiteLog(\A\SHB\Entity\SiteLog $siteLog)
      {
          $this-&gt;siteLog[] = $siteLog;
  
          return $this;
      }
  
      /**
       * Remove siteLog
       *
       * @param \A\SHB\Entity\SiteLog $siteLog
       */
      public function removeSiteLog(\A\SHB\Entity\SiteLog $siteLog)
      {
          $this-&gt;siteLog-&gt;removeElement($siteLog);
      }
  
      /**
       * Get siteLog
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getSiteLog()
      {
          return $this-&gt;siteLog;
      }
  }
  </code></pre>
  
  <p>Logs Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace A\SHB\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Logs
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="A\SHB\Entity\LogsRepository")
   */
  class Logs
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="siteid", type="integer")
       */
      private $siteid;
  
      /**
       * @var integer
       *
       * @ORM\Column(name="dateline", type="integer")
       */
      private $dateline;
  
      /**
       * @var Log
       *
       * @ORM\ManyToOne(targetEntity="Sites")
       * @ORM\JoinColumns({
       *   @ORM\JoinColumn(name="site_id", referencedColumnName="id")
       * })
       */
      private $log;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set siteid
       *
       * @param integer $siteid
       * @return Logs
       */
      public function setSiteid($siteid)
      {
          $this-&gt;siteid = $siteid;
  
          return $this;
      }
  
      /**
       * Get siteid
       *
       * @return integer 
       */
      public function getSiteid()
      {
          return $this-&gt;siteid;
      }
  
      /**
       * Set dateline
       *
       * @param integer $dateline
       * @return Logs
       */
      public function setDateline($dateline)
      {
          $this-&gt;dateline = $dateline;
  
          return $this;
      }
  
      /**
       * Get dateline
       *
       * @return integer 
       */
      public function getDateline()
      {
          return $this-&gt;dateline;
      }
  
      /**
       * Set log
       *
       * @param \A\SHB\Entity\Log $log
       * @return Logs
       */
      public function setLog(\A\SHB\Entity\Log $log = null)
      {
          $this-&gt;log = $log;
  
          return $this;
      }
  
      /**
       * Get log
       *
       * @return \A\SHB\Entity\Log 
       */
      public function getLog()
      {
          return $this-&gt;log;
      }
  }
  </code></pre>
  
  <p>Controller  :</p>
  
  <pre><code>public function indexAction()
  {
      $sites = $this-&gt;getDoctrine()-&gt;getRepository('ASHB:Sites')-&gt;findAll();
  
      foreach ($sites as $site)
      {
          $host = $site-&gt;getForum();
          // Do something ....
  
          $log = new Logs();
          $log-&gt;setSiteid($site-&gt;getId());
          $log-&gt;setDateline($temp['dateline']);
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($log);
          $em-&gt;flush();
      }
      return $this-&gt;render('ASHB:Default:index.html.twig', array('sites' =&gt; $output, 'counters' =&gt; $counters));
  }
  </code></pre>
  
  <p>Now when I run this code, I get the following error:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'siteid' cannot be null"
  </code></pre>
  
  <p>If I var_dump <code>$log</code>, before <code>$em-&gt;persist($log);</code>, the siteid is there. I am not sure what is wrong and why the siteid is getting set to null.</p>
  
  <p><strong>Update 1:</strong> 
  I tried to make the following changes and still get the same error:</p>
  
  <pre><code>/**
   * @var Log
   *
   * @ORM\ManyToOne(targetEntity="Sites", inversedBy="siteLog")
   */
  private $log;
  </code></pre>