开发者

update a record without changing the uploaded file in yii

CONTROLLER:

/**
 * 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,));

            }


        }

Now the create is working fine and the update works okay if you select a file to include in the update but when you dont select a file,it does not update and redirects you to the view.

I want it to be updating even if i dont select a file so that it will still have the current file there.


In you model you can create a rule scenario that your 'image' is only required on create, somtething like:

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

Also, you should add in an if validate() before you save:

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);
    }
}

This will help you see where the error is coming from. Obviously on a live environment handle the error better


Controller :

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));
        }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜