Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Doctrine from lazy loading one to one relationsip?

EDIT: If you're having similar issues This Topic will be of interest to you

I have User and UserSettings with one to one bidirectional relationship. It appears that even if i do not use any UserSettings values in my page, doctrine lazy loads it anyways.

Is this expected behavior? Why is Doctrine fetching this data even though I'm not using it in my page? If I'm unable to stop it, I would have to join this UserSettings to User every time I retrieve user object, but this is so unnecessary.

What can I do to prevent this from happening?

Code that loads data:

->createQuery('SELECT p, u, s FROM TestPostBundle:Post p LEFT JOIN p.user u LEFT JOIN p.sub s WHERE p.id IN (:ids)')
->setParameter('ids', $ids)
->getResult();

The in twig I loop through posts and display Post data and associated user, but I never request any UserSettings variables, I'm not accessing them at all.

like image 684
DavidW Avatar asked Mar 16 '12 10:03

DavidW


1 Answers

I've seen this question asked in a few places and am adding my answer here:

I came across this same problem and remember that the symblog tutorial gave an example of how to reduce the lazy loading by explicitly add left joins on the tables that you do not need. It seems strange to include tables on a join when you do not even want that data at all, but this way you will reduce all of those extra queries down to 1 and it does run faster.

Search for lazy loading - about 1/5 of the way down http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html

To fix this for the user/userdata issue try adding this to the user repository and use to whenever you need to get all users even if you do not want userdata. It can be further enhanced by selecting partial: ->select('partial p.{user_id,name,}')

   public function getAll($limit = 500) {
       $qb = $this->createQueryBuilder('u')
          ->select('u', 'd')
          ->leftJoin('p.userdata', 'd')
       if (false === is_null($limit))
           $qb->setMaxResults($limit);
    return $qb->getQuery()->getResult();
    }

UPDATE
The symblog tutorial seems to be down and I'm leaving the link here for the time being in case its temporary. The relevant code is here in the answer.

like image 139
George Avatar answered Sep 28 '22 05:09

George