2011-07-27 68 views
2

控制器上傳文件中的記錄:更新不改變警予

/** 
* 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

在你的模型,你可以創建你的「形象」只需要在創建規則的情況下,somtething像:

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

此外,你應該在增加,如果驗證()你之前保存:

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

控制器:

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