yii\base\Model::loadMultiple PHP Method

loadMultiple() public static method

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 Model::formName, and index the index of the model in the $models array. If Model::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 Model::setAttributes.
public static loadMultiple ( array $models, array $data, string $formName = null ) : boolean
$models array the models to be populated. Note that all models should have the same class.
$data array the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array supplied by end user.
$formName string 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`. This parameter is available since version 2.0.1.
return boolean whether at least one of the models is successfully populated.
    public static function loadMultiple($models, $data, $formName = null)
    {
        if ($formName === null) {
            /* @var $first Model */
            $first = reset($models);
            if ($first === false) {
                return false;
            }
            $formName = $first->formName();
        }
        $success = false;
        foreach ($models as $i => $model) {
            /* @var $model Model */
            if ($formName == '') {
                if (!empty($data[$i])) {
                    $model->load($data[$i], '');
                    $success = true;
                }
            } elseif (!empty($data[$formName][$i])) {
                $model->load($data[$formName][$i], '');
                $success = true;
            }
        }
        return $success;
    }

Usage Example

Example #1
2
 /**
  * Anlegen eines Benutzers
  * 
  * @return \yii\web\View
  * @author KAS <*****@*****.**> 28.07.2015
  */
 public function actionCreate()
 {
     Yii::$app->view->params['headline'] = 'Benutzer anlegen';
     $model = new User();
     //----------------------------------------------------------------------
     $post = \Yii::$app->request->post();
     if ($model->load($post)) {
         $authArr = [];
         foreach ($post['Auth'] as $authData) {
             $authArr[] = new Auth($authData);
         }
         // Daten Validieren und Zuordnen -----------------------------------
         if (Model::loadMultiple($authArr, $post) && Model::validateMultiple($authArr)) {
             // aus den Auth Objekten machen wir arrays,
             // damit wir das in die Mongo speichern können
             $model->auth = array_map(function ($a) {
                 return $a->toArray();
             }, $authArr);
             // Speichern ---------------------------------------------------
             $model->save();
             // Benutzer benachrichtigen ------------------------------------
             \Yii::$app->session->setFlash('success', 'Benutzer wurde erfolgreich angelegt!', TRUE);
             // Neue Daten laden, da wir in den Models Veränderungen vornehmen
             $model->refresh();
         }
     }
     // Defaultwerte festlegen ----------------------------------------------
     $model->created_at = new \MongoDate();
     $model->updated_at = new \MongoDate();
     $model->role = "Normal";
     //----------------------------------------------------------------------
     return $this->render('create', ['model' => $model]);
 }
All Usage Examples Of yii\base\Model::loadMultiple