Error In Update Filefield

Hi All

I add upload file filed to upload image with text fields and then insert it to database with image name

after upload . and everything is OK but in update function if i don’t upload new image I have this error !!

Error Message




[b]Fatal error[/b]:  Call to a member function saveAs() on a non-object in [b]H:\xampp\htdocs\cms\protected\controllers\ArticleController.php[/b] on line [b]230[/b]



  • How to get old value .

  • How To Fix it ???

Thanks in advance

_form //view





<?php

/* @var $this ArticleController */

/* @var $model Articles */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'articles-form',

	'enableAjaxValidation'=>true,

   'htmlOptions'=>array('enctype'=>'multipart/form-data'),	

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'title'); ?>

		<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'title'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'article'); ?>

		

    	<?php $this->widget('application.extensions.eckeditor.ECKEditor', array(

            	'model'=>$model,

            	'attribute'=>'article',

            	)); ?>    	

		<?php echo $form->error($model,'article'); ?>

	</div>













	<div class="row">

		<?php echo $form->labelEx($model,'section_id'); ?>

	<?php echo chtml::activeDropDownList($model,'section_id',$model->getsections(),array('prompt'=>'Select Section')); ?>

		<?php echo $form->error($model,'section_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'image'); ?>

		<?php echo $form->fileField($model,'image',array('value'=>$model->image)); ?>

		<?php echo $form->error($model,'image'); ?>

	</div>


	


	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->




controller :




public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		

    	

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

		{

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

      	

      	$articleid=$model->article_id;

      	$user_id=Yii::app()->user->id;

      	

      	

       	

            	

  	

    	if(Yii::app()->user->status==2 or Yii::app()->user->status==3 or Yii::app()->user->status==1  ){	

     	

     	

     	///// Start user 

     	if(Yii::app()->user->status==1){

        	

        	$count=Yii::app()->db->createCommand(" select article_id from tbl_articles where user_id='$user_id' and article_id='$model->article_id' and edited='0' ")->query()->count();

	

	if($count!==0){

        	$model->edite_date=date("Y-m-d-h-s-m");

        	$model->editby=Yii::app()->user->id;

        	$model->language=1;

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

   	

   	

   	/// create new image name 

    	//   $newname = rand(1,987699) . '-' . $model->image;

     	




			if($model->save()){

			   

        	

            	

          	echo $model->image;

          	

           	$model->image->saveAs('postimages/'.$model->image);

           	

           	

         	Yii::app()->user->setFlash('success',' Administration Message <br/>

             	

            	Aricle  Was Edited 

              	');




	        	

              	

             	

            	//Yii::app()->db->createCommand(" update tbl_articles set image='$model->image'  ")->query();

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

	}

		}

    	}

    	

    	//// End user 

    	

    	//// start Admin

      	if(Yii::app()->user->status==2){ 

        	

       	$model->edite_date=date("Y-m-d-h-s-m");

       	$model->editby=Yii::app()->user->id;

       	$model->edited=1;

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

   	

   	

   	/// create new image name 

      	//  $newname = rand(1,987699) . '-' . $model->image;


			if($model->save()){

			   

           	$model->image->saveAs('postimages/'.$model->image);

             	

                	

         	Yii::app()->user->setFlash('success',' Administration Message <br/>

             	

            	Aricle  Was Edited 

              	');

            	//Yii::app()->db->createCommand(" update tbl_articles set image='$newname'  ")->query();

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

	}

		}

    	

    	//// End Admin 

    	


if(Yii::app()->user->status==3){ /// Editor 

	

 	

       	$model->edite_date=date("Y-m-d-h-s-m");

       	$model->editby=Yii::app()->user->id;

       	$model->edited=1;

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

       	if($model->save()){

			   

           	$model->image->saveAs('postimages/'.$model->image);

             	

                	

         	Yii::app()->user->setFlash('success',' Administration Message <br/>

             	

            	Aricle  Was Edited 

              	');

            	//Yii::app()->db->createCommand(" update tbl_articles set image='$newname'  ")->query();

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

	}

       	

	

}


}

}







In your controller, you should make sure that an image has been uploaded before attempting to save it.

You could structure it like this:




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

if ($image !== null)

    $model->image = $image;



However, you’re using the image attribute to store both the image instance and the image path at different times, which is a very bad idea.

I would recommend creating a new attribute, say $imageFile and setting that with the file instance. Use the $image attribute to store just the path once file upload has been successful. Your file based validation is performed against the $imageFile attribute, which doesn’t have an analogue in the database table.

In pseudo-code:




if ($model->save())

{

    if (fileHasBeenUploaded)

    {

        saveFile();

        $model->image = newFilePath;

        $model->save();

    }

}



Thanks for your important notes , My problem was fixed .