加载中...

YII框架分析笔记8:CDataProvider


CDataProvider,顾名思义,数据提供者,它提供了三个抽象方法(fetchData,、fetchKeys 和 calculateTotalItemCount),分别为调用不同数据结构的数据提供了获取数据、获取键值、获取数量的,接口,在YII框架中,CActiveDataProvider、CArrayDataProvider、CSqlDataProvider是它的子类,除了提供数据之外,他还提供分页和排序功能。下面以获取数据fetchData()为例

CActiveDataProvider通过CActiveRecord的子类和CDbCriteria对象

  1. /**
  2. * Fetches the data from the persistent data storage.
  3. * @return array list of data items
  4. */
  5. protected function fetchData()
  6. {
  7. $criteria=clone $this->getCriteria();
  8.  
  9. if(($pagination=$this->getPagination())!==false)
  10. {
  11. $pagination->setItemCount($this->getTotalItemCount());
  12. $pagination->applyLimit($criteria);
  13. }
  14.  
  15. $baseCriteria=$this->model->getDbCriteria(false);
  16.  
  17. if(($sort=$this->getSort())!==false)
  18. {
  19. // set model criteria so that CSort can use its table alias setting
  20. if($baseCriteria!==null)
  21. {
  22. $c=clone $baseCriteria;
  23. $c->mergeWith($criteria);
  24. $this->model->setDbCriteria($c);
  25. }
  26. else
  27. $this->model->setDbCriteria($criteria);
  28. $sort->applyOrder($criteria);
  29. }
  30.  
  31. $this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
  32. $data=$this->model->findAll($criteria);
  33.  
  34. $this->model->setDbCriteria($baseCriteria); // restore original criteria
  35. return $data;
  36. }
CArrayDataProvider通过传人的原生数据来获取数据
  1. /**
  2. * Fetches the data from the persistent data storage.
  3. * @return array list of data items
  4. */
  5. protected function fetchData()
  6. {
  7. if(($sort=$this->getSort())!==false && ($order=$sort->getOrderBy())!='')
  8. $this->sortData($this->getSortDirections($order));
  9.  
  10. if(($pagination=$this->getPagination())!==false)
  11. {
  12. $pagination->setItemCount($this->getTotalItemCount());
  13. return array_slice($this->rawData, $pagination->getOffset(), $pagination->getLimit());
  14. }
  15. else
  16. return $this->rawData;
  17. }
CSqlDataProvider通过传人的sql,经过db执行获取数据
  1. /**
  2. * Fetches the data from the persistent data storage.
  3. * @return array list of data items
  4. */
  5. protected function fetchData()
  6. {
  7. $sql=$this->sql;
  8. $db=$this->db===null ? Yii::app()->db : $this->db;
  9. $db->active=true;
  10.  
  11. if(($sort=$this->getSort())!==false)
  12. {
  13. $order=$sort->getOrderBy();
  14. if(!empty($order))
  15. {
  16. if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$sql))
  17. $sql.=', '.$order;
  18. else
  19. $sql.=' ORDER BY '.$order;
  20. }
  21. }
  22.  
  23. if(($pagination=$this->getPagination())!==false)
  24. {
  25. $pagination->setItemCount($this->getTotalItemCount());
  26. $limit=$pagination->getLimit();
  27. $offset=$pagination->getOffset();
  28. $sql=$db->getCommandBuilder()->applyLimit($sql,$limit,$offset);
  29. }
  30.  
  31. $command=$db->createCommand($sql);
  32. foreach($this->params as $name=>$value)
  33. $command->bindValue($name,$value);
  34.  
  35. return $command->queryAll();
  36. }




还没有评论.