在 Yii 中对相关数据进行分页的最佳方式是什么?
例如,我可能有帖子,我想对评论进行分页。
这可以通过使用这样的代码来实现,假设您有CommentController.php
:
function listComments(){
/* Write code here to fetch comments with current post from db and
return template code via calling renderPartial().
Also implement paging, sorting etc. in this same function.
*/
}
之后调用此函数以查看帖子页面并在查看帖子模板中传递评论的列表代码。
查看CActiveDataProvider它已经内置了分页支持。