Paginating and sorting data
In the latest Yii releases, the focus was moved from using Active Record directly to grids, lists, and data providers. Still, sometimes it is better to use Active Record directly. Let's see how to list paginated AR records with the ability to sort them.
Getting ready
Set up a new application using
yiic webapp.Create a database structure table
postwithidandtitleas fields, and add 10 to 20 records.Generate the
Postmodel using Gii.
How to do it...
First, you need to create
protected/controllers/PostController.php:class PostController extends Controller { function actionIndex() { $criteria = new CDbCriteria(); $count=Post::model()->count($criteria); $pages=new CPagination($count); // elements per page $pages->pageSize=5; $pages->applyLimit($criteria); // sorting $sort = new CSort('Post'); $sort->attributes = array( 'id', 'title', ); $sort->applyOrder($criteria); $models = Post::model...