使用上载的文件更新yii表单记录如果我没有';请不要再附加文件

使用上载的文件更新yii表单记录如果我没有';请不要再附加文件,yii,Yii,当我用上传的文件在我的yii表单中创建一个新记录时,它工作正常,但当我更新时,我必须再次附加该文件,否则它将给出错误消息 这是我的控制器文件,请告诉我我的错误是什么 我上传的文件是一个图像,我想要的是改变一个字段,比如说日期,并保留其余的内容,包括上传的文件,但如果不再次附加文件,它将给出一个错误 <?php namespace app\controllers; use Yii; use app\models\JetskiDamageSettlementAgreement; use a

当我用上传的文件在我的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.');
    }
}


据我所知,由于您将规则设置为该模型,因此您会出现错误。在您的模型规则中,对于所有场景,文件的此字段设置为必需


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

谢谢Zmilan,这听起来是一个合理的解决方案,但实际上,如果可能的话,我更喜欢将其设置为“不需要”,而是保留现有文件,以及该规则在何处,何时可以修改?规则在您的模型中设置。您可以调整规则以满足业务逻辑的确切需要。如果使用Gii从数据库表生成模型,则Gii将根据数据库表规则应用规则。我认为数据库中的文件字段是必需的,这就是为什么规则最终会变成必需的。但你可以进一步调整。因此,您可以将其设置为新记录所必需的,而不是现有记录所必需的(因为新记录需要它,所以它们将已经拥有它),或者甚至创建您自己的验证器来进行更详细的检查。非常感谢您的详细解释,我完全理解您的要求points@Rashwan不客气。我很高兴能帮上忙:)