Image Database Field doesn't update

Hi All,

I’ve starred at the code for quite some time and can’t see what I’m doing wrong here.

Basically I have a form with a model and controller that uploads an image and some basic info.

Creating a new record works just fine, but when updating the record, the "image’ field in the database doesn’t update!!

The image uploads and the basic info fields update but not the ‘image’ field.

I guess its a rule that I have wrong but I’m going round in circles and I’m reaching out for some help!!

Controller…

public function actionCreate()

{

	$model=new InventoryLine;


	$model->inv_id = $this->_inventory->id;


	if(isset($_POST['InventoryLine']))


	{	


		


	$model->attributes=$_POST['InventoryLine'];


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


	$fileName = date("d-m-Y-h-i-s", time()).".jpg";


	if(empty($uploadedFile))


		 $fileName = 'default.jpg'; 


	$model->image = $fileName;


		if($model->save())


		{


		$uploadedFile->saveAs(Yii::app()->basePath.'/../uploads/inventory/site/'.$fileName);


		chmod(Yii::app()->basePath.'/../uploads/inventory/site/'.$fileName, 0777 );


		


			$this->redirect(array("inventory/view",'id'=>$model->inv_id));


	}


	}


	$this->render('create',array(


		'model'=>$model,


	));


}

public function actionUpdate($id)

{

	$model=$this->loadModel($id);


	


	if(isset($_POST['InventoryLine']))


	{


		$_POST['InventoryLine']['image'] = $model->image;


		$model->attributes=$_POST['InventoryLine'];


		


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


			if(empty($uploadedFile))


		 $fileName = $model->image;


		 else


		 			$fileName = date("d-m-Y-h-i-s", time()).".jpg";





		if($model->save())





			{


        		if(!empty($uploadedFile))


				{


				$uploadedFile->saveAs(Yii::app()->basePath.'/../uploads/inventory/site/'.$fileName);


				chmod(Yii::app()->basePath.'/../uploads/inventory/site/'.$fileName, 0777 );


				}





			}


			$this->redirect(array("inventory/view",'id'=>$model->inv_id));


			


			}

$this->render(‘update’,array(

‘model’=>$model,

));

}

Model

public function rules()

{


	return array(


		array('room, s_order, item, description', 'required'),


		array('room', 'length', 'max'=>50),


		array('item', 'length', 'max'=>50),


		array('image', 'length', 'max'=>500, 'on'=>'insert,update'),


		array('description, comment, x_comment', 'length', 'max'=>5000),


		array('image','file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),


		array('invpath', 'length', 'max'=>5000),


		array('id, inv_id, room, item, description, comment', 'safe', 'on'=>'search'),


	);


}

You are using the same attribute for image name and uploaded image instance and your code is not triggering the file validator at all.

You are preparing the name for the file after upload so there is no need for the name rule.

Also your rule allows jpg, gif and png but you create file name with jpg extension only.

Set $model->image to get CUploadedFile::getInstance and don’t overwrite it with image name until $model->validate() returns true. Then you can overwrite this attribute with image name and call $model->save() although more elegant solution is to set two separate attributes for file instance and name.

I would use two rules for file upload:


array('image', 'file', 'types'=>'jpg, gif, png'),

and:


array('image', 'required', 'on' => 'insert'),

I don’t store images in the database, because I got many problems. I store images in folders instead.

Thanks for pointing me in the right direction, Bizley, all good now!

Neither do I moginn, its a bad idea, not only will it swell your DB but could lead to data corruption if not managed, I only store the path and filename in the DB!