yii CActiveDataProvider with limit and pagination

24,695

Solution 1

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!

Solution 2

I had the same problem and I found a solution as follows:

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

and set CGridView to pagination is hidden:

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

Solution 3

One of the basic features of a data provider is that you can override the calculation of the amount of rows by specifying a "totalItemCount" in the config. Normally (I haven't tested it) this also works for the ActiveDataProvider:

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

Related videos on Youtube

S2201
Author by

S2201

Updated on August 31, 2020

Comments

  • S2201
    S2201 over 3 years

    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.

Related