-
Notifications
You must be signed in to change notification settings - Fork 7
/
ModelHelper.php
195 lines (186 loc) · 7.38 KB
/
ModelHelper.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
<?php
namespace mdm\widgets;
use ReflectionClass;
use Yii;
use yii\base\Model;
use yii\db\BaseActiveRecord;
use yii\helpers\Html;
/**
* Description of ModelHelper
*
* @author Misbahul D Munir <[email protected]>
* @since 1.0
*/
class ModelHelper
{
/**
* Populates a set of models with the data from end user.
* This method is mainly used to collect tabular data input.
* The data to be loaded for each model is `$data[formName][index]`, where `formName`
* refers to the sort name of model class, and `index` the index of the model in the `$data` array.
* If `$formName` is empty, `$data[index]` will be used to populate each model.
* The data being populated to each model is subject to the safety check by [[setAttributes()]].
* @param string $class Model class name.
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
* supplied by end user.
* @param string $formName the form name to be used for loading the data into the models.
* If not set, it will use the sort name of called class.
* @param Model[] $origin original models to be populated. It will be check using `$keys` with supplied data.
* If same then will be used for result model.
* @param array $options Option to model
* - scenario for model.
* - arguments The parameters to be passed to the class constructor as an array.
* @return boolean|Model[] whether at least one of the models is successfully populated.
*/
public static function createMultiple($class, $data, $formName = null, &$origin = [], $options = [])
{
$reflector = new ReflectionClass($class);
$args = isset($options['arguments']) ? $options['arguments'] : [];
if ($formName === null) {
/* @var $model Model */
$model = empty($args) ? new $class() : $reflector->newInstanceArgs($args);
$formName = $model->formName();
}
if ($formName != '') {
$data = isset($data[$formName]) ? $data[$formName] : null;
}
if ($data === null) {
return false;
}
$models = [];
foreach ($data as $i => $row) {
$model = null;
if (isset($origin[$i])) {
$model = $origin[$i];
unset($origin[$i]);
} else {
$model = empty($args) ? new $class() : $reflector->newInstanceArgs($args);
}
if (isset($options['scenario'])) {
$model->scenario = $options['scenario'];
}
$model->load($row, '');
$models[$i] = $model;
}
return $models;
}
/**
* Populates a set of models with the data from end user.
* This method is mainly used to collect tabular data input.
* The data to be loaded for each model is `$data[formName][index]`, where `formName`
* refers to the sort name of model class, and `index` the index of the model in the `$data` array.
* If `$formName` is empty, `$data[index]` will be used to populate each model.
* The data being populated to each model is subject to the safety check by [[setAttributes()]].
* @param string $class Model class name.
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
* supplied by end user.
* @param string $formName the form name to be used for loading the data into the models.
* If not set, it will use the sort name of called class.
* @param Model[] $origin original models to be populated. It will be check using `$keys` with supplied data.
* If same then will be used for result model.
* @param array $config Option to model
* @return boolean|Model[] whether at least one of the models is successfully populated.
*/
public static function loadMultiple($class, $data, $formName = null, &$origin = [], $config = [])
{
$config['class'] = $class;
if ($formName === null) {
/* @var $model Model */
$model = Yii::createObject($config);
$formName = $model->formName();
}
if ($formName != '') {
$data = isset($data[$formName]) ? $data[$formName] : null;
}
if ($data === null) {
return [];
}
$models = [];
foreach ($data as $i => $row) {
$model = null;
if (isset($origin[$i])) {
$model = $origin[$i];
unset($origin[$i]);
} else {
$model = Yii::createObject($config);
}
$model->load($row, '');
$models[$i] = $model;
}
return $models;
}
/**
*
* @param Model[] $models
* @param array $options
* @param array $messages
* @return boolean
*/
public static function validateMultiple(array $models, $options = [], &$messages = [])
{
/* @var $model Model */
$validateAll = !empty($options['validateAll']);
/* @var $model BaseActiveRecord */
if (isset($options['values'])) {
foreach ($models as $model) {
Yii::configure($model, $options['values']);
}
}
$result = true;
foreach ($models as $i => $model) {
$oke = !isset($options['beforeValidate']) || call_user_func($options['beforeValidate'], $model) !== false;
if ($oke && $model->validate()) {
isset($options['afterValidate']) && call_user_func($options['afterValidate'], $model);
} else {
foreach ($model->getErrors() as $attribute => $message) {
$messages[Html::getInputId($model, "[$i]$attribute")] = $message;
}
if ($validateAll) {
$result = false;
} else {
return false;
}
}
}
return $result;
}
/**
*
* @param BaseActiveRecord[] $models
* @param array $options
* - runValidation boolean default true.
* - beforeSave callable
* - afterSave callable
* - deleteUnsaved boolean default true.
* @param array $messages
* @return boolean
*/
public static function saveMultiple(array $models, $options = [], &$messages = [])
{
$runValidation = !isset($options['runValidation']) || $options['runValidation'];
/* @var $model BaseActiveRecord */
if (isset($options['values'])) {
foreach ($models as $model) {
Yii::configure($model, $options['values']);
}
}
unset($options['values']);
if (!$runValidation || static::validateMultiple($models, $options, $messages)) {
foreach ($models as $model) {
if (!isset($options['beforeSave']) || call_user_func($options['beforeSave'], $model) !== false) {
if ($model->save(false)) {
isset($options['afterSave']) && call_user_func($options['afterSave'], $model);
} else {
return false;
}
} elseif (!isset($options['deleteUnsaved']) || $options['deleteUnsaved']) {
if (!$model->isNewRecord) {
$model->delete();
}
}
}
return true;
}
return false;
}
}