0

I have easy query with inner join

$qb = $this->createQueryBuilder('r')
    ->innerJoin('UserBundle:User', 'u')
    ->where('r.nick = u.id')
    ->andWhere('r.odobera=:id')
    ->setParameter('id', $id)
    ->orderBy('r.time', 'DESC')
    ->setMaxResults(50);

return $qb->getQuery()->getResult();

It should be one query right? But in profiler i have

SELECT t0.username AS [...] FROM users t0 WHERE t0.id = ? LIMIT 1
Parameters: [1465] 
[Display runnable query]
Time: 1.79 ms [   Explain query ]
 SELECT r0_.id AS id0, [...] FROM requests r0_ INNER [...] ORDER BY r0_.time DESC LIMIT 50
Parameters: [1465] 
[Display runnable query]
Time: 0.67 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['2072'] 
[Display runnable query]
Time: 0.64 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['2386'] 
[Display runnable query]
Time: 0.50 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['79'] 
[Display runnable query]
Time: 0.50 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['2432'] 
[Display runnable query]
Time: 0.50 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['40'] 
[Display runnable query]
Time: 0.52 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['2090'] 
[Display runnable query]
Time: 0.46 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['1044'] 
[Display runnable query]
Time: 0.51 ms [   Explain query ]
 SELECT t0.username AS username1, [...] FROM users t0 WHERE t0.id = ?
Parameters: ['27'] 
[Display runnable query]
Time: 0.54 ms [   Explain query ]

one query for each "join" for each record... WHY?

PS: this text is just for making this post longer because i can't submit question with "just" so many details.. but i think my problem is explained very well. BTW: i asked friend and he has similiard problem)

1 Answer 1

0

You didn't specify what to select, so just requests where hydrated.

You can specify what to hydrate with select() method on the query builder:

$qb = $this->createQueryBuilder('r')
     ->select(array('r', 'u'))
     ->innerJoin('UserBundle:User', 'u')
     ->where('r.nick = u.id')
     ->andWhere('r.odobera=:id')
     ->setParameter('id', $id)
     ->orderBy('r.time', 'DESC')
     ->setMaxResults(50);

return $qb->getQuery()->getResult();
5
  • hm... it causing me problems ... didn't symfony guess select based on the relationship in entities? Because now the dump of the result looks nice.. requests object with property nick what is proxy to user entity... but when i add your part of code it looks weird and i cant then pass results to twig and access them like for follower in followers or something like that anymore...check screen: upnito.sk/0/5u5ztfusawxq5pk7uab7f3e49sr9h534.png Mar 3, 2013 at 20:03
  • 1
    Symfony has nothing to do with it. It's a purely Doctrine thing. I updated my answer since initially I made a mistake on how you should specify fields you want to select. Mar 3, 2013 at 20:10
  • I'm not sure what (and how) you're dumping. The code should produce correct result. Mar 3, 2013 at 20:32
  • \Doctrine\Common\Util\Debug::dump($followers);die(); Mar 3, 2013 at 20:33
  • 1
    I'm sure that string is here in place to prevent accidental recursions and I think that Twig should have not problem whatsoever with traversing the object. Have you tried running it? If it causes error which one is it? Mar 3, 2013 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.