|
|
|
@ -639,8 +639,9 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Populates the model with the data from end user. |
|
|
|
|
* The data is subject to the safety check by [[setAttributes()]]. If [[formName()]] is not empty, |
|
|
|
|
* the data indexed by [[formName()]] in `$data` will be used to populate the model. |
|
|
|
|
* The data to be loaded is `$data[formName]`, where `formName` refers to the value of [[formName()]]. |
|
|
|
|
* If [[formName()]] is empty, the whole `$data` array will be used to populate the model. |
|
|
|
|
* The data being populated is subject to the safety check by [[setAttributes()]]. |
|
|
|
|
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array |
|
|
|
|
* supplied by end user. |
|
|
|
|
* @return boolean whether the model is successfully populated with some data. |
|
|
|
@ -660,6 +661,57 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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 value of [[formName()]], and `index` the index of the model in the `$models` 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 array $models the models to be populated. Note that all models should have the same class. |
|
|
|
|
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array |
|
|
|
|
* supplied by end user. |
|
|
|
|
* @return boolean whether the model is successfully populated with some data. |
|
|
|
|
*/ |
|
|
|
|
public static function loadMultiple($models, $data) |
|
|
|
|
{ |
|
|
|
|
/** @var Model $model */ |
|
|
|
|
$model = reset($models); |
|
|
|
|
if ($model === false) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
$success = false; |
|
|
|
|
$scope = $model->formName(); |
|
|
|
|
foreach ($models as $i => $model) { |
|
|
|
|
if ($scope == '') { |
|
|
|
|
if (isset($data[$i])) { |
|
|
|
|
$model->setAttributes($data[$i]); |
|
|
|
|
$success = true; |
|
|
|
|
} |
|
|
|
|
} elseif (isset($data[$scope][$i])) { |
|
|
|
|
$model->setAttributes($data[$scope[$i]]); |
|
|
|
|
$success = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return $success; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Validates multiple models. |
|
|
|
|
* @param array $models the models to be validated |
|
|
|
|
* @return boolean whether all models are valid. False will be returned if one |
|
|
|
|
* or multiple models have validation error. |
|
|
|
|
*/ |
|
|
|
|
public static function validateMultiple($models) |
|
|
|
|
{ |
|
|
|
|
$valid = true; |
|
|
|
|
/** @var Model $model */ |
|
|
|
|
foreach ($models as $model) { |
|
|
|
|
$valid = $model->validate() && $valid; |
|
|
|
|
} |
|
|
|
|
return $valid; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Converts the object into an array. |
|
|
|
|
* The default implementation will return [[attributes]]. |
|
|
|
|
* @return array the array representation of the object |
|
|
|
|