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

insrf1ej  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(131)

当我创建一个新的记录在我的yii形式与上传的文件,它的工作很好,但当我更新我必须再次附加文件,否则它会给予错误这里是我的控制器文件,请告诉我什么是我的错误我上传的文件是一个图像,我想要的是改变一个字段让我们说的日期,并保持其余的是包括上传的文件,但如果不再次附加文件,则会给予错误

<?php

namespace app\controllers;

use Yii;
use app\models\JetskiDamageSettlementAgreement;
use app\models\JetskiDamageSettlementAgreementSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * 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.');
    }
}
amrnrhlw

amrnrhlw1#

据我所知,您得到这个错误是因为您的规则设置为该模型。在您的模型规则中,该文件的这个字段被设置为必填用于所有场景。
一个可能的解决方案是将字段设置为仅在insert方案中是必需的,而将update方案中的字段设置为非必需的。但这实际上取决于您需要满足的业务逻辑。

相关问题