03.
Pagination Links
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
Why use that $createLinkUrl function instead of creating private function?
Hi,
Great tutorial,
But why not using HATEOS bundle to generate links like you do in silex rest tutorial ?
Thanks.
Hey Chuck!
Thank you!
Great question! HATEOS bundle is really cool! But we don't want to show this topic in basics and kept it for the next episode (Symfony REST 5) which was already released a few weeks ago. You can check it http://knpuniversity.com/sc... .
Cheers!
Hi,
thanks for the quick answer.
I just saw that when I take a quick look to the rest of the course.
Any way, great job.
Hi,
When I pass a query (e.g.
$em->createQuery), instead of a query builder to this paginator, I get an error: <strong>"The Paginator does not support Queries which only yield ScalarResults."</strong>.Is there a paginator I can use with a query that returns scalar results?
Thank you!
Hey Vlad!
Hmm, interesting! I don't have experience doing this, but in theory, I would be surprised if it's not supported. The error specifically is coming from Doctrine (not the paginator library itself), so changing libraries (probably) won't help.
Could you post your query? By looking at the code in Doctrine near where that error is thrown, it looks like it might be complaining that there is no "root alias" in the query, which means it may just be the query itself that's causing the problems. If you haven't tried it yet, try including the primary key (e.g. alias.id) in the results.
Btw - link to the code in Doctrine fwiw - it's complex, but might help :) https://github.com/doctrine...
Cheers!
Hi Ryan,
I just got it to work! Turns out I had to disable output walkers.
I changed the following line in PaginationFactory::createCollection()
new DoctrineORMAdapter($queryBuilder, true, false);
`thus setting the <strong>$useOutputWalkers</strong> parameter to <strong>false</strong>.
My query is a DTO query with the <strong>NEW</strong> operator, with 5 joins.
What are output walkers anyway, and in which cases are they needed?
Thank you for your hints.
Wow, nice work!
So, output walkers are really advanced. An output walker - which is a less common thing to worry about, there are also tree walkers, which modify the AST - is responsible for turning the "DQL" (represented by an abstract syntax tree - AST) into the actual SQL. In essence, this the actual code that turns the AST into the actual query string. Here's the default walker: https://github.com/doctrine...
In the pagination library, they use a sub-class of this walker: https://github.com/doctrine... - which helps to "count" the query result, used for pagination. That's what you turned off. I'm honestly not sure what the result of that is - but if it works, do it :). Pagination is quite "magical" - since Doctrine needs to take your query and dynamically change it so that it can first get a COUNT of those potential results (without actually fetching all of them). The only thing I'd double-check is that the pagination library is not now querying for ALL the rows, just to get a count of them. Double-check that in the profiler.
Cheers!
Hi Ryan,
I was also able to accomplish this by using a custom ORM adapter that implements <strong>AdapterInterface</strong> and the 2 interface methods. Its constructor has two parameters: a query to get the collection of items and a query to get the total number of items.
Here is the code:
Wow, very clean. I really appreciate you posting your complete solutions inside here - it will definitely help others :)
Thanks!
Thank you, Ryan!
"Houston: no signs of life"
Start the conversation!