开发者

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 th开发者_StackOverflow社区e 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.


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!


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'),
));


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,
        )
    );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜