我想要使用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,
]);
}
}
为什么我总是得到这个错误?
尝试
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)
,看看是否它是否工作。
尝试这个
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,
]);
}
}
一对夫妇的事情,你可以试一下:
$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
的代码以及
这显然是发生在$这个 - >重定向。检查main.php文件的URL规则。这里应位于某处main.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
...
]
],
有些领域是太短了,所以它不能存储,但现在表现出精确的错误消息。我增加字段的大小,这是确定。