Pagination | Using Pager with Doctrine | sfDoctrinePager | Symfony | PHP | MVC Framework
anoopsachari | May 14, 2010 | Comments 14
As symfony is updated frequently i decided to update my skills too . I have been into many symfony project but the ORM used was Propel but the latest version supports Doctrine , so planned to do a project using doctrine .
As i like yml than xml , wrote schema using in yml and succeeded in generating the model , sql and inserted successfully . But implementing pagination using pager was not simple as i thought . After some research implemented it successfully and i am sharing you the code with you .
Code in action page
public function executeIndex(sfWebRequest $request)
{
$this->pager = new sfDoctrinePager('TableName', '5');
$this->pager->setQuery(Doctrine::getTable('TableName')->createQuery('a'));
$this->pager->setPage($request->getParameter('page', 1));
$this->pager->init();
}
The above code fetches 5 results from the table .
Code within template page :
<?php foreach ($pager->getResults() as $metro_bussiness_list): ?>
<tr>
<td><?php echo $metro_bussiness_list->getMetroCategoryId() ?></td>
<td><?php echo $metro_bussiness_list->getTitle() ?></td>
<td><?php echo $metro_bussiness_list->getImage() ?></td>
</tr>
<?php endforeach; ?>
<div style="width:20px;float:left;margin-top:3px;margin-right:10px">
<?php echo link_to('first', 'module/index?page='.$pager->getFirstPage()) ?>
</div>
<div>
<?php if ($pager->haveToPaginate()): ?>
<?php $links = $pager->getLinks(); foreach ($links as $page): ?>
<div style="padding:5px 5px 5px 5px;border:#000000 thin solid;float:left;width:10px;margin-left:3px;font-size:10px" >
<?php echo ($page == $pager->getPage()) ? $page : link_to($page, 'bussiness/index?page='.$page) ?>
</div>
<?php endforeach ?>
<?php endif ?>
</div>
<div style="width:20px;float:left;margin-left:10px;margin-top:3px;">
<?php echo link_to('last', 'module/index?page='.$pager->getLastPage()) ?>
</div>
It’s done . Hope it helped you to implement it .
Source of the article .
Filed Under: symfony
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.

connect me on






nice code! thanks!
i’m just new to symfony and it really helped me.
by the way, how can i add an descending order on this:
$this->pager->setQuery(Doctrine::getTable(‘TableName’)->createQuery(‘a’));
thanks!
Try this to sort in desc order and let me knw .
$this->pager->setQuery(Doctrine::getTable(‘TableName’)->createQuery(‘a’)->orderBy(‘a.id DESC’));
wow! it worked man!
you’re really awesome!
one last thing, favor that is! i’m listing it by category. i’m doing a list of laptops by brands(category) and models( DESC – latest down to the oldest).
thanks man!
i forgot to say that the category and models are on both different tables.
Why not use doctrine pager?
http://www.doctrine-project.org/documentation/manual/1_2/en/utilities
@chris Thankyou for the info . I was new to doctrine when i wrote this article . So tried to implement pagination using pager .
Hey everybody;
and i don’t now where is the probleme
can you see this code pager PLEAAAASE it don’t work
public function executeList(sfWebRequest $request)
{
$Shops = Doctrine::getTable(‘Shop’)
->getAllShops();
$numPage = $request->getParameter(‘page’, 1);
$this->pager = new sfDoctrinePager(‘Shop’, 4);
$this->pager->setQuery($Shops);
$this->pager->setPage($numPage);
$this->pager->init();
}
_paginate.php
getRouting()->getCurrentInternalUri(false) ?>
getPreviousPage()) ?>
getLinks() as $page): ?>
getPage() == $page): ?>
getNextPage()) ?>
getLastPage()) ?>
listSuccess.php
getResults() as $shop): ?>
getPresentation(); ?>
haveToPaginate()): ?>
$pager)) ?>
getRouting()->getCurrentInternalUri(false) ?>
public function getAllShops() dans ShopTable.class.php
{
$q = $this->createQuery(‘p’)
->execute();
return $q;
}
thank’s soo muuuuuch
very nice. thanks
[...] working with Doctrine for the first time. This is a good explanation of handling pagination. [...]
Hey dudes I just have a quick question;
Can I use this pager more than once on a page? I mean, you can add another query and make another $nav variable with another name, but you can’t really give another get variable as it will always be ‘page’..
Good post!,
Very simple explained, and useful.
Thank you! Very usefull.
HI, i am new to symfony and php as well, is there any sample using facebook connect plugin
Thanks
Anil
It’s works!!! Thank you thank you thank you thank you, you’re a life saver! i looked all over the place for this!!! Good job!