/
CActiveDataProvider.php
232 lines (216 loc) · 6.46 KB
/
CActiveDataProvider.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* CActiveDataProvider class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link https://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
/**
* 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.
*
* CActiveDataProvider may be used in the following way:
* <pre>
* $dataProvider=new CActiveDataProvider('Post', array(
* 'criteria'=>array(
* 'condition'=>'status=1',
* 'order'=>'create_time DESC',
* 'with'=>array('author'),
* ),
* 'countCriteria'=>array(
* 'condition'=>'status=1',
* // 'order' and 'with' clauses have no meaning for the count query
* ),
* 'pagination'=>array(
* 'pageSize'=>20,
* ),
* ));
* // $dataProvider->getData() will return a list of Post objects
* </pre>
*
* @property CDbCriteria $criteria The query criteria.
* @property CDbCriteria $countCriteria The count query criteria. This property is available
* since 1.1.14
* @property CSort $sort The sorting object. If this is false, it means the sorting is disabled.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @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 CActiveRecord the AR finder instance (eg <code>Post::model()</code>).
* This property can be set by passing the finder instance as the first parameter
* to the constructor. For example, <code>Post::model()->published()</code>.
* @since 1.1.3
*/
public $model;
/**
* @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;
/**
* @var CDbCriteria
*/
private $_criteria;
/**
* @var CDbCriteria
*/
private $_countCriteria;
/**
* Constructor.
* @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance
* (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>).
* @param array $config configuration (name=>value) to be applied as the initial property values of this class.
*/
public function __construct($modelClass,$config=array())
{
if(is_string($modelClass))
{
$this->modelClass=$modelClass;
$this->model=$this->getModel($this->modelClass);
}
elseif($modelClass instanceof CActiveRecord)
{
$this->modelClass=get_class($modelClass);
$this->model=$modelClass;
}
$this->setId(CHtml::modelName($this->model));
foreach($config as $key=>$value)
$this->$key=$value;
}
/**
* Returns the query criteria.
* @return CDbCriteria the query criteria
*/
public function getCriteria()
{
if($this->_criteria===null)
$this->_criteria=new CDbCriteria;
return $this->_criteria;
}
/**
* Sets the query criteria.
* @param CDbCriteria|array $value 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);
}
/**
* Returns the count query criteria.
* @return CDbCriteria the count query criteria.
* @since 1.1.14
*/
public function getCountCriteria()
{
if($this->_countCriteria===null)
return $this->getCriteria();
return $this->_countCriteria;
}
/**
* Sets the count query criteria.
* @param CDbCriteria|array $value the count query criteria. This can be either a CDbCriteria object
* or an array representing the query criteria.
* @since 1.1.14
*/
public function setCountCriteria($value)
{
$this->_countCriteria=$value instanceof CDbCriteria ? $value : new CDbCriteria($value);
}
/**
* Returns the sorting object.
* @param string $className the sorting object class name. Parameter is available since version 1.1.13.
* @return CSort the sorting object. If this is false, it means the sorting is disabled.
*/
public function getSort($className='CSort')
{
if(($sort=parent::getSort($className))!==false)
$sort->modelClass=$this->modelClass;
return $sort;
}
/**
* Given active record class name returns new model instance.
*
* @param string $className active record class name.
* @return CActiveRecord active record model instance.
*
* @since 1.1.14
*/
protected function getModel($className)
{
return CActiveRecord::model($className);
}
/**
* 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());
$pagination->applyLimit($criteria);
}
$baseCriteria=$this->model->getDbCriteria(false);
if(($sort=$this->getSort())!==false)
{
// set model criteria so that CSort can use its table alias setting
if($baseCriteria!==null)
{
$c=clone $baseCriteria;
$c->mergeWith($criteria);
$this->model->setDbCriteria($c);
}
else
$this->model->setDbCriteria($criteria);
$sort->applyOrder($criteria);
}
$this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
$data=$this->model->findAll($criteria);
$this->model->setDbCriteria($baseCriteria); // restore original criteria
return $data;
}
/**
* Fetches the data item keys from the persistent data storage.
* @return array list of data item keys.
*/
protected function fetchKeys()
{
$keys=array();
foreach($this->getData() as $i=>$data)
{
$key=$this->keyAttribute===null ? $data->getPrimaryKey() : $data->{$this->keyAttribute};
$keys[$i]=is_array($key) ? implode(',',$key) : $key;
}
return $keys;
}
/**
* Calculates the total number of data items.
* @return integer the total number of data items.
*/
protected function calculateTotalItemCount()
{
$baseCriteria=$this->model->getDbCriteria(false);
if($baseCriteria!==null)
$baseCriteria=clone $baseCriteria;
$count=$this->model->count($this->getCountCriteria());
$this->model->setDbCriteria($baseCriteria);
return $count;
}
}