Update a record without removing the uploaded image

Hi just wondering if no new image upload, will the record get updated without losing the existing data in DB?

Currently my code belows if no image upload…the record updated with erase exiting image path in db. Please assist.

public function actionUpdate($id)






                    $basePath = Yii::getPathOfAlias('webroot');

                    $image = CUploadedFile::getInstance($model, 'image_1');

		 if (isset($image) && !is_null($image)) {

                        $filename = uniqid('', true).'.'.strtolower($image->extensionName); //uniqid('', true) . $image1;

                        $model->image_1 = (!is_null($image)) ? '/images/promotions/' . $filename : '';





			if (isset($image) && !is_null($image)) {

                                    $image->saveAs($basePath. $model->image_1);


			$this->redirect(array('view', 'id' => $model->id));









i saw many topics in the forum discuss about this and a github ticket but no clear answer out there (i guess the issue was not presented in a clear way).

instead of making new thread i will ask your same question in another way if you allow me.

[color="#008000"]the case:[/color]

  • we have a DB/Model that contains file field and another field let us call it: title.

  • we need to update the title in a particular record that contains value in that file field (let say [color="#0000FF"]filename.jpg[/color] <string>).

this is our current record will be:

title: my old title

file: filename.jpg

[color="#8B0000"]result after update is:[/color]

  • title will be updated successfully BUT the file field value ([color="#0000FF"]filename.jpg[/color]) will be deleted from DB even if we don’t want to delete it.

our record after update is:

title: updated title

file: null

the questions:

1- how to update a (record/db row) that have a file field without (re-uploading) the file again since yii delete default value. <it can be done from old Model record before $model->save()>.

2- ok. here is the main concern: in update if we did not upload a file, we always get an empty value from submitted form. so the question is:

from this submitted empty file filed how we can distinguish if the user want to "keep" the existing file_name in db OR he wants to "delete" this file from DB?

apart from that, updating field in DB should be straight forward without messing with any other data in that record! developers should not figure out a workaround to keep other fields data (save) from deletion. :mellow:

i hope this will clearly the issue. ;)