Commit fe546cd2 by Qiang Xue

Fixes #5688: Added optional `$formName` to `Model::loadMultiple()` to support…

Fixes #5688: Added optional `$formName` to `Model::loadMultiple()` to support customizing form name directly
parent 6ada8737
......@@ -32,6 +32,7 @@ Yii Framework 2 Change Log
- Enh #5600: Allow configuring debug panels in `yii\debug\Module::panels` as panel class name strings (qiangxue)
- Enh #5613: Added `--overwrite` option to Gii console command to support overwriting all files (motin, qiangxue)
- Enh #5646: Call `yii\base\ErrorHandler::unregister()` instead of `restore_*_handlers` directly (aivus)
- Enh #5688: Added optional `$formName` to `Model::loadMultiple()` to support customizing form name directly (qiangxue)
- Enh #5735: Added `yii\bootstrap\Tabs::renderTabContent` to support manually rendering tab contents (RomeroMsk)
- Enh #5770: Added more PHP error names for `ErrorException` (mongosoft)
- Enh #5806: Allow `Html::encode()` to be used when the application is not started (qiangxue)
......
......@@ -763,25 +763,31 @@ class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayab
* @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.
* @param string $formName the form name to be used for loading the data into the models.
* If not set, it will use the [[formName()]] value of the first model in `$models`.
* @return boolean whether at least one of the models is successfully populated.
*/
public static function loadMultiple($models, $data)
public static function loadMultiple($models, $data, $formName = null)
{
/* @var $model Model */
$model = reset($models);
if ($model === false) {
if ($formName === null) {
/* @var $first Model */
$first = reset($models);
if ($first === false) {
return false;
}
$formName = $first->formName();
}
$success = false;
$scope = $model->formName();
foreach ($models as $i => $model) {
if ($scope == '') {
if (isset($data[$i])) {
$model->setAttributes($data[$i]);
/* @var $model Model */
if ($formName == '') {
if (!empty($data[$i])) {
$model->load($data[$i], '');
$success = true;
}
} elseif (isset($data[$scope][$i])) {
$model->setAttributes($data[$scope][$i]);
} elseif (!empty($data[$formName][$i])) {
$model->load($data[$formName][$i], '');
$success = true;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment