model->attributes in Yii2 always has NULL value

别说谁变了你拦得住时间么 提交于 2019-12-04 01:56:47
Jason G

If there is no rule for your attribute the $model->load() will ignore those not in the rules of the model.

Add your attributes to the rules function

public function rules()
{
    return [
        ...
        [['attribute_name'], 'type'],
        ...
    ];
}
Amjad Ali Chauhdry

To fetch data for an individually attributes(db-fields) in yii2.0 then you should just do as:

echo $yourModel->getAttribute('email');
Alex

ActiveRecord $attributes is a private property Use $model->getAttribute(string)

You can use following codes:

$model = new _Users();
$model->attributes=Yii::$app->request->post('_Users');
$model->title= $model->title
$model->type = $model->type . ' CYC'; // CYC is static type code
#$model->sampleAttribute='Hello World';
Alexander

You must remove all public properties (title, type, etc.) in your _User model and $model->attributes = $post will work correctly.

I have also encountered the same problem, i Add my attributes to the rules function,but also error. And i found the reason for this problem. It is beause that the submit form's name in corresponding view file is not the same as the model's name which you use in controller

[controller file]:

$model=new SearchForm();

[view file]:

<input name="SearchForm[attribus]" ...

or 

[view file]:

<?= $form->field($model,'atrribus')->textInput()?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!