某些规则仅在Yii中更新时生效


Certain rules only take effecton update in Yii

我做这个功能的地方,用户可以更新描述和图像,但我也想允许用户如果不选择图像,前一个图像将保持不变。它告诉我,我不能空着,现在我知道这是因为规则,但我试图制定规则只是为了更新,但它似乎不起作用。

以下是模型中的规则

return array(
        array('description,imageUrl', 'required', 'on'=>'create'),
        array('imageUrl','file','types'=>'jpg,gif,png'),
        array('description','required', 'on'=>'update'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, description', 'safe', 'on'=>'search'),
    );

在我的actionUpdate控制器中,我有以下内容。

    $model=$this->loadModel($id);
    $model->setScenario('update');

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['DestinationWedding']))
    {
        $model->attributes=$_POST['DestinationWedding'];
        if(!empty($_POST['DestinationWedding']['imageUrl'])){
            //Generating a new file name
            $fileName = mt_rand();
            //Assging the super gloabn array file
            $uploadedFile = CUploadedFile::getInstance($model,'imageUrl');
            //Check if the name is taken or not
            $checkName = DestinationWedding::model()->findAllByAttributes(array('imageUrl'=>$fileName));
            while(!empty($checkName)){
                $fileName = mt_rand();
                $checkName = DestinationWedding::model()->findAllAttributes(array('imageUrl'=>$fileName));
            }
            //Removeing the old image
            unlink(Yii::getPathOfAlias('webroot').'/images/upload/destinationWeddings/'.$model['imageUrl']);
            $model->imageUrl = $fileName;
        }
        $valid = $model->validate();
        if($valid){
            if($model->save()){
                //Moveing the uploaded file
                $uploadedFile->saveAs('images/upload/destinationWeddings/'.$fileName);
                //Seeting a new session showing the user successful message
                Yii::app()->user->setFlash('succcess','Wedding is updated successfully');
                $this->redirect(array('admin'));
            }else{
                //Setting a new session shwoing the user error message
                Yii::app()->user->setFlash('error','An error has occured');
            }
        }
    }
    $this->render('update',array(
        'model'=>$model,
    ));

我的操作创建是以下

$model=new DestinationWedding;
        $model->setScenario = 'create';
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['DestinationWedding']))
        {
            $model->attributes=$_POST['DestinationWedding'];
            //Assinging the super golbal array File
            $uploadedFile = CUploadedFile::getInstance($model,'imageUrl');
            do{
                //Generating a new file name
                $fileName = mt_rand();
                //Check if the name is taken or not
                $checkName = DestinationWedding::model()->findAllByAttributes(array('imageUrl'=>$fileName));
            }while(!empty($checkName));
            $model->imageUrl = $fileName;
            if($model->save()){
                //Moveing the upload file
                $uploadedFile->saveAs("images/upload/destinationWeddings/".$fileName);
                //Setting a new session showing the user successful message
                Yii::app()->user->setFlash('success',"File uploaded Successfully");
            }else{
                //Setting a new session showing the user errror message
                Yii::app()->user->setFlash('eroor','File was not uploaded Successfully');
            }
        }
        $this->render('create',array(
            'model'=>$model,
        ));

所以我想要extly是

  • 用户上传图像时可以更改图像
  • 验证文件
  • 如果保留为空,则不会出现错误,因为使用了以前的图像

假设其余的代码都能满足您的需要,您可以按照如下方式更新代码。您的模型规则应该是:

return array(
        array('description, imageUrl', 'required','create'),
        array('imageUrl','file','types'=>'jpg,gif,png'),
        array('description','required', 'on'=>'update'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, description', 'safe', 'on'=>'search'),
);

在上面的规则中,使descriptionimageUrl在创建场景时需要ONLY

并将控制器操作更改为:

$model=$this->loadModel($id);
$model->setScenario('update');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['DestinationWedding']))
{
    $uploadedFile = null;
    $model->attributes=$_POST['DestinationWedding'];
    if(!empty($_POST['DestinationWedding']['imageUrl'])){
        //Generating a new file name
        $fileName = mt_rand();
        //Assging the super gloabn array file
        $uploadedFile = CUploadedFile::getInstance($model,'imageUrl');
        if($uploadedFile){
            //Check if the name is taken or not
            do{
                $checkName = DestinationWedding::model()->findAllAttributes(array('imageUrl'=>$fileName));
                $fileName = mt_rand();
            }while(!empty($checkname));
            //Removeing the old image
            unlink(Yii::getPathOfAlias('webroot').'/images/upload/destinationWeddings/'.$model['imageUrl']);
            $model->imageUrl = $fileName;
        }
    }
    $valid = $model->validate();
    if($valid){
        if($model->save()){
            //Moveing the uploaded file
            if($uploadedFile)
                $uploadedFile->saveAs('images/upload/destinationWeddings/'.$fileName);
            //Seeting a new session showing the user successful message
            Yii::app()->user->setFlash('succcess','Wedding is updated successfully');
            $this->redirect(array('admin'));
        }else{
            //Setting a new session shwoing the user error message
            Yii::app()->user->setFlash('error','An error has occured');
        }
    }
}
$this->render('update',array(
    'model'=>$model,
));

在检查$uploadedFile实例的操作中,如果存在实例,则上传文件并更新数据库,否则将其保留。我已将while循环更改为while,这将使它至少运行一次。