title: Symfony / Doctrine: OneToMany insert results in null id
body: <p>I have two entities that I'm trying to apply a OneToMany / ManyToOne relationship to (one Game has many GameContent).</p>
  
  <p><strong>Game</strong></p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="GameContent", mappedBy="game")
   */
  private $contents;
  
  public function __construct()
  {
      $this-&gt;contents = new ArrayCollection();
  }
  
  public function getContents()
  {
      return $this-&gt;contents;
  }
  </code></pre>
  
  <p><strong>GameContent</strong></p>
  
  <pre><code>/**
   * @ORM\ManyToOne(targetEntity="Game", inversedBy="contents")
   */
  private $game;
  </code></pre>
  
  <p>And the following code inserts both records into their respective tables:</p>
  
  <pre><code>$game = $form-&gt;getData();
  $content = new GameContent();
  $content-&gt;setType('some type');
  $game-&gt;getContents()-&gt;add($content);
  
  $em = $this-&gt;getDoctrine()-&gt;getManager();
  $em-&gt;persist($content);
  $em-&gt;persist($game);
  $em-&gt;flush();
  </code></pre>
  
  <p>However, the GameContent's <code>game_id</code> is inserted as <code>null</code>:</p>
  
  <pre><code>INSERT INTO game_content (type, game_id) VALUES (?, ?)
  Parameters: { 1: 'some type', 2: null }
  </code></pre>
  
  <p>I've also tried:</p>
  
  <ul>
  <li>changing the order of <code>persist()</code></li>
  <li>replacing <code>$game-&gt;getContents()-&gt;add($content)</code> with <code>$game-&gt;addContents($content)</code> by doing <code>$this-&gt;contents[] = $content;</code></li>
  <li>removing <code>persist($content)</code> and having <code>cascade={"persist"}</code> on the Game entity.</li>
  </ul>
  
  <p>Why is <code>game_id</code> being inserted as null?</p>
  
  <hr>
  
  <p>My current workaround is:</p>
  
  <pre><code>$em = $this-&gt;getDoctrine()-&gt;getManager();
  
  $game = $form-&gt;getData();
  $em-&gt;persist($game);
  
  $content = new GameContent();
  $content-&gt;setType('some type');
  $content-&gt;setGame($game);
  $em-&gt;persist($content);
  
  $em-&gt;flush();
  </code></pre>