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