【发布时间】:2014-07-18 11:06:32
【问题描述】:
我有一个临时模型作为 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,
]);
}
我认为 $model->load(Yii::$app->request->post()) 不起作用并且对象属性为 NULL。是 Yii2 的 bug 还是我的代码不正确??
【问题讨论】:
-
您必须为此属性设置规则
-
首先...给我看看你的表格和模型
-
$model->attributes = $_POST['_Users'];
标签: php yii-extensions yii yii2