我可以为我的应用程序中的表格错误上传图像。当我上传文件图像时,此方法有效,但如果文件为空,则会出现错误:
“违反完整性约束”
我的规则:
public function rules()
{
return [
[['nm_bug', 'description', 'date_from','module'], 'required'],
[['id_bug'], 'integer'],
[['nm_bug', 'image_file'], 'string', 'max' => 100],
[['description'], 'string', 'max' => 300],
[['image_file'], 'file', 'extensions'=> 'png,gif,jpg', 'skipOnEmpty' => true],
[['module'], 'string', 'max' => 100],
];
}
我的控制器:
public function actionCreatebug()
{
$model = new Bug();
$current_image = $model->image_file;
if ($model->load(Yii::$app->request->post())) {
$img = UploadedFile::getInstance($model, 'image_file');
if(!empty($img)){
$image = $model->nm_bug.rand(1, 1000) . '.' . $img->extension;
$image_path = 'bug/' . $image;
$img->saveAs($image_path);
$img = $image_path;
$model->save();
}
else{
$model->image_file = $current_image;
$model->save();
}
yii::$app->session->setFlash('success', 'Data Berhasil Disimpan');
return $this->redirect(['moduls/view', 'id' => $model->module]);
}
return $this->render('createbug', [
'model' => $model,
]);
}`
我该怎么做?