05.
Consultas de reducción JOINs y addSelect
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
10 Comments
Hey, team!
Found posible typo!
"if we just grab the fort_U_ne cookie data in the first"
Hey RM,
Good catch! Thanks for pointing it out, I fixed in https://github.com/SymfonyCasts/doctrine-queries/commit/3b42d2ec7047c8cf7190f8047f3045cdd66d4842
Cheers!
Hello, Ryan!
[1] Really handy optimization for reducing query count with addSelect().
[2] Is there an equivalent method of reducing query count for a Tree?
(Tree - Nestedset behavior extension for Doctrine)
when i add
->addSelect('c')i get an error
[Semantical Error] line 0, col 73 near 'c WHERE c.root': Error: 'c' is already defined.Hey @RM
You don't need to
->addSelect()with root alias as it's already added. So the question is which real queries do you want to minimize? Because this query should return the whole tree in scalar array view, and if you need additional data from relations you should add joins here and add this relation to->addSelect()Cheers.
Ok, thank you!
Hi Ryan,
The strange thing is that the counts of fortune cookies for categories have changed after adding the join. On video it's 2:40 vs 3:44. But it feels like these shouldn't change because it was a left join that was added. Perhaps some data was added/removed during video recording?
PS. thanks for another great course!
Hey, hey I'm here with updates. Yeah that was a correct situation, because you are on search page and you are limiting results to "be" query, so adding select with join fixed issue with counting, now it shows the correct number of elements in that category
Because you have this
fortuneCookie.fortune LIKE :searchTermin queryCheers!
Hi Vladimir,
Thanks for checking!
So if I understand correctly:
At first the
|lengthin Twig made it count all the records within a category, because Doctrine detected that the relation isn't hydrated and it fetched all the records from the db. What was tricky here for me is that this query was already filtering relatedfortuneCookiesby the search query.But adding an
addSelect('fortuneCookie')made Doctrine hydrate the relation only with the records matching theLIKEcondition, and then|lengthalready had related(filtered out) rows. So Doctrine didn't need to resort back to the default relation mapping(no filtering) to fetch the related fortuneCookies.I guess that makes sense now!
Yeah that's correct! Sometimes it can be tricky!
Cheers and happy coding!
That is a great catch, I'll check the details and get back to you =)
"Houston: no signs of life"
Start the conversation!