Nerds who love the symfony-project
24 Jun
Wow, I’m blown away with how easy it is to get some pagination going with Symfony. In my previous life, I had coded my own custom Pagination class - it would have been easily 100 lines of code. I’ve just implemented some very basic pagination with less than 20 lines of code.
I’m assuming you have created your model objects, and have a basic setup going. Im also doing this in Symfony 1.0. Lets now go ahead and create some basic pagination.
1. Edit your actions.class.php for the module you want to paginate. In my case, I want to create a listing of all companies. For this, I’ll have a executeList action within that class. Here is what it looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class companyActions extends sfActions { ... public function executeList(){ //Get a listing of all companies $this->companys = CompanyPeer::doSelect(new Criteria()); //Lets do the pagination for it, firstly define a pager for the Company object. Lets go with 10 results per page $pager = new sfPropelPager('Company', 10); //Define the criteria, in this case, we won't have any - lets get all companies $pager->setCriteria(new Criteria()); //Set the first page to 1 $pager->setPage($this->getRequestParameter('page', 1)); $pager->init(); $this->pager = $pager; } ... }//class |
2. Now lets modify your view for listSuccess.php to display what results you are viewing
1 2 | //Display a summary of the total companies found, and how many companies there are echo $pager->getNbResults()." Companies found. Displaying results ".$pager->getFirstIndice()." to ".$pager->getLastIndice(); |
3. Now lets put in our main code to loop through the paginated results (still in listSucess.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | foreach ($pager->getResults() as $company){ echo $company->getCompanyName(); //go through and display all you want maybe in a nice table here //... } //Now lets display a small navigation bar with arrows for next, previous and page numbers echo "<br/>"; <?php if ($pager->haveToPaginate()): ?> <?php echo link_to('«', 'company/list?page='.$pager->getFirstPage()) ?> <?php echo link_to('<', 'company/list?page='.$pager->getPreviousPage()) ?> <?php $links = $pager->getLinks(); foreach ($links as $page): ?> <?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'company/list?page='.$page) ?> <?php if ($page != $pager->getCurrentMaxLink()): ?> - <?php endif ?> <?php endforeach ?> <?php echo link_to('>', 'company/list?page='.$pager->getNextPage()) ?> <?php echo link_to('»', 'company/list?page='.$pager->getLastPage()) ?> <?php endif ?> |
That’s it! Now have a play with that, and if you are gaim, you could even go and do some Ajax Pagination or get some sorting going.
17 Responses for "Symfony Pagination made easy with the sfPropelPager"
Woot! Great directions. I used this in a project today. I literally just changed “company” to my action name and everything worked like a champ.
Mucho appreciation.
//subscribed to your feed
You actually don’t need to do an extra doSelect there.
$this->companys = CompanyPeer::doSelect(new Criteria());
Looks good, 80 lines less of code, but could you open and close any more php tags?
x10
[...] Our new client is Bluepie. We are working on a project that is being built using the Symfony project. Today I had to figure out how to do pagination in Symfony. This short article was superb. [...]
Thanks a lot. It really helped me.
Cheers!
Great stuff, really made my day! Thx
Hello
Please let me know how can we JOIN table in sfPropelPager?
Thanks,
@Hiren,
you can create your own criteria and pass it to sfPropelPager via $pager->setCriteria($c);
Cheers,
Thanks for the article, really helped me a lot!!
I love you. I can paginate now! very tnks
Man, this is great! Just starting with sf and spent several hours trying to adapt the Jobeet Day 7 tutorial. Finally went to search for a different way, found this, and ten minutes later I had pagination working! Great instructions!
Thanks for the article.
Thanks a lot it’s so amazing, i m stucked by pagination i m novice program in symfony ,
Great stuff for new learner,
Great, so simple to understand. Thanks buddy
love it!! thank you so much for this, i got pagination up and working within a few minutes with this. cheers.
Thanks for the post. I also implemented the same. Its working fine. Gr8 job. Thank u and keep sharing information.
Cheers…..
Gagan
thanks.This code helped me a lot.Its easy to understand.
Leave a reply