title: Doctrine: Why $entity->ManyToOneRelation->getId() does fire a query to database?
body: <p>I have a User class with referrer field:</p>
  
  <pre><code>class User {
      /**
       * @ORM\ManyToOne(targetEntity="User", cascade={"detach"})
       * @ORM\JoinColumn(name="referrer", referencedColumnName="id")
       */
      protected $referrer = null;
  
      public function getReferer(){
          return $this-&gt;referrer;
      }
  }
  </code></pre>
  
  <p>What have I read is that since Doctrine 2.2 when I call <code>$user-&gt;getReferer()-&gt;getId()</code> I should get referenced id without doctrine firing additional query.</p>
  
  <p>But in my case, doctrine is fetching whole entity from database.
  What am I doing wrong?</p>
  
  <p>I use symfony 2.7.1 and I run echo <code>\Doctrine\ORM\Version::VERSION</code> to check doctrine's version.</p>
  
  <p>(<code>$user-&gt;getReferrer()</code> isn't firing any additional query yet.)</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>How I test it:</p>
  
  <pre><code>$user = $this-&gt;getUser();
  $referrer = $user-&gt;getReferrer();
  //Until there one query is executed by security(?) to fetch authenticated user.
  //Referrer is not yet initialized.
  
  $this-&gt;addFlash('info', 'Referrer id: '.$referrer-&gt;getId());
  //Now second query was fired. And $refferer is now initialized.
  
  return array('data' =&gt; $referrer); //return $refferer object for twig dump.
  </code></pre>
  
  <p>Twig dumps something like this for code without <code>getId()</code>:</p>
  
  <pre><code>User {#1184 ▼
      +__isInitialized__: false
      #id: "2"
      #referrer: null
  }
  </code></pre>
  
  <p>And this for code with <code>getId()</code> used on <code>$referrer</code>:</p>
  
  <pre><code>User {#1184 ▼
      +__isInitialized__: true
      #id: 2      
      #referrer: null
  }
  </code></pre>