Yii Framework Forum: Yii 1.1.16 and CUploadedFile - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Yii 1.1.16 and CUploadedFile Rate Topic: -----

#1 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 27-October 10

Posted 20 July 2015 - 04:13 AM

Hi,
I have a webapp that stores files inside DB, in blob fields.
Since I have installed Yii 1.1.16, when I call an action that updates another field and does save() upon the model, my field storing binary data is flushed !
Very surprising side effect !
Maybe a side effect of the correction of this bug :
Bug #1257: CFileValidator is no longer unsafe by default to prevent setting arbitrary values. Instead, when no file is uploaded attribute is set to null (marcovtwout)

This is what my action does :
$file = Files::model()->findByAttributes(array('id' => $id, 'deleted' => '0'));
        if ($file !== null) {
            $file->nbdownloads++;
            $file->save();
        }

This post has been edited by jpj: 20 July 2015 - 04:16 AM

0

#2 User is offline   Saurabh Dhariwal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 27-September 13

Posted 22 July 2015 - 01:21 AM

For this problem you have to check whether blob field is empty or not as below:

public function actionUpdate($id) {
        $model = $this->loadModel($id);
        $oldBlob = $model->file;

        if(isset($_POST['FileUpload']))        {
                $model->attributes=$_POST['FileUpload'];

                $file = CUploadedFile::getInstance($this,'file');

                if (!empty($file)) {
                        if ($file->size > 0) {
                                $model->file=file_get_contents($file->tmp_name);
                        }
                }
                else {
                        $model->file = $oldBlob;        // old blob field data is remained same here
                }

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


Let me know if you face any further query/concern regarding this.

Thanks
Saurabh Dhariwal
Thanks,
Saurabh Dhariwal
Skype ID: dhariwal.saurabh
0

#3 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 27-October 10

Posted 22 July 2015 - 02:02 AM

I'm not sure you understand my problem.
I just want to update the counter of downloads when a file is downloaded.
I don't call update() but save() and I know there will never be any uploaded file in that case, $_POST will never have any uploaded file.
I don't understand why when I call save() after changing one integer field, my blob field is set to null.
0

#4 User is offline   phtamas 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 777
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 22 July 2015 - 11:18 AM

View Postjpj, on 20 July 2015 - 04:13 AM, said:

Maybe a side effect of the correction of this bug :
Bug #1257


Very likely.
Try to save the model without validation:
$file->save(false);

0

#5 User is offline   Bizley 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 922
  • Joined: 29-July 14
  • Location:Wrocław

Posted 22 July 2015 - 11:38 AM

This method might help http://www.yiiframew...Counters-detail
http://bizley.pl ʕ•ᴥ•ʔ
0

#6 User is offline   jpj 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 27-October 10

Posted 23 July 2015 - 01:57 AM

It works with save(false), thanks.
Anyway I find this behavior very weird.
For me, upgrading Yii should not have broken my code such a way, it shows there's no backward compatibility.
Fortunately, I noticed the problem quickly, if not it would have been a serious problem ; each download was deleting the corresponding file content !
I think it's the job of the developer to validate input data and set defaults values, the framework should not set cuploadedfile to null if there's no uploaded file.
In my case, I can bypass validation rules but what if I needed them ?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users