diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index c9897e2..9601969 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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) diff --git a/framework/base/Model.php b/framework/base/Model.php index 048baf5..e1a2818 100644 --- a/framework/base/Model.php +++ b/framework/base/Model.php @@ -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) { - return 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; } }