Yii Framework Forum: Some Problem By Inserting In Database. - Yii Framework Forum

Jump to content

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

Some Problem By Inserting In Database. When i save a record by model, it insert 2 record else. Rate Topic: -----

#1 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 27 April 2013 - 07:31 AM

I have model which handle my file uploading. everything ok.
but when it run the save() method, in the datebase insert 2 record and then insert my uploaded file name record.
what is the problem?:unsure:
this is my controller action:
public function actionCreate() {
    	$model = new Picture;
    	if (isset($_POST['Picture'])) {
        	$rnd = rand(0, 9999);
        	$model->attributes = $_POST['Picture'];

        	$uploadFile = CUploadedFile::getInstance($model, 'address');
        	
        	$model->address = "{$rnd}-{$uploadFile}";
        	
        	if ($model->save()) {
                	$uploadFile->saveAs(Yii::app()->basePath . '/../upload/' . "{$rnd}-{$uploadFile}");
            	//$this->redirect(array('view', 'id' => $model->id));
        	}
    	}

    	$this->render('create', array('model' => $model, ));
	}

www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 27 April 2013 - 08:30 AM

Do you have enabled the ajax validation in your form? If so, you have to handle the ajax validation request in the controller action, otherwise $model->save() could be called twice.
0

#3 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 27 April 2013 - 08:36 AM

No, in my controller there isn't any ajax validation.
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 27 April 2013 - 08:55 AM

View PostShahcheraghean, on 27 April 2013 - 08:36 AM, said:

No, in my controller there isn't any ajax validation.

No, I mean in the view. Something like:
    $form = $this->beginWidget('CActiveForm',
        array(
            'id' => 'photo-form',
            'enableAjaxValidation' => true, // !!
            ...
        )
    );

0

#5 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 27 April 2013 - 09:24 AM

ohh. Thank`s very much. that was it.:huh:
www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
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