1 | <?php |
||
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 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 | 61 | public function init() |
|
59 | { |
||
60 | 61 | parent::init(); |
|
61 | 61 | if ($this->id === null) { |
|
62 | 61 | if (self::$counter > 0) { |
|
63 | 61 | $this->id = 'dp-' . self::$counter; |
|
64 | } |
||
65 | 61 | self::$counter++; |
|
66 | } |
||
67 | 61 | } |
|
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 | 50 | public function prepare($forcePrepare = false) |
|
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 | 50 | public function getModels() |
|
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) |
|
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 | 26 | public function getKeys() |
|
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) |
||
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 | 18 | public function getCount() |
|
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 | 50 | public function getTotalCount() |
|
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) |
|
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 | 50 | public function getPagination() |
|
198 | |||
199 | /** |
||
200 | * Sets the pagination for this data provider. |
||
201 | * @param array|Pagination|bool $value the pagination to be used by this data provider. |
||
202 | * This can be one of the following: |
||
203 | * |
||
204 | * - a configuration array for creating the pagination object. The "class" element defaults |
||
205 | * to 'yii\data\Pagination' |
||
206 | * - an instance of [[Pagination]] or its subclass |
||
207 | * - false, if pagination needs to be disabled. |
||
208 | * |
||
209 | * @throws InvalidArgumentException |
||
210 | */ |
||
211 | 52 | public function setPagination($value) |
|
226 | |||
227 | /** |
||
228 | * Returns the sorting object used by this data provider. |
||
229 | * @return Sort|bool the sorting object. If this is false, it means the sorting is disabled. |
||
230 | */ |
||
231 | 47 | public function getSort() |
|
239 | |||
240 | /** |
||
241 | * Sets the sort definition for this data provider. |
||
242 | * @param array|Sort|bool $value the sort definition to be used by this data provider. |
||
243 | * This can be one of the following: |
||
244 | * |
||
245 | * - a configuration array for creating the sort definition object. The "class" element defaults |
||
246 | * to 'yii\data\Sort' |
||
247 | * - an instance of [[Sort]] or its subclass |
||
248 | * - false, if sorting needs to be disabled. |
||
249 | * |
||
250 | * @throws InvalidArgumentException |
||
251 | */ |
||
252 | 48 | public function setSort($value) |
|
266 | |||
267 | /** |
||
268 | * Refreshes the data provider. |
||
269 | * After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again, |
||
270 | * they will re-execute the query and return the latest data available. |
||
271 | */ |
||
272 | public function refresh() |
||
278 | } |
||
279 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.