用数组array代替CActiveRecord构建CArrayDataProvider
当需要构建 GridView的时候;
常常用 CArrayDataProvider 或者 CActiveDataProvider
这是就需要一个CActiveRecord
比如:
- $sql = "SELECT * FROM zhuanli_applicantCount where kId=:kId order by percentage desc";
- $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));
- $IDARRAY = array();
-
- $paginationTop = new CPagination(sizeof($rawData));
- $paginationTop->pageVar = 'page';
- $paginationTop->pageSize= 10;
- $paginationButtom = clone $paginationTop;
- $dataProvider = new CArrayDataProvider($rawData, array(
- 'keyField'=>false,
- ));
- $this->render('applicantListMore', array(
- 'dataProvider'=>$dataProvider,
- 'paginationTop'=>$paginationTop,
- 'totalItemCount'=>sizeof($rawData),
- 'paginationButtom'=>$paginationButtom,
- ));
如果需要展示的数据主题并不在一个数据表中,而是我们用Coreseek得到的一个数组,或者就是一个普通的数组
如何用数组数据来展现 GridView视图呢?
首先可以去看下$dataProvider =
new CArrayDataProvider($rawData, array(
的数据结构
- object(CArrayDataProvider)#37 (10) {
- ["keyField"]=>
- bool(false)
- ["rawData"]=>
- array(24) {
- [0]=>
- array(2) {
- ["provinceId"]=>
- string(2) "11"
- ["provinceName"]=>
- string(9) "江苏省"
- }
- ……
- ……
- [23]=>
- array(2) {
- ["provinceId"]=>
- string(2) "15"
- ["provinceName"]=>
- string(9) "江西省"
- }
- }
- ["_id":"CDataProvider":private]=>
- NULL
- ["_data":"CDataProvider":private]=>
- NULL
- ["_keys":"CDataProvider":private]=>
- NULL
- ["_totalItemCount":"CDataProvider":private]=>
- NULL
- ["_sort":"CDataProvider":private]=>
- NULL
- ["_pagination":"CDataProvider":private]=>
- NULL
- ["_e":"CComponent":private]=>
- NULL
- ["_m":"CComponent":private]=>
- NULL
- }
好,那我们想要的结果是最后能够
$dataProvider = new CArrayDataProvider($IDARRAY, array(
那就来构建这个数组
- $IDARRAY = array();
- $total =0;
- $totalItemCount = 0;
- if($result['total'])
- {
- foreach($result['matches'] AS $k=>$v)
- {
- $IDARRAY[] = array('site_name'=>$v['attrs']['site_name'],'count'=>$v['attrs']['@count']);
- $total+= $v['attrs']['@count'];
- }
- $totalItemCount = $result['total'];
- $this->sourceMoreIDARRAY=$IDARRAY;
- $this->sourceMoreNum=$total;
- }
-
- $paginationTop = new CPagination($totalItemCount);
- $paginationTop->pageVar = 'page';
- $paginationTop->pageSize= 10;
- $paginationButtom = clone $paginationTop;
- $dataProvider = new CArrayDataProvider($IDARRAY, array(
- 'keyField'=>false,
- ));
-
- $this->render('sourceListMore', array(
- 'dataProvider'=>$dataProvider,
- 'paginationTop'=>$paginationTop,
- 'totalItemCount'=>$totalItemCount,
- 'paginationButtom'=>$paginationButtom,
- ));
来看下
- $dataProvider = new CArrayDataProvider($IDARRAY, array(
- object(CArrayDataProvider)#37 (10) {
- ["keyField"]=>
- bool(false)
- ["rawData"]=>
- array(456) {
- [0]=>
- array(2) {
- ["site_name"]=>
- string(0) ""
- ["count"]=>
- int(3046)
- }
- [1]=>
- array(2) {
- ["site_name"]=>
- string(9) "易车网"
- ["count"]=>
- int(904)
- }
- [455]=>
- array(2) {
- ["site_name"]=>
- string(9) "科技讯"
- ["count"]=>
- int(1)
- }
- }
- ["_id":"CDataProvider":private]=>
- NULL
- ["_data":"CDataProvider":private]=>
- NULL
- ["_keys":"CDataProvider":private]=>
- NULL
- ["_totalItemCount":"CDataProvider":private]=>
- NULL
- ["_sort":"CDataProvider":private]=>
- NULL
- ["_pagination":"CDataProvider":private]=>
- NULL
- ["_e":"CComponent":private]=>
- NULL
- ["_m":"CComponent":private]=>
- NULL
- }
OK ,成功,其实用着样的方法,比用SQL 是不是要方便很多?
但是应该适用于,GridView中 column没几个的情况。
如果有很多Column,一般都是某个数据表的数据要展示,当然就用CActiveRecord
- $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));