Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii CActiveDataProvider with limit and pagination

I'm trying to select few rows from the table and render it in multiple pages (pagination). This is a code in the model:

   return new CActiveDataProvider('Downloads',
        array(
            'criteria' => array(
                'select' => 'download_id,title,thumb_ext',
                'order' => 'download_id DESC',
                'limit' => $count,
            ),
            'pagination' => array('pageSize' => 5,),
        )
    );

In the view I display it using CGridView:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns' => array('download_id', 'title', 'thumb_ext'),
    ));

The problem is that CActiveDataProvider ignores limit of criteria and returns all rows of table...

Thanks.

like image 378
S2201 Avatar asked Dec 30 '10 09:12

S2201


1 Answers

I'm not positive... but I think Yii uses the LIMIT clause to do SQL result pagination, so it will overwrite/replace your LIMIT clause. You can check this by turning on the CWebLogRoute log route to see exactly what SQL is being executed.

I'm not quite sure how this is supposed to work, anyway. What is the LIMIT clause you added for? If you are paginating anyway, why not let the user paginate through all the records? The solution is probably to change your criteria to get rid of the LIMIT clause.

Are you trying to set the number of results per page? You already have the pageSize set to 5 though...

One other thing to try that might do what you want, is to check out the base Pager class, CPagination. With CLinkPager, it might let you paginate more creatively than the CListPager you are using with the CGridView.

Good luck!

like image 117
thaddeusmt Avatar answered Sep 25 '22 21:09

thaddeusmt