php 模型-> Yii2 中的属性始终具有 NULL 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24823579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
model->attributes in Yii2 always has NULL value
提问by b24
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 模型。我使用了下面的代码,但我的模型对象属性总是显示属性的 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 错误还是我的代码不正确??
回答by Jason G
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'],
...
];
}
回答by Amjad Ali Chauhdry
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');
回答by Alex
ActiveRecord $attributes
is a private property
Use $model->getAttribute(string)
ActiveRecord$attributes
是私有属性 使用$model->getAttribute(string)
回答by Mahmut Ayd?n
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';
回答by soc
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()?>
回答by Alexander
You must remove all public properties (title, type, etc.) in your _User
model and $model->attributes = $post
will work correctly.
您必须删除模型中的所有公共属性(title 、 type等)_User
,$model->attributes = $post
才能正常工作。