title: How can I check if a doctrine entity has a oneToOne mapped entity?
body: <p>I use doctrine, <code>User</code> and <code>Info</code> entities are <code>OneToOne</code> to each other, When I call user entity, first I want to check if that entity has a info or not, if has it, update it, if doesn't have it, create a info entity for that user.</p>
  
  <p>I have this hasser method in my User entity:</p>
  
  <pre><code>public function hasInfo() {  
         return $this-&gt;info instanceof Info;
  }
  </code></pre>
  
  <p>Then in my script I have:</p>
  
  <pre><code>$user = $em-&gt;find(User.....);
  if ($user-&gt;hasInfo()) {
      $info = $user-&gt;getInfo();
      $something = $info-&gt;getSomething();
  } else {
      $info = new Info;
      $info-&gt;setUser($user);
  } 
  </code></pre>
  
  <p>But I still get entity <code>Info</code> not found error. I did a print debug and noticed it never goes to else clause even if it doesn't have info so I get entity <code>Info</code> not found error. What wrong I did in my script above?</p>
  
  <p>This one works neither:</p>
  
  <pre><code>$user = $em-&gt;find(User...);
  $Info = $user-&gt;getInfo();
  if ($info !== null) {
  ...
  </code></pre>
  
  <p>It always assumes <code>User</code> has a <code>Info</code> and never goes to else clause. There is no such problem for other mappings than <code>OneToOne</code>.</p>
  
  <p>EDIT1: I guess this should because that both entities are mapped by identifiers of each other (as they are one to one) rather than the owning side having a FK? Should be that the issue?</p>
  
  <p>EDIT2: Here are my mappings, I don't use annotation but php mapping:</p>
  
  <p>User entity:</p>
  
  <pre><code>$metadata-&gt;mapOneToOne(array(
      'fieldName' =&gt; 'info',
      'targetEntity' =&gt; 'Info',
      'mappedBy' =&gt; 'user',
      'cascade' =&gt; array( 
          0 =&gt; 'remove', 
          1 =&gt; 'persist', 
      ),
      'joinColumns' =&gt; array( 
          0 =&gt; array( 
              'name' =&gt; 'id',
              'referencedColumnName' =&gt; 'id',
              'nullable' =&gt; true,
              'onDelete' =&gt; 'cascade',
              'columnDefinition' =&gt; NULL,
          ), 
      )
  ));
  </code></pre>
  
  <p>Info entity:</p>
  
  <pre><code>$metadata-&gt;mapOneToOne(array(
      'fieldName' =&gt; 'user',
      'targetEntity' =&gt; 'User',
      'inversedBy' =&gt; 'info',
      'joinColumns' =&gt; array( 
          0 =&gt; array( 
              'name' =&gt; 'id',
              'referencedColumnName' =&gt; 'id',
              'nullable' =&gt; true,
              'columnDefinition' =&gt; NULL,
          )
      )
  ));
  </code></pre>
  
  <p>As they are oneToOne, the PK of info entity is PK of user entity too.</p>
  
  <p>with an empty Info table. it never goes to else clause.</p>
  
  <pre><code>// search by identifier:
  $info = $em-&gt;find("Info", 1);
  if ($info !== null) {
      print("yes");
  } else  {
      print("no");
  }
  </code></pre>
  
  <p>But if I use findOneBy it works fine! what is the problem?</p>
  
  <pre><code>$info = $em-&gt;getRepository('Info')-&gt;findOneBy(array('id' =&gt; 1));
  </code></pre>
  
  <p>why cannot I use find() as this is just identifier?</p>
  
  <p>Here is my identifer definition:</p>
  
  <pre><code>$metadata-&gt;mapField(array(   'fieldName' =&gt; 'id',
    'columnName' =&gt; 'id',
    'type' =&gt; 'integer',
    'nullable' =&gt; false,
    'options' =&gt; 
    array(
         'unsigned' =&gt; true,
    ),
    'id' =&gt; true,
  ));
  </code></pre>