douhong6187 2015-03-13 16:48
浏览 19

无法更新yii项目中的图像

I would like to upload an image and save the image name in database. I can create it and everything is ok, but update has problems. If i update the $model->img (image) value will be blank in db. This is my model rules:

            array('img', 'file','types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'on'=>'update'),
            array('title, img', 'length', 'max'=>255, 'on'=>'insert,update'),

And this is the controller:

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
                $_SESSION['KCFINDER']['disabled'] = false;
                $_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/../images/"; // URL for the uploads folder
                $_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../../images/"; // path to the uploads folder
        // $this->performAjaxValidation($model);
        if(isset($_POST['News']))
        {
                    $_POST['News']['img'] = $model->img;
                    $model->attributes=$_POST['News'];
                    $model->img = $_POST['News']['img'];
                        $uploadedFile=CUploadedFile::getInstance($model,'img');

            if($model->save()){
                            if(!empty($uploadedFile)) 
                            {
                                    $uploadedFile->saveAs(Yii::app()->basePath.'/../../images/'.$model->img);
                            }
                        }
                $this->redirect(array('view','id'=>$model->id));
        }

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

Before the save i check $model->img and it has value. So i think something wrong with save() in update.

  • 写回答

1条回答 默认 最新

  • drvjlec1767 2015-03-14 10:51
    关注

    $_POST['News']['img'] = $model->img; Why you did it? You must remove this line.

     $model->attributes=$_POST['News'];
     $model->img = $_POST['News']['img'];
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致