-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ModelCollection.php
221 lines (196 loc) · 5.8 KB
/
ModelCollection.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
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\collection;
use yii\base\Arrayable;
use yii\base\InvalidCallException;
use yii\db\ActiveQuery;
use yii\db\ActiveRecordInterface;
use yii\db\BaseActiveRecord;
use yii\helpers\Json;
/**
* ModelCollection
*
* @author Carsten Brandt <mail@cebe.cc>
* @since 1.0
*/
class ModelCollection extends Collection
{
/**
* @var ActiveQuery|null the query that returned this collection.
* May be`null` if the collection has not been created by a query.
*/
public $query;
/**
* @var array|BaseActiveRecord[]
*/
private $_models;
/**
* Collection constructor.
* @param array $models
* @param array $config
*/
public function __construct($models = [], $config = [])
{
$this->_models = $models;
parent::__construct([], $config);
}
/**
* Lazy evaluation of models, if this collection has been created from a query.
*/
public function getData()
{
$this->ensureModels();
return (new Collection($this->_models))->getData();
}
private function queryAll()
{
if ($this->query === null) {
throw new InvalidCallException('This collection was not created from a query.');
}
return $this->query->all();
}
private function ensureModels()
{
if ($this->_models === null) {
$this->_models = $this->queryAll();
}
}
/**
* @return array|BaseActiveRecord[]|ActiveRecordInterface[]|Arrayable[] models contained in this collection.
*/
public function getModels()
{
return $this->getData();
}
// TODO relational operations like link() and unlink() sync()
// https://github.com/yiisoft/yii2/pull/12304#issuecomment-242339800
// https://github.com/yiisoft/yii2/issues/10806#issuecomment-242346294
// TODO addToRelation() by checking if query is a relation
// https://github.com/yiisoft/yii2/issues/10806#issuecomment-241505294
// https://github.com/yiisoft/yii2/issues/12743
public function findWith($with)
{
if (!$this->query) {
throw new InvalidCallException('This collection was not created from a query, so findWith() is not possible.');
}
$this->ensureModels();
$this->query->findWith($with, $this->_models);
return $this;
}
// AR specific stuff
/**
* https://github.com/yiisoft/yii2/issues/13921
*
* TODO add transaction support
*/
public function deleteAll()
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->delete();
}
}
public function scenario($scenario)
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->scenario = $scenario;
}
return $this;
}
/**
* https://github.com/yiisoft/yii2/issues/13921
*
* TODO add transaction support
* @param array $attributes
* @param bool $safeOnly
* @param bool $runValidation
* @return ModelCollection
* @throws \yii\db\Exception
* @throws \yii\db\StaleObjectException
*/
public function updateAll($attributes, $safeOnly = true, $runValidation = true)
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->setAttributes($attributes, $safeOnly);
$model->update($runValidation, array_keys($attributes));
}
return $this;
}
/**
* @param array $attributes
* @param bool $safeOnly
* @param bool $runValidation
* @return $this
*/
public function insertAll($attributes, $safeOnly = true, $runValidation = true)
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->setAttributes($attributes, $safeOnly);
$model->insert($runValidation, array_keys($attributes));
}
// TODO could be a batch insert
return $this;
}
public function fillAll($attributes, $safeOnly = true)
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->setAttributes($attributes, $safeOnly);
}
return $this;
}
public function saveAll($runValidation = true, $attributeNames = null)
{
$this->ensureModels();
foreach ($this->_models as $model) {
$model->save($runValidation, $attributeNames);
}
return $this;
}
/**
* https://github.com/yiisoft/yii2/issues/10806#issuecomment-242119472
*
* @return bool
*/
public function validateAll()
{
$this->ensureModels();
$success = true;
foreach ($this->_models as $model) {
if (!$model->validate()) {
$success = false;
}
}
return $success;
}
/**
* @param array $fields
* @param array $expand
* @param bool $recursive
* @return Collection|static
*/
public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
return $this->map(function ($model) use ($fields, $expand, $recursive) {
/** @var $model Arrayable */
return $model->toArray($fields, $expand, $recursive);
});
}
/**
* Encodes the collected models into a JSON string.
* @param int $options the encoding options. For more details please refer to
* <https://www.php.net/manual/en/function.json-encode.php>. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
* @return string the encoding result.
*/
public function toJson($options = 320)
{
return Json::encode($this->toArray()->_models, $options);
}
}