Here is my dilemma, I can upload files, delete files, but update is not working correct. Of course if I change the file then I can update the whole record but If I want to just update the description field then I get of course: Filename cannot be blank. This is supposed to be supplied by a hidden field as a part of activeFileField, but why not just simply return the path back to this field on update. Anyway how do I go about fixing this?
Sorry if this post would have helped me, I would not have posted here. No offense, I have read that post a 100 times. I am missing something and require a little more direction, thank you though.
Can I see the actionCreate method please, I only say this because my Create and Update are exactly the same. I remember having a similar issue and I think setting attribute in models to unsafe and allow empty to true solved it
Some of the problem was contained in the rules() of the model.
This site was a definite help in cleaning up my function.
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('description, origfile, public, user_id', 'required'),
array('user_id', 'numerical', 'integerOnly'=>true),
array('origfile, filename', 'length', 'max'=>255),
array('public', 'length', 'max'=>1),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, description, origfile, filename, public, user_id', 'safe', 'on'=>'search'),
);
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Userfiles']))
{
$model->attributes=$_POST['Userfiles'];
$oldfilename = $model->filename; // let's store original filename (if it was defined)
// Now check if there was anything uploaded and store new name if so
$file = CUploadedFile::getInstance($model, 'filename');
//$model->filename = $file;
if (is_object($file) && get_class($file)==='CUploadedFile') {
$model->filename = $file;
} else {
$model->filename = $oldfilename;
}
//echo print_r($_POST['Userfiles']);
if($model->save())
//echo print_r($_POST['Userfiles']);
if (is_object($file) && get_class($file)==='CUploadedFile') {
// again, if anything was uploaded and if we have db done then move the file from tmp to the right place
//$file->saveAs(Yii::app()->baseUrl.'/images/' . $file->name);
$file= 'images/'.$model->filename->name;
$model->filename->saveAs($file);
if ($oldfilename != $model->filename->name) {
//unlink($model->fileWithPath(Yii::app()->baseUrl.'/images/' . $oldfilename));
$file= 'images/' . $oldfilename;
unlink($file);
}
}
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}