Image Upload Duplicate Sql Entry!

Hi,

When am uploading an image the data is duplicated in the the db, because of saveAs!




	public function actionCreate()

	{

		$model=new Article;


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

		{


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

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

			if(is_object($main_image)){

					$MainFileName= uniqid(time(), false);	

					$FileExt = $main_image->getExtensionName();

					$model->main_image = $MainFileName.$FileExt;

			}

			if($model->save()){

				 if(is_object($main_image)){

				 	$main_image->saveAs(Yii::app()->basePath.'../../images/'.$MainFileName.".".$FileExt);

				 }

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

			}			


		 }


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

			'model'=>$model,

		));

	}




What the mistake ?!

Thanks,

What exactly is being duplicated?

What did you mean by saying "because of saveAs"?

Paste the Article model’s code.

What I mean that once am usingCUploadedFile::save as to save the image, the submitted data will goes twice in the db, the yii saving the into the db twice, the image saving is going fine.

CUploadedFile::saveAs is not dealing with db, the problem is somewhere else.

You probably are using an CActiveForm in your update view with some ajax validation enabled. Before you save the form it makes an ajax call to the same action. You don’t check for it and thus it saves the record instead of just validating it. Then you save it again, via normal request (not ajax).

Either disable the ajax validation or add code that intercepts it, there is another field in the POST data called ‘ajax’.

Yes, it was ajax issue in the form ;)

Thanks a lot!