6

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.

Community
  • 1
  • 1
DavidW
  • 5,069
  • 14
  • 46
  • 68
  • Post the code which loads users, please. – Vladislav Rastrusny Mar 16 '12 at 11:08
  • @FractalizeR I just added code – DavidW Mar 16 '12 at 12:50
  • 1
    It is not expected behavior. I'm guessing that somewhere you are in fact getting a user setting. Try putting a die() statement in your getUserSetting method. That should confirm or deny my speculation. – Cerad Mar 16 '12 at 13:18
  • @Cerad I put the die() statement in User's getUserSettings finction and it is not being called, this confirms that I am not calling any UserSettings methods. How else can I troubleshoot this? – DavidW Mar 16 '12 at 14:55
  • I posted a workaround in the related question: http://stackoverflow.com/a/14885237/138106 – Dave Lancea Feb 14 '13 at 22:28
  • Thanks, the link you provided ended up containing the answer for me. ( http://stackoverflow.com/a/11920901/1076092 ) – willbradley Oct 31 '14 at 21:07

2 Answers2

4

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.

George
  • 1,478
  • 17
  • 28
1

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

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • Related post I thought you might be interested to look at: http://stackoverflow.com/questions/9848747/primary-key-of-owning-side-as-a-join-column – DavidW Mar 24 '12 at 02:50
  • I posted a workaround in the related question: http://stackoverflow.com/a/14885237/138106 – Dave Lancea Feb 14 '13 at 22:29