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

12,454

Solution 1

I also faced same problem. It seems that when querying from inverse side doctrine also queries the owning side. See this discussion.

Solution 2

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.

Share:
12,454
DavidW
Author by

DavidW

Updated on June 15, 2022

Comments

  • DavidW
    DavidW almost 2 years

    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.

  • DavidW
    DavidW about 12 years
    Related post I thought you might be interested to look at: stackoverflow.com/questions/9848747/…
  • Dave Lancea
    Dave Lancea about 11 years
    I posted a workaround in the related question: stackoverflow.com/a/14885237/138106