我想使用GII工具制作CRUD操作,但是当我尝试保存帖子时收到错误消息Missing required parameters: id
。
邮政控制人:
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post())) {
$model->post_create_time=date('Y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->id_post]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
为什么我总是会收到此错误?
答案 0 :(得分:4)
尝试
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post())) {
$model->post_create_time=date('Y-m-d h:m:s');
$model->save(false);
return $this->redirect(['view', 'id' => $model->id_post]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
确保执行$model->save(false)
并查看是否有效。
答案 1 :(得分:0)
你可以尝试一些事情:
$model->post_id
是否为空。见下面的例子
$success=$model->save();
// if it's false, it means there was an error
var_dump($success);
exit;
save()
是否成功:见下面的代码
if($model->save()){
return $this->redirect(['view', 'id' => $model->id_post]);
}else{
// show errors
var_dump($model->getErrors();
exit;
}
除此之外,我建议您发布actionView
和类Post
的代码以及
答案 2 :(得分:0)
试试这个
public function actionCreate()
{
$model = new Post();
if ($model->load(Yii::$app->request->post())) {
$model->post_create_time=date('Y-m-d h:m:s');
if($model->save())
return $this->redirect(['view', 'id' => $model->id_post]);
else
{
return $this->render('create', [
'model' => $mod`enter code here`el,
]);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
答案 3 :(得分:0)
显然发生在$ this->重定向上。检查main.php文件中的url规则。 它应该位于main.php的某处
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
...
]
],
答案 4 :(得分:0)
某些字段太短,因此无法存储,但现在显示准确的错误消息。 我增加字段大小,没关系。