Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 127 lines (119 sloc) 3.503 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
<?php
/**
* CActiveDataProvider implements a data provider based on ActiveRecord.
*
* CActiveDataProvider provides data in terms of ActiveRecord objects which are
* of class {@link modelClass}. It uses the AR {@link CActiveRecord::findAll} method
* to retrieve the data from database. The {@link criteria} property can be used to
* specify various query options, such as conditions, sorting, pagination, etc.
*
* CActiveDataProvider may be used in the following way:
* <pre>
* $dataProvider=new CActiveDataProvider('Post', array(
* 'criteria'=>array(
* 'condition'=>'status=1 AND tags LIKE :tags',
* 'params'=>array(':tags'=>$_GET['tags']),
* 'with'=>array('author'),
* ),
* 'pagination'=>array(
* 'pageSize'=>20,
* ),
* ));
* // $dataProvider->getData() will return a list of Post objects
* </pre>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web
* @since 1.1
*/
class CActiveDataProvider extends CDataProvider
{
/**
* @var string the primary ActiveRecord class name. The {@link getData()} method
* will return a list of objects of this class.
*/
public $modelClass;
/**
* @var string the name of key attribute for {@link modelClass}. If not set,
* it means the primary key of the corresponding database table will be used.
*/
public $keyAttribute;

private $_criteria;

/**
* Constructor.
* @param string the model class. This will be assigned to the {@link modelClass} property.
* @param array configuration (name=>value) to be applied to this data provider.
* Any public properties of the data provider can be configured via this parameter
*/
public function __construct($modelClass,$config=array())
{
$this->modelClass=$modelClass;
$this->setId($modelClass);
foreach($config as $key=>$value)
$this->$key=$value;
}

/**
* @return CDbCriteria the query criteria
*/
public function getCriteria()
{
if($this->_criteria===null)
$this->_criteria=new CDbCriteria;
return $this->_criteria;
}

/**
* @param mixed the query criteria. This can be either a CDbCriteria object or an array
* representing the query criteria.
*/
public function setCriteria($value)
{
$this->_criteria=$value instanceof CDbCriteria ? $value : new CDbCriteria($value);
}

/**
* Fetches the data from the persistent data storage.
* @return array list of data items
*/
protected function fetchData()
{
$criteria=clone $this->getCriteria();
if(($pagination=$this->getPagination())!==false)
{
$pagination->setItemCount($this->getTotalItemCount(true));
$pagination->applyLimit($criteria);
}
if(($sort=$this->getSort())!==false)
{
$sort->modelClass=$this->modelClass;
$sort->applyOrder($criteria);
}

return CActiveRecord::model($this->modelClass)->findAll($criteria);
}

/**
* Fetches the data item keys from the persistent data storage.
* @return array list of data item keys.
*/
protected function fetchKeys()
{
$keys=array();
if($this->keyAttribute===null)
{
foreach($this->getData() as $i=>$data)
$keys[$i]=$data->getPrimaryKey();
}
else
{
foreach($this->getData() as $i=>$data)
$keys[$i]=$data->{$this->keyAttribute};
}
return $keys;
}

/**
* Calculates the total number of data items.
* @return integer the total number of data items.
*/
protected function calculateTotalItemCount()
{
return CActiveRecord::model($this->modelClass)->count($this->getCriteria());
}
}
Something went wrong with that request. Please try again.