05.
Joins and addSelect Reduce Queries
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.
11 Comments
Hey,
Im using the querybuilder for joining tables. Now i want to use criteria but the matching method is not available on array that is returned from the querybuilder. How can i use criteria in my querybuilder result?
Hey Rob,
As I understand you want to apply Doctrine criteria on the simple PHP array - you can't. Criteria can be applied on Doctrine's ArrayCollection only. Or instead of returning the result array in your repository you can return the query builder, i.e. do not call "->getQuery()->getResult()" and then you can call "->addCriteria()" on query builder. Or, even better, just pass the criteria to the repository method and apply them with "->addCriteria()" on query builder, then return the simple PHP array back.
Cheers!
Never mind. passed it trough an arrayCollection. :)
Hey Rob,
Ah, noticed your comment too late. Glad you solved it yourself, but I left a few ideas for you in my previous comment :)
I hope this helps!
Cheers!
Hey,
Just an idea when i follow the turorial with works but it might be an good idea to update it to 5.4 instead to keep it at 5.3 because short array syntax only then got introduced in PHP.
But still keep up the good work guys love the tutorials.
Cheers,
Hey, I think doctrine present a lot of joins types (innerJoin, Join, leftJoin, outerJoin...bla bla) :( What I'm supposed to learn with symfony as Join types.
1) Waht is the dffierence between InnerJoin, Join and leftJoin ?
2) Does addading the fetch='EAGER' annotation on $frtuneCookies do the same job of addSelect()?
Hey @Batiste!
Very good question! Let's see if we can shed some life on these :).
1) first, the ->innerJoin() and ->join() methods on the QueryBuilder object are identical - check it out here: https://github.com/doctrine...
Second, in general, the difference between an innerJoin and a leftJoin deals with whether you are joining over to *one* matching row or many matching rows. For example, in this video, each Category has many FortuneCookies. So, we want to "left join" because we're joining over to "many" rows. This is probably an imprecise explanation, but it's a simple way that helps me think about it. Now, look at the other direction: suppose we were querying for 10 FortuneCookie objects, but (maybe to save a query) we want to join over to Category. In this case, each row in FortuneCookie will join to *one* Category. So, I would use an innerJoin.
2) fetch="EAGER" is ALMOST the same as using the addSelect(), but not quite. With the addSelect() strategy, we are making ONE query that contains a JOIN to get all of our data. With fetch="EAGER", there are 2 queries: one for the original Category and then a second query for all of the FortuneCookies related to that Category (this works even if you fetch multiple Category objects - I'm pretty sure the second query would query for ALL of the FortunateCookies you need for all of the Categories). And of course, the addSelect() way only applies to a single query, where fetch="EAGER" makes that behavior always happen, which can be nice for convenience.
Let me know if this makes sense! Great questions!
Cheers!
From one of the Symfony tutorials I recall you can also do fetch=EXTRA_LAZY, would that make sense here?
Hey Asfsdfas,
That "fetch=EXTRA_LAZY" allows you to not load the full collection on some operations like count, etc - it will call a separate query avoiding loading the full collection into memory unless you will start iterate over it. It's a good performance trick sometimes. You can read more about it in Doctrine docs here: https://www.doctrine-projec...
Cheers!
Hello. o/
( Aside )Seems there's some problem with the formatting? I'm seeing some blocks <<an asp="" developer="" stole="" our="" code="" block.="" quick="" -="" chase="" them!="">>
Hey Caim o/
Thanks for reporting this, we have some problem with code blocks inaccessibility. I've updated the cache, now it works.
Cheers!
"Houston: no signs of life"
Start the conversation!