更新记录而不更改yii中的上传文件

时间:2011-07-27 11:07:42

标签: php yii

控制器:

/**
 * Creates a new model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 */
public function actionCreate()
{
    $model=new Issue;
    $model->project_id = $this->_project->id;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Issue']))
    {
        $model->attributes=$_POST['Issue'];
        $model->image=CUploadedFile::getInstance($model,'image');
        if($model->save())
            $model->image->saveAs(Yii::app()->basePath . '/../images/' . $model->image);
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}


/**
 * Updates a particular model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id the ID of the model to be updated
 */
public function actionUpdate($id)
{
    $model=$this->loadModel($id);

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Issue']))
    {
        $model->attributes=$_POST['Issue'];
        $file_flyer = CUploadedFile::getInstance($model,'image');


    if ( (is_object($file_flyer) && get_class($file_flyer)==='CUploadedFile'))
    //{
            {
            $model->image = $file_flyer;
            }

        if($model->save())
               {
            if (is_object($file_flyer))
            {
                $model->image->saveAs(Yii::app()->basePath.'/../images/'.$model->image);
                //$this->render('update',array('model'=>$model,));

            }


        }

现在创建工作正常,如果您选择要包含在更新中的文件但是当您不选择文件时,更新可以正常运行,但它不会更新并将您重定向到视图。

我希望它即使我不选择文件也要更新,以便它仍然有当前文件。

2 个答案:

答案 0 :(得分:2)

在您的模型中,您可以创建一个规则场景,只有在创建时才需要“图像”,例如:

public function rules(){
    return array(
        array('image', 'required','on'=>array('create')),
    );
}

此外,您应该在保存之前添加if validate():

if(isset($_POST['Issue'])) {
    $model->attributes=$_POST['Issue'];
    $model->image=CUploadedFile::getInstance($model,'image');
    if ($model->validate()){
        if($model->save()){
            $model->image->saveAs(Yii::app()->basePath . '/../images/' . $model->image);
            $this->redirect(array('view','id'=>$model->id));
        }
    } else {    
        print_r($model->errors);
    }
}

这将帮助您了解错误的来源。显然在实时环境中更好地处理错误

答案 1 :(得分:1)

控制器:

if(isset($_POST['Propertyfeatures']))
        {


             $_POST['Propertyfeatures']['image'] = $model->image;
            // 
//           print_r($_POST['Propertyfeatures']);
//      exit;


            $model->attributes=$_POST['Propertyfeatures'];

            $uploadedFile=CUploadedFile::getInstance($model,'image');



            if($model->save())
            {

            if(!empty($uploadedFile))  // check if uploaded file is set or not
                {
                    $uploadedFile->saveAs(Yii::app()->basePath.'../../banner/'.$model->image);


                    //print_r($uploadedFile);
                    //exit();
                }

                $this->redirect(array('view','id'=>$model->p_id));
        }
        }