title: Symfony2 & Doctrine : One-To-One relation and persistence of the id references
body: <p>I have a problem with establishing a bidirectional One-To-One relationship between two entities.
  I have an entity "Campaign" that CAN reference another entity "DonationData". A donation data that is created is always linked to a campaign. I want a bidirectional relationship because I want to be able to find the related campaign from a DonationData entity.</p>
  
  <p>This is my code for the Campaign Entity :</p>
  
  <pre><code>&lt;?php
  /**
   * @ORM\Entity
   * ...
   */
  class Campaign
  {
  ...
  /**
   * @ORM\OneToOne(targetEntity="DonationData", mappedBy="campaign", cascade={"persist", "remove"})
   */
  protected $donationData;
  
  ...
  
  /**
   * Set donationData
   *
   * @param DonationData $donationData
   * @return Campaign
   */
  public function setDonationData(DonationData $donationData = null)
  {
      $this-&gt;donationData = $donationData;
  
      return $this;
  }
  
  ...
  
  ?&gt;
  </code></pre>
  
  <p>And the related code for my DonationData entity :</p>
  
  <pre><code>&lt;?php
  /**
   * @ORM\Entity
   * ...
   */
  class DonationData
  {
  ...
  /**
   * @ORM\OneToOne(targetEntity="Campaign")
   * @ORM\JoinColumns({
   *     @ORM\JoinColumn(name="campaign_id", referencedColumnName="id")
   * })
   */
  protected $campaign;
  
  ...
  
  /**
   * Set campaign
   * @param Campaign $campaign
   *
   * @return DonationData
   */
  public function setCampaign($campaign)
  {
      $this-&gt;campaign = $campaign;
  
      return $this;
  }
  
  
  ...
  ?&gt;
  </code></pre>
  
  <p>The piece of code where I'm adding my DonationData form in my Campaign Form (CampaignType.php)</p>
  
  <pre><code>&lt;?php
  ...
  -&gt;add('donationData', new DonationDataType(), array(
      'label' =&gt; false
  ))
  ...
  ?&gt;
  </code></pre>
  
  <p>And in the CampaignController.php side, when I'm handling the creation operations, I didn't change anything : I'm just persisting the related entity binded from the request, and then flushing the Entity Manager :</p>
  
  <pre><code>&lt;?php
  ...
  $em-&gt;persist($campaign);
  $em-&gt;flush();
  ...
  ?&gt;
  </code></pre>
  
  <p>My problem is when I want to persist a Campaign entity form that has an embedded DonationData form inside. I successfully persist both the Campaign and the DonationData entities, but there is only the donation_data_id reference in the Campaign entry that is persisted. When I look in database to the persisted DonationData, the campaign_id is always set to NULL.</p>
  
  <p>Do you see any explanation for that?</p>
  
  <p>Thank you.</p>