Class CSqlDataProvider
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 CSqlDataProvider::$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:
$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.
- CComponent
- CDataProvider implements IDataProvider
- CSqlDataProvider
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.1.4
Located at x2engine/framework/web/CSqlDataProvider.php
public
|
|
protected
array
|
|
protected
array
|
|
protected
integer
|
#
calculateTotalItemCount( )
Calculates the total number of data items. This method is invoked when |
getData(),
getId(),
getItemCount(),
getKeys(),
getPagination(),
getSort(),
getTotalItemCount(),
setData(),
setId(),
setKeys(),
setPagination(),
setSort(),
setTotalItemCount()
|
public
|
$db |
|
#
the database connection to be used in the queries. Defaults to null, meaning using Yii::app()->db. |
public
string| |
$sql |
|
#
the SQL statement to be used for fetching data rows. Since version 1.1.13
this can also be an instance of |
public
array
|
$params | array() |
#
parameters (name=>value) to be bound to the SQL statement. |
public
string
|
$keyField | 'id' |
#
the name of key field. Defaults to 'id'. |
$data,
$id,
$itemCount,
$keys,
$pagination,
$sort,
$totalItemCount
|