Avoid lazy loading Doctrine Symfony2
As described in the Doctrine docs, you just need to specify the fetching behavior to be eager.
/**
* @OneToOne(targetEntity="User", fetch="EAGER")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
See the documentation for YAML or other configuration examples.
Related videos on Youtube
Leogout
After two years as a student I began my career as a web developer. A year later I went back to school to learn more about computer science. My degree obtained, I tried web devlopment again. Things are much simpler now that I actually understand what I copy/paste from StackOverflow ;)
Updated on June 05, 2022Comments
-
Leogout almost 2 years
I have two entities in my project : User and Avatar.
User owns Avatar with a OneToOne relation.
Avatar is an entity with a file object and a fileName. It uses @ORM\HasLifecycleCallbacks to save the file or to remove it as described in the Symfony2 documentation.
In my controller, I want to remove the Avatar entity from the current user (I use
$user = $this->get('security.context')->getToken()->getUser()
), but I can't get to the avatar with$user->getAvatar()
:var_dump($user->getAvatar()); object(AppBundle\Entity\Avatar) private 'id' => int 20 public 'file' => null private 'fileName' => null
But if I try to acces the avatar's fileName, it gets returned :
$filename = $user->getAvatar()->getFileName(); var_dump($user->getAvatar()); object(AppBundle\Entity\Avatar) private 'id' => int 20 public 'file' => null private 'fileName' => string 'myfile.png'
How can I get the Avatar associated with my user ?
-
Leogout almost 9 yearsThank you so mutch, i've been searching for hours now ! (It's "EAGER", "eager" throw an exeption)
-
Andrey S. Rabchevsky about 8 yearsChanging the fetch mode during a query is only possible for one-to-one and many-to-one relations.
-
thoroc over 7 yearsFor ref: I had to put the
fetch
argument in the@OneToOne
annotation for it to work under Symfony 2.8 Doctrine/ORM 2.5 -
Nisse Engström almost 7 years@Anonymous: Note this suggested edit about the placement of the
fetch
attribute. I don't know, but it seems to be consistent with the documentation. As the author of the post, I think you can still accept the edit if you think it is correct. -
Pete almost 7 years@NisseEngström: Thanks. I'm not sure why my edit was rejected by the other two reviewers, this answer is just plain wrong without it, but very close to correct. Would it be bad form to suggest an edit again? See: github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/… and github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/… for valid the annotation definitions and their valid attributes.
-
Nisse Engström almost 7 years@Pete: Code edits are tricky to get through, especially on an accepted answer. I've applied the edit myself since it is consistent with Anonymous' own link and there hasn't been any objections. Given this, I feel reasonably confident that the edit is correct even though I'm not familiar with the subject.