Resultset Paging with Yii Framework

I’ve been playing around with Yii Framework lately. Here’s how I ended up doing pagination with the resources that are provided. Now, one of the goals was not to rely on a complete solution provided by the framework itself but to understand and work with the building blocks myself.
Here’s the code.
This is what happens in the Controller:

public function actionPaginationSample() {
$Criteria = new CDbCriteria();
$Criteria->condition = "field_name = :value";
$Criteria->params = array (
                ':value' => $input
$Page = new CPagination(ModelName::model()->count($Criteria));
$Page->pageSize = Yii::app()->params['listPerPage'];
$this->render('view_name', array(
    'ModelInstance' => ModelName::model()->->findAll($Criteria),
    'PageInstance' => $Page

Notice how we make $ModelInstance and $PageInstance available for the view.
The listPerPage value is defined in the /config/main.php In my case it’s at the bottom of that page:

        'listPerPage'=> 10

The View looks like this (protected/views/controller_name/view_name.php):

         foreach($ModelInstance as $Data) {
            echo $Data->field_name' ;//name of the db table field name

    <?php $this->widget('ext.Pagination.Base', array('CPaginationObject' => $PageInstance)); ?>

The key here is the last line. We are basically loading a custom widget that simply sets the default properties and overrides a few methods of the CLinkPager widget provided by Yii.

Here’s how the widget class looks like. By the way, it’s located under protected/extensions/Pagination/Base.php
I called it “Base” because I might override it on some other pages if I want a different pager.

class Base extends CLinkPager{
    public $CPaginationObject;
    public function init() {
       $this->pages = $this->CPaginationObject;
       $this->cssFile = false;
       $this->maxButtonCount = 10;

       $this->htmlOptions = array();
       $this->id = 'pager';
       $this->footer = '';
       $this->header = '';    
       $this->firstPageLabel = 'First;
       $this->lastPageLabel = '
       $this->nextPageLabel = 'Next';
       $this->prevPageLabel = 'Previous';  
    public function run() {
        echo '<div class="pagercontainer">';
        echo $this->header;
        echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
        echo $this->footer;
        echo '</div>';


So, we are basically setting most of the available options that the CLinkPager class has. I set the cssFile property to false because I don’t want to use the built in css definitions for the pager. The properties are nicely described here. I also override the run() method because I wanted to add an extra


block to wrap around the paging links.

That’s it.

1 Comment to “Resultset Paging with Yii Framework”

  1. By Kevin Korb, August 13, 2010 @ 9:26 am


    By Yii’s conventions typcially if you override one of their classes, you’d just drop the prefixed’C’ so this would simply be the ‘LinkPager’ class, and also when extending one of the Yii-provided classes it’s typical to put them in the ‘components’ folder inside of protected.


RSS feed for comments on this post. TrackBack URI

Leave a Reply