I have one temporary model as viewModel. In my CRUD actions (for example actionCreate) I want to get this viewModel data and assign that to a ActiveRecord model. I used below code but my model object atrribute always show NULL value for attributes:
我有一个临时模型作为viewModel。在我的CRUD操作中(例如actionCreate)我想获取此viewModel数据并将其分配给ActiveRecord模型。我使用下面的代码但我的模型对象atrribute总是显示属性的NULL值:
$model = new _Users();
if ($model->load(Yii::$app->request->post())) {
Yii::info($model->attributes,'test'); // NULL
$attributesValue =[
'title' => $_POST['_Users']['title'],
'type' => $_POST['_Users']['type'],
];
$model->attributes = $attributesValue;
Yii::info($model->attributes,'test'); // NULL
$dbModel = new Users();
$dbModel->title = $model->title;
$dbModel->type = $model->type . ' CYC'; // CYC is static type code
Yii::info($dbModel->attributes,'test'); // NULL
if ($dbModel->save()) {
return $this->redirect(['view', 'id' => $dbModel->id]); // Page redirect to blank page
}
}
else {
return $this->render('create', [
'model' => $model,
]);
}
I think $model->load(Yii::$app->request->post()) not working and object attribute being NULL. Is it Yii2 bug or my code is incorrect??
我认为$ model-> load(Yii :: $ app-> request-> post())不工作,对象属性为NULL。是Yii2错误还是我的代码不正确?
6 个解决方案
#1
20
If there is no rule for your attribute the $model->load()
will ignore those not in the rules of the model.
如果您的属性没有规则,$ model-> load()将忽略那些不在模型规则中的规则。
Add your attributes to the rules function
将属性添加到规则函数
public function rules()
{
return [
...
[['attribute_name'], 'type'],
...
];
}
#2
3
To fetch data for an individually attributes(db-fields) in yii2.0 then you should just do as:
要获取yii2.0中单独属性(db-fields)的数据,那么您应该执行以下操作:
echo $yourModel->getAttribute('email');
#3
1
ActiveRecord $attributes
is a private property Use $model->getAttribute(string)
ActiveRecord $ attributes是私有属性使用$ model-> getAttribute(string)
#4
1
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';
#5
0
You must remove all public properties (title, type, etc.) in your _User
model and $model->attributes = $post
will work correctly.
您必须删除_User模型中的所有公共属性(标题,类型等),$ model-> attributes = $ post才能正常工作。
#6
0
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()?>
#1
20
If there is no rule for your attribute the $model->load()
will ignore those not in the rules of the model.
如果您的属性没有规则,$ model-> load()将忽略那些不在模型规则中的规则。
Add your attributes to the rules function
将属性添加到规则函数
public function rules()
{
return [
...
[['attribute_name'], 'type'],
...
];
}
#2
3
To fetch data for an individually attributes(db-fields) in yii2.0 then you should just do as:
要获取yii2.0中单独属性(db-fields)的数据,那么您应该执行以下操作:
echo $yourModel->getAttribute('email');
#3
1
ActiveRecord $attributes
is a private property Use $model->getAttribute(string)
ActiveRecord $ attributes是私有属性使用$ model-> getAttribute(string)
#4
1
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';
#5
0
You must remove all public properties (title, type, etc.) in your _User
model and $model->attributes = $post
will work correctly.
您必须删除_User模型中的所有公共属性(标题,类型等),$ model-> attributes = $ post才能正常工作。
#6
0
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()?>