简体   繁体   中英

file field removed autometically on save yii model

I am trying to update a record which consist of a pdf file . record is updating successfully but the file field becomes empty on update .

my controller code is

$model2=$this->loadModel($alldata[0]->frm_id,'Forms');
            $values=array();
            $tagsarray=$_POST['searched_tag'];   // matched tag values
            $pdfval=$_POST['searched_tag_pdf'];    // pdf form values

            for($i=0;$i< count($pdfval);$i++) 
            {
                    $values[$pdfval[$i]]=$tagsarray[$i];
            }
                $model2->analyse_data=json_encode($values);
                $model2->frm_status=2;
            if($model2->save()) 
            { 
                Yii::app()->user->setFlash('pdfupload','Form analysis done! data saved.');
                $this->redirect(Yii::app()->createUrl('/formsupload'));
            }else 
            {
                Yii::app()->user->setFlash('pdfupload','Some error to save the data.'); 
                $this->redirect(Yii::app()->createUrl('/formsupload'));
            } 

and model rules is

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('frm_code,frm_desc,frm_pdf', 'required'),
            array('frm_code', 'unique'),
            array('is_approve', 'safe'),
            //array('desc', 'min'=>1, 'max'=>200)
            array('frm_pdf', 'file', 'types'=>'pdf','allowEmpty'=>true),
        );
    }

Update also tried this solution here unable to catch the problem .

Handling file upload updates in yii can be a bit tricky. Try setting the file attribute to be unsafe and see if that helps. Yii seems to set the value of that attribute to null if it cannot find an uploaded file. Setting it to unsafe should prevent that.

You can check my answer at the yii forum link below to someone running into a similar problem for more info.

http://www.yiiframework.com/forum/index.php/topic/63506-how-can-i-update-records-without-adding-same-image-again/page__p__279888#entry279888

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM