title: Symfony/Doctrine inserts null as foreign key in many to one relationship
body: <p>I have two entities, <code>League</code> and <code>Club</code>, and predictably a <code>League</code> can have many <code>Club</code>s. Just to test things out I'm trying to insert a club called <code>test</code> into the league with an ID of 1 (which does exist in the database, that's not the problem). If I do the following then it works fine:</p>
  
  <pre><code>$club = new Club();
  $club-&gt;setName("test");
  $league = $this-&gt;getDoctrine()-&gt;getRepository("AppBundle:League")-&gt;find(1);
  $club-&gt;setLeague($league);
  $em-&gt;persist($club);
  $em-&gt;persist($league);
  $em-&gt;flush();
  </code></pre>
  
  <p>However, if I alter the fourth line to <code>$league-&gt;addClub($club);</code> then it inserts a null value as the <code>league_id</code> in the database.</p>
  
  <p>I have the following code for the <code>Club</code> entity:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Club
   *
   * @ORM\Table(name="club")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\ClubRepository")
   */
  class Club
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\ManyToOne(targetEntity="League", inversedBy="clubs")
       * @ORM\JoinColumn(name="league_id", referencedColumnName="id")
       */
      private $league;
  
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       *
       * @return Club
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set league
       *
       * @param \AppBundle\Entity\League $league
       *
       * @return Club
       */
      public function setLeague(\AppBundle\Entity\League $league = null)
      {
          $this-&gt;league = $league;
  
          return $this;
      }
  
      /**
       * Get league
       *
       * @return \AppBundle\Entity\League
       */
      public function getLeague()
      {
          return $this-&gt;league;
      }
  }
  </code></pre>
  
  <p>and the following for the <code>League</code> entity:
      
  
  <pre><code>namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * League
   *
   * @ORM\Table(name="league")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\LeagueRepository")
   */
  class League
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\OneToMany(targetEntity="Club", mappedBy="league")
       */
      private $clubs;
  
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       *
       * @return League
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string
       */
      public function getName()
      {
          return $this-&gt;name;
      }
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;clubs = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Add club
       *
       * @param \AppBundle\Entity\Club $club
       *
       * @return League
       */
      public function addClub(\AppBundle\Entity\Club $club)
      {
          $this-&gt;clubs[] = $club;
  
          return $this;
      }
  
      /**
       * Remove club
       *
       * @param \AppBundle\Entity\Club $club
       */
      public function removeClub(\AppBundle\Entity\Club $club)
      {
          $this-&gt;clubs-&gt;removeElement($club);
      }
  
      /**
       * Get clubs
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getClubs()
      {
          return $this-&gt;clubs;
      }
  }
  </code></pre>
  
  <p>I've checked that the database schema is up to data with <code>doctrine:schema:update</code>, and I've tried seemingly every combination of persisting and flushing... persisting the club before the league, persisting the league before the club, persisting just the league, persisting just the club, persisting the league then flushing then persisting the club then flushing again... Nothing has worked.</p>