使用上传的文件更新 Yii 表单记录,如果我不再附加文件,它会引发错误



>当我使用上传的文件在我的 Yii 表单中创建一条新记录时,它工作正常,但是当我更新时,我必须再次附加该文件,否则它会给出错误 这是我的控制器文件,请告诉我我的错误是什么 我上传的文件是一张图像,我想要的是更改一个字段,比如说日期并保持其余部分,包括上传的文件,但如果不再附加文件,它会给出错误

<?php
namespace appcontrollers;
use Yii;
use appmodelsJetskiDamageSettlementAgreement;
use appmodelsJetskiDamageSettlementAgreementSearch;
use yiiwebController;
use yiiwebNotFoundHttpException;
use yiifiltersVerbFilter;
use yiiwebUploadedFile;
/**
* JetskiDamageSettlementAgreementController implements the CRUD actions for JetskiDamageSettlementAgreement model.
*/
class JetskiDamageSettlementAgreementController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all JetskiDamageSettlementAgreement models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new JetskiDamageSettlementAgreementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single JetskiDamageSettlementAgreement model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new JetskiDamageSettlementAgreement model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new JetskiDamageSettlementAgreement();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// get the instance of the uploaded file
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->agreement_date = date ('y-m-d h:m:s');
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing JetskiDamageSettlementAgreement model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->damage_image = UploadedFile::getInstance($model, 'damage_image');
$image_name = $model->customer_name.'.'.$model->damage_image->extension;
$image_path = 'attachments/' .$image_name;
$model->damage_image->saveAs($image_path);
$model->damage_image = $image_path;
$model->save();
return $this->redirect(['view', 'id' => $model->agreement_id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing JetskiDamageSettlementAgreement model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the JetskiDamageSettlementAgreement model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return JetskiDamageSettlementAgreement the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = JetskiDamageSettlementAgreement::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

据我了解,由于您设置了该模型的规则,因此您会收到错误。在模型规则中,文件的此字段设置为所有方案都需要

一种可能的解决方案是将字段设置为仅在插入方案中必需,并将更新方案保留为字段不需要。但这实际上取决于您需要满足的业务逻辑。

最新更新