CSqlDataProvider implements a data provider based on a plain SQL statement.
CSqlDataProvider provides data in terms of arrays, each representing a row of query result.
Like other data providers, CSqlDataProvider also supports sorting and pagination. It does so by modifying the given sql statement with “ORDER BY” and “LIMIT” clauses. You may configure the sort and pagination properties to customize sorting and pagination behaviors.
CSqlDataProvider may be used in the following way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_user')->queryScalar(); $sql='SELECT * FROM tbl_user'; $dataProvider=new CSqlDataProvider($sql, array( 'totalItemCount'=>$count, 'sort'=>array( 'attributes'=>array( 'id', 'username', 'email', ), ), 'pagination'=>array( 'pageSize'=>10, ), )); // $dataProvider->getData() will return a list of arrays. |
Note: if you want to use the pagination feature, you must configure the totalItemCount property to be the total number of rows (without pagination). And if you want to use the sorting feature, you must configure sort property so that the provider knows which columns can be sorted.
More in: http://www.yiiframework.com/doc/api/1.1/CSqlDataProvider