Chapters
27 Chapters
|
2:45:18
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
26.
Pagination with Pagerfanta
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.3", // v3.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99.3
"doctrine/doctrine-bundle": "^2.1", // 2.4.2
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
"doctrine/orm": "^2.7", // 2.9.5
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.1
"pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
"pagerfanta/twig": "^3.3", // v3.3.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
"symfony/asset": "5.3.*", // v5.3.4
"symfony/console": "5.3.*", // v5.3.7
"symfony/dotenv": "5.3.*", // v5.3.7
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.3.*", // v5.3.7
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/runtime": "5.3.*", // v5.3.4
"symfony/stopwatch": "5.3.*", // v5.3.4
"symfony/twig-bundle": "5.3.*", // v5.3.4
"symfony/validator": "5.3.*", // v5.3.14
"symfony/webpack-encore-bundle": "^1.7", // v1.12.0
"symfony/yaml": "5.3.*", // v5.3.6
"twig/extra-bundle": "^2.12|^3.0", // v3.3.1
"twig/string-extra": "^3.3", // v3.3.1
"twig/twig": "^2.12|^3.0" // v3.3.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.3.*", // v5.3.4
"symfony/maker-bundle": "^1.15", // v1.33.0
"symfony/var-dumper": "5.3.*", // v5.3.7
"symfony/web-profiler-bundle": "5.3.*", // v5.3.5
"zenstruck/foundry": "^1.1" // v1.13.1
}
}
12 Comments
So I don't know if I have an issue that I'm just not understanding how to make work properly or one that needs a different adapter type. I have a query that joins 3 different tables and one I need to write a raw query for so it doesn't seem to return the QueryBuilder type that the adapter is expecting. In a case like this should I just return an array with all the results (we're not talking hundreds here but enough to page) and try to use an Array Adapter?
Thanks and Happy Holidays!!
Hey @phpbutcher
It's never a good idea to load all the query results in memory at once. I think you have a few options here
1) Implement your own adapter
2) Paginate the results yourself (You can use
MAXandLIMITto fetch the desired records and with another query, you can count the total of records)3) Or, if you want to keep it simple and you're 100% sure that there won't be too many records on that table, just fetch all the info
I have a problem with pagination links and Symfony form.
When I submit my form I got filtered results but pagination links are just a tags and they do not submit the whole form with new page number
How to use pagerfanta with Symfony form ?
Hey mysiar,
I don't see we have any filters in the course code of this tutorial, are you talking about your private project? Well, in short, Pagerfanta works via GET, i.e. you can see query parameters in the URL. So, your filter form also should work via GET, not POST. Otherwise, you would need to send the form again on the 2nd page.
Cheers!
Hi Victor, thanks for prompt response.
It is my own project, and that was is GET instead POST
Hey @mysiar,
Cool! Then it should work I suppose - make sure the Pagerfanta navigation links hold those specific query parameters in their URLs and it just should work. IIRC Pagerfanta should hook those query parameters itself, i.e. it should build all navigation URLs based on the current URL you have in the browser's address bar.
Cheers!
Thanks! it works perfectly with me :)
I am wondering if it is possible to create button to Load More posts? Many thanks for your continued support!
Hey Lubna,
Perfect! Thanks for your feedback that it was useful to you :)
About the "Load more" feature - yes, you can implement it, but you would need to complicate your code with some JS, and Stimulus works great for this purpose (and we have a tutorial about Stimulus btw). But Stimulus isn't required and you can do it with a plain custom JS actually. You just need to send an AJAX request to the backend specifying the number of the page you're requesting, and instead of the whole rendered page render only the list of records that you will input on the page with the JS. And that's it, your paginator already knows how to return records for the specific page.
I hope this helps!
Cheers!
Thanks for your prompt reply! Really appreciated :)
You're welcome! :)
Soo, our old query anyways gets executed, but now as a subquery? And internally it grabs all the rows and then selects 5 ids from result of the subquery?
Hey Ruslan I.!
Yup, I believe so! I can't remember exactly why it's done this way. The pagination system needs to know both (A) which "5" results it should shows and also (B) how many total results there are. I believe (though it's not immediately obvious to me when I look at it) that the first query (with the sub-query) helps it know both which 5 ids it needs, but also how many total results there are (needed to show the correct number of pagination links).
I believe this strategy *can* cause performance issues with certain super-complex queries, but I've never hit this limitation myself.
Cheers!
"Houston: no signs of life"
Start the conversation!