yii\base\Model::load PHP Метод

load() публичный Метод

This method provides a convenient shortcut for: php if (isset($_POST['FormName'])) { $model->attributes = $_POST['FormName']; if ($model->save()) { handle success } } which, with load() can be written as: php if ($model->load($_POST) && $model->save()) { handle success } load() gets the 'FormName' from the model's Model::formName method (which you may override), unless the $formName parameter is given. If the form name is empty, load() populates the model with the whole of $data, instead of $data['FormName']. Note, that the data being populated is subject to the safety check by Model::setAttributes.
public load ( array $data, string $formName = null ) : boolean
$data array the data array to load, typically `$_POST` or `$_GET`.
$formName string the form name to use to load the data into the model. If not set, [[formName()]] is used.
Результат boolean whether `load()` found the expected form in `$data`.
    public function load($data, $formName = null)
    {
        $scope = $formName === null ? $this->formName() : $formName;
        if ($scope === '' && !empty($data)) {
            $this->setAttributes($data);
            return true;
        } elseif (isset($data[$scope])) {
            $this->setAttributes($data[$scope]);
            return true;
        } else {
            return false;
        }
    }

Usage Example

Пример #1
3
 /**
  * Performs ajax validation.
  *
  * @param \yii\base\Model $model
  *
  * @throws \yii\base\ExitException
  */
 protected function performValidationModel(Model $model)
 {
     if ($model->load(Yii::$app->request->post())) {
         return ActiveForm::validate($model);
     }
     return;
 }
All Usage Examples Of yii\base\Model::load