1 | <?php |
||
2 | /** |
||
3 | * @link http://www.yiiframework.com/ |
||
4 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
5 | * @license http://www.yiiframework.com/license/ |
||
6 | */ |
||
7 | |||
8 | namespace yii\data; |
||
9 | |||
10 | use yii\helpers\ArrayHelper; |
||
11 | |||
12 | /** |
||
13 | * ArrayDataProvider implements a data provider based on a data array. |
||
14 | * |
||
15 | * The [[allModels]] property contains all data models that may be sorted and/or paginated. |
||
16 | * ArrayDataProvider will provide the data after sorting and/or pagination. |
||
17 | * You may configure the [[sort]] and [[pagination]] properties to |
||
18 | * customize the sorting and pagination behaviors. |
||
19 | * |
||
20 | * Elements in the [[allModels]] array may be either objects (e.g. model objects) |
||
21 | * or associative arrays (e.g. query results of DAO). |
||
22 | * Make sure to set the [[key]] property to the name of the field that uniquely |
||
23 | * identifies a data record or false if you do not have such a field. |
||
24 | * |
||
25 | * Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient |
||
26 | * because it needs to have [[allModels]] ready. |
||
27 | * |
||
28 | * ArrayDataProvider may be used in the following way: |
||
29 | * |
||
30 | * ```php |
||
31 | * $query = new Query; |
||
32 | * $provider = new ArrayDataProvider([ |
||
33 | * 'allModels' => $query->from('post')->all(), |
||
34 | * 'sort' => [ |
||
35 | * 'attributes' => ['id', 'username', 'email'], |
||
36 | * ], |
||
37 | * 'pagination' => [ |
||
38 | * 'pageSize' => 10, |
||
39 | * ], |
||
40 | * ]); |
||
41 | * // get the posts in the current page |
||
42 | * $posts = $provider->getModels(); |
||
43 | * ``` |
||
44 | * |
||
45 | * Note: if you want to use the sorting feature, you must configure the [[sort]] property |
||
46 | * so that the provider knows which columns can be sorted. |
||
47 | * |
||
48 | * For more details and usage information on ArrayDataProvider, see the [guide article on data providers](guide:output-data-providers). |
||
49 | * |
||
50 | * @author Qiang Xue <[email protected]> |
||
51 | * @since 2.0 |
||
52 | */ |
||
53 | class ArrayDataProvider extends BaseDataProvider |
||
54 | { |
||
55 | /** |
||
56 | * @var string|callable the column that is used as the key of the data models. |
||
57 | * This can be either a column name, or a callable that returns the key value of a given data model. |
||
58 | * If this is not set, the index of the [[models]] array will be used. |
||
59 | * @see getKeys() |
||
60 | */ |
||
61 | public $key; |
||
62 | /** |
||
63 | * @var array the data that is not paginated or sorted. When pagination is enabled, |
||
64 | * this property usually contains more elements than [[models]]. |
||
65 | * The array elements must use zero-based integer keys. |
||
66 | */ |
||
67 | public $allModels; |
||
68 | /** |
||
69 | * @var string the name of the [[\yii\base\Model|Model]] class that will be represented. |
||
70 | * This property is used to get columns' names. |
||
71 | * @since 2.0.9 |
||
72 | */ |
||
73 | public $modelClass; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 39 | protected function prepareModels() |
|
80 | { |
||
81 | 39 | if (($models = $this->allModels) === null) { |
|
82 | return []; |
||
83 | } |
||
84 | |||
85 | 39 | if (($sort = $this->getSort()) !== false) { |
|
86 | 38 | $models = $this->sortModels($models, $sort); |
|
87 | } |
||
88 | |||
89 | 39 | if (($pagination = $this->getPagination()) !== false) { |
|
90 | 38 | $pagination->totalCount = $this->getTotalCount(); |
|
91 | |||
92 | 38 | if ($pagination->getPageSize() > 0) { |
|
93 | 38 | $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true); |
|
94 | } |
||
95 | } |
||
96 | |||
97 | 39 | return $models; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 39 | protected function prepareKeys($models) |
|
104 | { |
||
105 | 39 | if ($this->key !== null) { |
|
106 | $keys = []; |
||
107 | foreach ($models as $model) { |
||
108 | if (is_string($this->key)) { |
||
109 | $keys[] = $model[$this->key]; |
||
110 | } else { |
||
111 | $keys[] = call_user_func($this->key, $model); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $keys; |
||
116 | } |
||
117 | |||
118 | 39 | return array_keys($models); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 35 | protected function prepareTotalCount() |
|
125 | { |
||
126 | 35 | return is_array($this->allModels) ? count($this->allModels) : 0; |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Sorts the data models according to the given sort definition. |
||
131 | * @param array $models the models to be sorted |
||
132 | * @param Sort $sort the sort definition |
||
133 | * @return array the sorted data models |
||
134 | */ |
||
135 | 38 | protected function sortModels($models, $sort) |
|
136 | { |
||
137 | 38 | $orders = $sort->getOrders(); |
|
138 | 38 | if (!empty($orders)) { |
|
139 | 4 | ArrayHelper::multisort($models, array_keys($orders), array_values($orders), $sort->sortFlags); |
|
140 | } |
||
141 | |||
142 | 38 | return $models; |
|
143 | } |
||
144 | } |
||
145 |