|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\ActiveRecord; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Connection\ConnectionInterface; |
|
8
|
|
|
use Yiisoft\Db\Data\DataProvider; |
|
9
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
10
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function array_keys; |
|
13
|
|
|
use function call_user_func; |
|
14
|
|
|
use function count; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* ActiveDataProvider implements a data provider based on {@see \Yiisoft\Db\Query\Query} and |
|
19
|
|
|
* {@see \Yiisoft\ActiveRecord\ActiveQuery}. |
|
20
|
|
|
* |
|
21
|
|
|
* ActiveDataProvider provides data by performing DB queries using {@see query}. |
|
22
|
|
|
* |
|
23
|
|
|
* The following is an example of using ActiveDataProvider to provide ActiveRecord instances: |
|
24
|
|
|
* |
|
25
|
|
|
* ```php |
|
26
|
|
|
* $provider = new ActiveDataProvider( |
|
27
|
|
|
* $db, // connection db |
|
28
|
|
|
* Order::find()->orderBy('id') |
|
29
|
|
|
* ); |
|
30
|
|
|
* ``` |
|
31
|
|
|
* |
|
32
|
|
|
* And the following example shows how to use ActiveDataProvider without ActiveRecord: |
|
33
|
|
|
* |
|
34
|
|
|
* ```php |
|
35
|
|
|
* $query = new Query($db); |
|
36
|
|
|
* |
|
37
|
|
|
* $provider = new ActiveDataProvider( |
|
38
|
|
|
* $db, // connection db |
|
39
|
|
|
* $query->from('order')->orderBy('id') |
|
40
|
|
|
* ); |
|
41
|
|
|
* ``` |
|
42
|
|
|
* |
|
43
|
|
|
* For more details and usage information on ActiveDataProvider, see the |
|
44
|
|
|
* [guide article on data providers](guide:output-data-providers). |
|
45
|
|
|
*/ |
|
46
|
|
|
final class ActiveDataProvider extends DataProvider |
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @var QueryInterface|null the query that is used to fetch data models and {@see totalCount} |
|
50
|
|
|
* |
|
51
|
|
|
* if it is not explicitly set. |
|
52
|
|
|
*/ |
|
53
|
|
|
private ?QueryInterface $query = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string|callable the column that is used as the key of the data models. |
|
57
|
|
|
* |
|
58
|
|
|
* This can be either a column name, or a callable that returns the key value of a given data model. |
|
59
|
|
|
* |
|
60
|
|
|
* If this is not set, the following rules will be used to determine the keys of the data models: |
|
61
|
|
|
* |
|
62
|
|
|
* - If {@see query} is an {@see \Yiisoft\ActiveRecord\ActiveQuery} instance, the primary keys of |
|
63
|
|
|
* {@see \Yiisoft\ActiveRecord\ActiveQuery::modelClass} will be used. |
|
64
|
|
|
* |
|
65
|
|
|
* - Otherwise, the keys of the {@see models} array will be used. |
|
66
|
|
|
* |
|
67
|
|
|
* @see getKeys() |
|
68
|
|
|
*/ |
|
69
|
|
|
private $key; |
|
70
|
|
|
|
|
71
|
20 |
|
public function __construct(ConnectionInterface $db, QueryInterface $query) |
|
72
|
|
|
{ |
|
73
|
20 |
|
$this->db = $db; |
|
|
|
|
|
|
74
|
20 |
|
$this->query = $query; |
|
75
|
|
|
|
|
76
|
20 |
|
parent::__construct(); |
|
77
|
20 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function prepareActiveRecord(): array |
|
80
|
|
|
{ |
|
81
|
|
|
$query = $this->prepareQuery(); |
|
82
|
|
|
|
|
83
|
|
|
if ($query->shouldEmulateExecution()) { |
|
|
|
|
|
|
84
|
|
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $query->all($this->db); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Prepares the sql-query that will get the data for current page. |
|
92
|
|
|
* |
|
93
|
|
|
* @throws InvalidConfigException |
|
94
|
|
|
* |
|
95
|
|
|
* @return QueryInterface |
|
96
|
|
|
*/ |
|
97
|
|
|
public function prepareQuery(): QueryInterface |
|
98
|
|
|
{ |
|
99
|
|
|
if (!$this->query instanceof QueryInterface) { |
|
100
|
|
|
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. Yiisoft\Db\Query or its subclasses.'); |
|
101
|
|
|
} |
|
102
|
|
|
$query = clone $this->query; |
|
103
|
|
|
if (($pagination = $this->getPagination()) !== false) { |
|
|
|
|
|
|
104
|
|
|
$pagination->totalCount = $this->getTotalCount(); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
if ($pagination->totalCount === 0) { |
|
107
|
|
|
$query->emulateExecution(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (($sort = $this->getSort()) !== null) { |
|
114
|
|
|
$query->addOrderBy($sort->getOrders()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $query; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Prepares the keys associated with the currently available data models. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $models the available data models. |
|
124
|
|
|
* |
|
125
|
|
|
* @return array the keys. |
|
126
|
|
|
*/ |
|
127
|
20 |
|
protected function prepareKeys(array $models = []): array |
|
128
|
|
|
{ |
|
129
|
20 |
|
$keys = []; |
|
130
|
|
|
|
|
131
|
20 |
|
if ($this->key !== null) { |
|
132
|
|
|
foreach ($models as $model) { |
|
133
|
|
|
if (is_string($this->key)) { |
|
134
|
|
|
$keys[] = $model[$this->key]; |
|
135
|
|
|
} else { |
|
136
|
|
|
$keys[] = call_user_func($this->key, $model); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $keys; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
20 |
|
if ($this->query instanceof ActiveQueryInterface) { |
|
144
|
|
|
/* @var $class ActiveRecordInterface */ |
|
145
|
16 |
|
$class = $this->query->getModelClass(); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
16 |
|
$pks = $class::primaryKey(); |
|
148
|
|
|
|
|
149
|
16 |
|
if (count($pks) === 1) { |
|
150
|
16 |
|
$pk = $pks[0]; |
|
151
|
16 |
|
foreach ($models as $model) { |
|
152
|
16 |
|
$keys[] = $model[$pk]; |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
foreach ($models as $model) { |
|
156
|
|
|
$kk = []; |
|
157
|
|
|
foreach ($pks as $pk) { |
|
158
|
|
|
$kk[$pk] = $model[$pk]; |
|
159
|
|
|
} |
|
160
|
|
|
$keys[] = $kk; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
16 |
|
return $keys; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
4 |
|
return array_keys($models); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Prepares the data models that will be made available in the current page. |
|
172
|
|
|
* |
|
173
|
|
|
* @throws InvalidConfigException |
|
174
|
|
|
* |
|
175
|
|
|
* @return array the available data models. |
|
176
|
|
|
*/ |
|
177
|
20 |
|
protected function prepareModels(): array |
|
178
|
|
|
{ |
|
179
|
20 |
|
if (!$this->query instanceof QueryInterface) { |
|
180
|
|
|
throw new InvalidConfigException( |
|
181
|
|
|
'The "query" property must be an instance of a class that implements the QueryInterface e.g.' |
|
182
|
|
|
. '\Yiisoft\Db\Query\Query or its subclasses.' |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
20 |
|
$query = clone $this->query; |
|
187
|
|
|
|
|
188
|
20 |
|
if (($pagination = $this->getPagination()) !== null) { |
|
189
|
|
|
$pagination->totalCount = $this->getTotalCount(); |
|
|
|
|
|
|
190
|
|
|
if ($pagination->totalCount === 0) { |
|
191
|
|
|
return []; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
20 |
|
if (($sort = $this->getSort()) !== null) { |
|
198
|
|
|
$query->addOrderBy($sort->getOrders()); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
20 |
|
return $query->all(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Returns a value indicating the total number of data models in this data provider. |
|
206
|
|
|
* |
|
207
|
|
|
* @throws InvalidConfigException |
|
208
|
|
|
* |
|
209
|
|
|
* @return int total number of data models in this data provider. |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function prepareTotalCount(): int |
|
212
|
|
|
{ |
|
213
|
|
|
if (!$this->query instanceof QueryInterface) { |
|
214
|
|
|
throw new InvalidConfigException( |
|
215
|
|
|
'The "query" property must be an instance of a class that implements the QueryInterface e.g. ' |
|
216
|
|
|
. '\Yiisoft\Db\Query\Query or its subclasses.' |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$query = clone $this->query; |
|
221
|
|
|
|
|
222
|
|
|
return (int) $query->limit(-1)->offset(-1)->orderBy([])->count('*'); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|