Yii Framework Forum: Image Upload Duplicate Sql Entry! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Image Upload Duplicate Sql Entry! Rate Topic: -----

#1 User is offline   marvix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 02-August 13

Posted 17 August 2013 - 03:46 PM

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,
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 17 August 2013 - 04:02 PM

What exactly is being duplicated?
What did you mean by saying "because of saveAs"?
Paste the Article model's code.
God is real unless declared as integer
0

#3 User is offline   marvix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 02-August 13

Posted 17 August 2013 - 10:17 PM

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.
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 18 August 2013 - 12:04 AM

CUploadedFile::saveAs is not dealing with db, the problem is somewhere else.
God is real unless declared as integer
0

#5 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 622
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 18 August 2013 - 02:11 AM

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'.
Don't be a dick.
0

#6 User is offline   marvix 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 80
  • Joined: 02-August 13

Posted 18 August 2013 - 09:41 AM

Yes, it was ajax issue in the form ;)

Thanks a lot!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users