加载中...

用数组array代替CActiveRecord构建CArrayDataProvider


当需要构建 GridView的时候;
常常用 CArrayDataProvider 或者 CActiveDataProvider
这是就需要一个CActiveRecord

比如:
  1. $sql = "SELECT * FROM zhuanli_applicantCount where kId=:kId order by percentage desc";
  2. $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));
  3. $IDARRAY = array();
  4. $paginationTop = new CPagination(sizeof($rawData));
  5. $paginationTop->pageVar = 'page';
  6. $paginationTop->pageSize= 10;
  7. $paginationButtom = clone $paginationTop;
  8. $dataProvider = new CArrayDataProvider($rawData, array(
  9. 'keyField'=>false,
  10. ));
  11. $this->render('applicantListMore', array(
  12. 'dataProvider'=>$dataProvider,
  13. 'paginationTop'=>$paginationTop,
  14. 'totalItemCount'=>sizeof($rawData),
  15. 'paginationButtom'=>$paginationButtom,
  16. ));




如果需要展示的数据主题并不在一个数据表中,而是我们用Coreseek得到的一个数组,或者就是一个普通的数组
如何用数组数据来展现 GridView视图呢?

首先可以去看下$dataProvider = new CArrayDataProvider($rawData, array(
的数据结构
  1. object(CArrayDataProvider)#37 (10) {
  2. ["keyField"]=>
  3. bool(false)
  4. ["rawData"]=>
  5. array(24) {
  6. [0]=>
  7. array(2) {
  8. ["provinceId"]=>
  9. string(2) "11"
  10. ["provinceName"]=>
  11. string(9) "江苏省"
  12. }
  13. ……
  14. ……
  15. [23]=>
  16. array(2) {
  17. ["provinceId"]=>
  18. string(2) "15"
  19. ["provinceName"]=>
  20. string(9) "江西省"
  21. }
  22. }
  23. ["_id":"CDataProvider":private]=>
  24. NULL
  25. ["_data":"CDataProvider":private]=>
  26. NULL
  27. ["_keys":"CDataProvider":private]=>
  28. NULL
  29. ["_totalItemCount":"CDataProvider":private]=>
  30. NULL
  31. ["_sort":"CDataProvider":private]=>
  32. NULL
  33. ["_pagination":"CDataProvider":private]=>
  34. NULL
  35. ["_e":"CComponent":private]=>
  36. NULL
  37. ["_m":"CComponent":private]=>
  38. NULL
  39. }

好,那我们想要的结果是最后能够
$dataProvider = new CArrayDataProvider($IDARRAY, array(
那就来构建这个数组
  1. $IDARRAY = array();
  2. $total =0;
  3. $totalItemCount = 0;
  4. if($result['total'])
  5. {
  6. foreach($result['matches'] AS $k=>$v)
  7. {
  8. $IDARRAY[] = array('site_name'=>$v['attrs']['site_name'],'count'=>$v['attrs']['@count']);
  9. $total+= $v['attrs']['@count'];
  10. }
  11. $totalItemCount = $result['total'];
  12. $this->sourceMoreIDARRAY=$IDARRAY;
  13. $this->sourceMoreNum=$total;
  14. }
  15.  
  16. $paginationTop = new CPagination($totalItemCount);
  17. $paginationTop->pageVar = 'page';
  18. $paginationTop->pageSize= 10;
  19. $paginationButtom = clone $paginationTop;
  20. $dataProvider = new CArrayDataProvider($IDARRAY, array(
  21. 'keyField'=>false,
  22. ));
  23.  
  24. $this->render('sourceListMore', array(
  25. 'dataProvider'=>$dataProvider,
  26. 'paginationTop'=>$paginationTop,
  27. 'totalItemCount'=>$totalItemCount,
  28. 'paginationButtom'=>$paginationButtom,
  29. ));
来看下
  1. $dataProvider = new CArrayDataProvider($IDARRAY, array(
  2. object(CArrayDataProvider)#37 (10) {
  3. ["keyField"]=>
  4. bool(false)
  5. ["rawData"]=>
  6. array(456) {
  7. [0]=>
  8. array(2) {
  9. ["site_name"]=>
  10. string(0) ""
  11. ["count"]=>
  12. int(3046)
  13. }
  14. [1]=>
  15. array(2) {
  16. ["site_name"]=>
  17. string(9) "易车网"
  18. ["count"]=>
  19. int(904)
  20. }
  21. [455]=>
  22. array(2) {
  23. ["site_name"]=>
  24. string(9) "科技讯"
  25. ["count"]=>
  26. int(1)
  27. }
  28. }
  29. ["_id":"CDataProvider":private]=>
  30. NULL
  31. ["_data":"CDataProvider":private]=>
  32. NULL
  33. ["_keys":"CDataProvider":private]=>
  34. NULL
  35. ["_totalItemCount":"CDataProvider":private]=>
  36. NULL
  37. ["_sort":"CDataProvider":private]=>
  38. NULL
  39. ["_pagination":"CDataProvider":private]=>
  40. NULL
  41. ["_e":"CComponent":private]=>
  42. NULL
  43. ["_m":"CComponent":private]=>
  44. NULL
  45. }

OK ,成功,其实用着样的方法,比用SQL 是不是要方便很多?



但是应该适用于,GridView中 column没几个的情况。
如果有很多Column,一般都是某个数据表的数据要展示,当然就用CActiveRecord
  1. $rawData= model_applicant::model()->findAllBySql($sql,array('kId'=>$KDValue));




还没有评论.