forked from mdmsoft/yii2-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelHelper.php
68 lines (63 loc) · 2.59 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
<?php
namespace mdm\widgets;
use yii\base\Model;
/**
* 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;
}
}