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'),
);
}