某些规则仅在Yii中生效

时间:2014-10-21 09:01:46

标签: php 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,
    ));

我的actionCreate是以下

$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,
        ));

所以我想要的是

  • 如果用户上传图片,则能够更改图像
  • 验证文件
  • 如果留空,则不会使用先前的图像

1 个答案:

答案 0 :(得分:0)

假设其余代码满足您的需求,您可以更新代码,如下所示。您的模型规则应该是:

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需要

将控制器操作更改为:

$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循环,这将使它至少运行一次。