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 ...@@ -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 #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 #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 #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 #5735: Added `yii\bootstrap\Tabs::renderTabContent` to support manually rendering tab contents (RomeroMsk)
- Enh #5770: Added more PHP error names for `ErrorException` (mongosoft) - 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) - 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 ...@@ -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 $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 * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
* supplied by end user. * 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 */ if ($formName === null) {
$model = reset($models); /* @var $first Model */
if ($model === false) { $first = reset($models);
return false; if ($first === false) {
return false;
}
$formName = $first->formName();
} }
$success = false; $success = false;
$scope = $model->formName();
foreach ($models as $i => $model) { foreach ($models as $i => $model) {
if ($scope == '') { /* @var $model Model */
if (isset($data[$i])) { if ($formName == '') {
$model->setAttributes($data[$i]); if (!empty($data[$i])) {
$model->load($data[$i], '');
$success = true; $success = true;
} }
} elseif (isset($data[$scope][$i])) { } elseif (!empty($data[$formName][$i])) {
$model->setAttributes($data[$scope][$i]); $model->load($data[$formName][$i], '');
$success = true; $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