title: Symfony2 Doctrine2 trouble with optional one to one relation
body: <p>I have a problem with Doctrine2 in Symfony2 and two relationed entities.</p>
  
  <p>There is a user-entity that <em>can</em> (not must) have a usermeta-entity referenced which contains information like biography etc.</p>
  
  <p>The usermeta is optional because user is imported by another system, while usermeta is managed in my application.</p>
  
  <p>Of course I want to save both together, so that saving a user must create or update a usermeta-entity.</p>
  
  <p>Both are joined by a column named aduserid (same name in both tables).</p>
  
  <p>I've recognized that if usermeta is an optional reference the owning-side in this case should be usermeta, otherwise doctrine loads user and needs the usermeta entity - but it's not always there.</p>
  
  <p>Please note the comments in User->setMeta..</p>
  
  <pre><code>/**
   * User
   *
   * @ORM\Table(name="user")
   * @ORM\Entity
   */
  class User
  {
  /**
   * @var Usermeta
   * @ORM\OneToOne(targetEntity="Usermeta", mappedBy="user", cascade={"persist"})
   */
  protected $meta;
  
  public function getMeta()
  {
      return $this-&gt;meta;
  }
  
  /**
   * 
   * @param Usermeta $metaValue 
   */
  public function setMeta($metaValue)
  {        
  // I've tried setting the join-column-value here 
  //  - but it's not getting persisted
  // $metaValue-&gt;setAduserid($this-&gt;getAduserid());
  
  // Then I've tried to set the user-object in Usermeta - but then 
  //  it seems like Doctrine wants to update Usermeta and searches
  //  for ValId names aduserid (in BasicEntityPersister-&gt;_prepareUpdateData) 
  //  but only id is given -  so not luck here
  // $metaValue-&gt;setUser($this);           
  
      $this-&gt;meta = $metaValue;
  }
  
  /**
   * @var integer
   *
   * @ORM\Column(name="rowid", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="IDENTITY")
   */
  private $id;
  
  
  /**
   * Get rowid
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * @var integer
   *
   * @ORM\Column(name="ADuserid", type="integer", nullable=false)
   */
  private $aduserid;
  
  /**
   * Set aduserid
   *
   * @param integer $aduserid
   * @return User
   */
  public function setAduserid($aduserid)
  {
      $this-&gt;aduserid = $aduserid;
  
      return $this;
  }
  
  /**
   * Get aduserid
   *
   * @return integer 
   */
  public function getAduserid()
  {
      return $this-&gt;aduserid;
  }
  
  // some mor fields.... 
  }
  </code></pre>
  
  <p>And the Usermeta class:</p>
  
  <pre><code>/**
   * Usermeta
   *
   * @ORM\Table(name="userMeta")
   * @ORM\Entity
   */
  class Usermeta
  {
  /**
   * @ORM\OneToOne(targetEntity="User", inversedBy="meta")
   * @ORM\JoinColumn(name="ADuserid", referencedColumnName="ADuserid")
   */
  protected $user;
  
  public function getUser()
  {
      return $this-&gt;$user;
  }    
  
  public function setUser($userObj)
  {
      $this-&gt;user = $userObj;
  }
  
  /**
   * @var integer
   *
   * @ORM\Column(name="id", type="integer", nullable=false)
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="IDENTITY")
   */
  private $id;
  
  /**
   * @var integer
   *
   * @ORM\Column(name="ADuserid", type="integer", nullable=false)
   */
  private $aduserid;
  
  /**
   * Set aduserid
   *
   * @param integer $aduserid
   * @return User
   */
  public function setAduserid($aduserid)
  {
      $this-&gt;aduserid = $aduserid;
  
      return $this;
  }
  
  /**
   * Get aduserid
   *
   * @return integer 
   */
  public function getAduserid()
  {
      return $this-&gt;aduserid;
  }
  }
  </code></pre>
  
  <p>the controller code looks like this:</p>
  
  <pre><code>...
  
  $userForm-&gt;bind($request);
  
      if($userForm-&gt;isValid()) {
          $em-&gt;persist($user);
          $em-&gt;flush();
      }
  ...
  </code></pre>