1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\data; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Component; |
12
|
|
|
use yii\base\InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* BaseDataProvider provides a base class that implements the [[DataProviderInterface]]. |
16
|
|
|
* |
17
|
|
|
* For more details and usage information on BaseDataProvider, see the [guide article on data providers](guide:output-data-providers). |
18
|
|
|
* |
19
|
|
|
* @property-read int $count The number of data models in the current page. |
20
|
|
|
* @property array $keys The list of key values corresponding to [[models]]. Each data model in [[models]] is |
21
|
|
|
* uniquely identified by the corresponding key value in this array. |
22
|
|
|
* @property array $models The list of data models in the current page. |
23
|
|
|
* @property Pagination|false $pagination The pagination object. If this is false, it means the pagination is |
24
|
|
|
* disabled. Note that the type of this property differs in getter and setter. See [[getPagination()]] and |
25
|
|
|
* [[setPagination()]] for details. |
26
|
|
|
* @property Sort|bool $sort The sorting object. If this is false, it means the sorting is disabled. Note that |
27
|
|
|
* the type of this property differs in getter and setter. See [[getSort()]] and [[setSort()]] for details. |
28
|
|
|
* @property int $totalCount Total number of possible data models. |
29
|
|
|
* |
30
|
|
|
* @author Qiang Xue <[email protected]> |
31
|
|
|
* @since 2.0 |
32
|
|
|
*/ |
33
|
|
|
abstract class BaseDataProvider extends Component implements DataProviderInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var int Number of data providers on the current page. Used to generate unique IDs. |
37
|
|
|
*/ |
38
|
|
|
private static $counter = 0; |
39
|
|
|
/** |
40
|
|
|
* @var string|null an ID that uniquely identifies the data provider among all data providers. |
41
|
|
|
* Generated automatically the following way in case it is not set: |
42
|
|
|
* |
43
|
|
|
* - First data provider ID is empty. |
44
|
|
|
* - Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc. |
45
|
|
|
*/ |
46
|
|
|
public $id; |
47
|
|
|
|
48
|
|
|
private $_sort; |
49
|
|
|
private $_pagination; |
50
|
|
|
private $_keys; |
51
|
|
|
private $_models; |
52
|
|
|
private $_totalCount; |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
90 |
|
public function init() |
59
|
|
|
{ |
60
|
90 |
|
parent::init(); |
61
|
90 |
|
if ($this->id === null) { |
62
|
90 |
|
if (self::$counter > 0) { |
63
|
90 |
|
$this->id = 'dp-' . self::$counter; |
64
|
|
|
} |
65
|
90 |
|
self::$counter++; |
66
|
|
|
} |
67
|
90 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Prepares the data models that will be made available in the current page. |
71
|
|
|
* @return array the available data models |
72
|
|
|
*/ |
73
|
|
|
abstract protected function prepareModels(); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Prepares the keys associated with the currently available data models. |
77
|
|
|
* @param array $models the available data models |
78
|
|
|
* @return array the keys |
79
|
|
|
*/ |
80
|
|
|
abstract protected function prepareKeys($models); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns a value indicating the total number of data models in this data provider. |
84
|
|
|
* @return int total number of data models in this data provider. |
85
|
|
|
*/ |
86
|
|
|
abstract protected function prepareTotalCount(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Prepares the data models and keys. |
90
|
|
|
* |
91
|
|
|
* This method will prepare the data models and keys that can be retrieved via |
92
|
|
|
* [[getModels()]] and [[getKeys()]]. |
93
|
|
|
* |
94
|
|
|
* This method will be implicitly called by [[getModels()]] and [[getKeys()]] if it has not been called before. |
95
|
|
|
* |
96
|
|
|
* @param bool $forcePrepare whether to force data preparation even if it has been done before. |
97
|
|
|
*/ |
98
|
81 |
|
public function prepare($forcePrepare = false) |
99
|
|
|
{ |
100
|
81 |
|
if ($forcePrepare || $this->_models === null) { |
101
|
79 |
|
$this->_models = $this->prepareModels(); |
102
|
|
|
} |
103
|
81 |
|
if ($forcePrepare || $this->_keys === null) { |
104
|
81 |
|
$this->_keys = $this->prepareKeys($this->_models); |
105
|
|
|
} |
106
|
81 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the data models in the current page. |
110
|
|
|
* @return array the list of data models in the current page. |
111
|
|
|
*/ |
112
|
80 |
|
public function getModels() |
113
|
|
|
{ |
114
|
80 |
|
$this->prepare(); |
115
|
|
|
|
116
|
80 |
|
return $this->_models; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the data models in the current page. |
121
|
|
|
* @param array $models the models in the current page |
122
|
|
|
*/ |
123
|
2 |
|
public function setModels($models) |
124
|
|
|
{ |
125
|
2 |
|
$this->_models = $models; |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the key values associated with the data models. |
130
|
|
|
* @return array the list of key values corresponding to [[models]]. Each data model in [[models]] |
131
|
|
|
* is uniquely identified by the corresponding key value in this array. |
132
|
|
|
*/ |
133
|
30 |
|
public function getKeys() |
134
|
|
|
{ |
135
|
30 |
|
$this->prepare(); |
136
|
|
|
|
137
|
30 |
|
return $this->_keys; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets the key values associated with the data models. |
142
|
|
|
* @param array $keys the list of key values corresponding to [[models]]. |
143
|
|
|
*/ |
144
|
|
|
public function setKeys($keys) |
145
|
|
|
{ |
146
|
|
|
$this->_keys = $keys; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the number of data models in the current page. |
151
|
|
|
* @return int the number of data models in the current page. |
152
|
|
|
*/ |
153
|
25 |
|
public function getCount() |
154
|
|
|
{ |
155
|
25 |
|
return count($this->getModels()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the total number of data models. |
160
|
|
|
* When [[pagination]] is false, this returns the same value as [[count]]. |
161
|
|
|
* Otherwise, it will call [[prepareTotalCount()]] to get the count. |
162
|
|
|
* @return int total number of possible data models. |
163
|
|
|
*/ |
164
|
72 |
|
public function getTotalCount() |
165
|
|
|
{ |
166
|
72 |
|
if ($this->_pagination === false) { |
167
|
|
|
return $this->getCount(); |
168
|
|
|
} |
169
|
72 |
|
if ($this->_totalCount === null) { |
170
|
69 |
|
$this->_totalCount = $this->prepareTotalCount(); |
171
|
|
|
} |
172
|
72 |
|
return $this->_totalCount; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Sets the total number of data models. |
177
|
|
|
* @param int $value the total number of data models. |
178
|
|
|
*/ |
179
|
10 |
|
public function setTotalCount($value) |
180
|
|
|
{ |
181
|
10 |
|
$this->_totalCount = $value; |
182
|
10 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the pagination object used by this data provider. |
186
|
|
|
* Note that you should call [[prepare()]] or [[getModels()]] first to get correct values |
187
|
|
|
* of [[Pagination::totalCount]] and [[Pagination::pageCount]]. |
188
|
|
|
* @return Pagination|false the pagination object. If this is false, it means the pagination is disabled. |
189
|
|
|
*/ |
190
|
84 |
|
public function getPagination() |
191
|
|
|
{ |
192
|
84 |
|
if ($this->_pagination === null) { |
193
|
63 |
|
$this->setPagination([]); |
194
|
|
|
} |
195
|
84 |
|
return $this->_pagination; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Sets the pagination for this data provider. |
200
|
|
|
* @param array|Pagination|bool $value the pagination to be used by this data provider. |
201
|
|
|
* This can be one of the following: |
202
|
|
|
* |
203
|
|
|
* - a configuration array for creating the pagination object. The "class" element defaults |
204
|
|
|
* to 'yii\data\Pagination' |
205
|
|
|
* - an instance of [[Pagination]] or its subclass |
206
|
|
|
* - false, if pagination needs to be disabled. |
207
|
|
|
* |
208
|
|
|
* @throws InvalidArgumentException |
209
|
|
|
*/ |
210
|
72 |
|
public function setPagination($value) |
211
|
|
|
{ |
212
|
72 |
|
if (is_array($value)) { |
213
|
69 |
|
$config = ['class' => Pagination::className()]; |
|
|
|
|
214
|
69 |
|
if ($this->id !== null) { |
215
|
63 |
|
$config['pageParam'] = $this->id . '-page'; |
216
|
63 |
|
$config['pageSizeParam'] = $this->id . '-per-page'; |
217
|
|
|
} |
218
|
69 |
|
$value = Yii::createObject(array_merge($config, $value)); |
219
|
|
|
} |
220
|
72 |
|
if ($value instanceof Pagination) { |
221
|
70 |
|
$value->totalCount = $this->getTotalCount(); |
222
|
70 |
|
$this->_pagination = $value; |
223
|
2 |
|
} elseif ($value === false) { |
224
|
2 |
|
$this->_pagination = false; |
225
|
|
|
} else { |
226
|
|
|
throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.'); |
227
|
|
|
} |
228
|
72 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns the sorting object used by this data provider. |
232
|
|
|
* @return Sort|bool the sorting object. If this is false, it means the sorting is disabled. |
233
|
|
|
*/ |
234
|
84 |
|
public function getSort() |
235
|
|
|
{ |
236
|
84 |
|
if ($this->_sort === null) { |
237
|
60 |
|
$this->setSort([]); |
238
|
|
|
} |
239
|
|
|
|
240
|
84 |
|
return $this->_sort; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Sets the sort definition for this data provider. |
245
|
|
|
* @param array|Sort|bool $value the sort definition to be used by this data provider. |
246
|
|
|
* This can be one of the following: |
247
|
|
|
* |
248
|
|
|
* - a configuration array for creating the sort definition object. The "class" element defaults |
249
|
|
|
* to 'yii\data\Sort' |
250
|
|
|
* - an instance of [[Sort]] or its subclass |
251
|
|
|
* - false, if sorting needs to be disabled. |
252
|
|
|
* |
253
|
|
|
* @throws InvalidArgumentException |
254
|
|
|
*/ |
255
|
83 |
|
public function setSort($value) |
256
|
|
|
{ |
257
|
83 |
|
if (is_array($value)) { |
258
|
81 |
|
$config = ['class' => Sort::className()]; |
|
|
|
|
259
|
81 |
|
if ($this->id !== null) { |
260
|
60 |
|
$config['sortParam'] = $this->id . '-sort'; |
261
|
|
|
} |
262
|
81 |
|
$this->_sort = Yii::createObject(array_merge($config, $value)); |
263
|
2 |
|
} elseif ($value instanceof Sort || $value === false) { |
264
|
2 |
|
$this->_sort = $value; |
265
|
|
|
} else { |
266
|
|
|
throw new InvalidArgumentException('Only Sort instance, configuration array or false is allowed.'); |
267
|
|
|
} |
268
|
83 |
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Refreshes the data provider. |
272
|
|
|
* After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again, |
273
|
|
|
* they will re-execute the query and return the latest data available. |
274
|
|
|
*/ |
275
|
6 |
|
public function refresh() |
276
|
|
|
{ |
277
|
6 |
|
$this->_totalCount = null; |
278
|
6 |
|
$this->_models = null; |
279
|
6 |
|
$this->_keys = null; |
280
|
6 |
|
} |
281
|
|
|
} |
282
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.