This begins with the introduction of the Zii component, a brief introduction to the data source interface supported by Yii Idataprovider,idataprovider The main function is to provide a data source for UI components such as Gridview,listview, and also to support the paging and sorting of data. The following figure is the three types of data sources that are built in Yii:
Cactivedataprovider a data source based on an active record
Carrarydataprovider data sources based on arrays
Csqldataprovider a data source based on SQL queries
The three kinds of data provider are used in a similar way:
Cactivedataprovider is based on ActiveRecord, which reads database records through the AR Cactiverecord::findall method and sets the query criteria through the criteria property.
Such as:
$dataProvider =new
cactivedataprovider (' Post ', Array ('
criteria ' =>array) ('
condition ' => ' status= 1 ',
' order ' => ' Create_time DESC ',
' with ' =>array (' author '),
' pagination ' =>array (
' PageSize ' =>20,))
;
$dataProvider->getdata () would return a list of Post objects
Carraydataprovider is based on an array where the properties RawData set raw data, typically an array or DAO query results, such as:
$rawData =yii::app ()->db->createcommand (
' SELECT * from Tbl_user ')->queryall ();
or using: $rawData =user::model ()->findall ();
$dataProvider =new Carraydataprovider ($rawData, Array (
' id ' => ' user ',
' sort ' =>array (
' Attributes ' =>array (
' id ', ' username ', ' email ',
),
),
' pagination ' =>array (
' pageSize ' =>10,))
;
$dataProvider->getdata () would return a list of arrays.
Csqldataprovider is based on SQL queries and is configured by setting up SQL statements, such as:
$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 () would return a list of arrays.
For Dataprovider detailed documentation, see YII Documentation: Http://www.yiiframework.com/doc/api/1.1/CDataProvider
See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm