Yii Framework Forum: model is inserted twice? - Yii Framework Forum

Jump to content

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

model is inserted twice? Rate Topic: -----

#1 User is offline   paro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-February 10

Posted 18 February 2010 - 12:22 PM

Hi

So I've been trying to do a simple insert using active record with the following code.

public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $hasher = Yii::app()->passwordCreator;

    $user = new Customer();
    $user->email = 'test@test.com';
    $user->passwordSalt = $hasher->createSalt();
    $user->password = $hasher->createPassword('test', $user->passwordSalt);
    $user->isLocked = 0;
    $user->role = 'someRole';
    $user->creationDate = Date::utcnow()->getTimeStamp();
    $user->isVerified = 1;
    
    if($user->save()) {
        Yii::trace('save success', 'info');
    } else {
        Yii::trace('save failure: ' . print_r($user->getErrors(), true), 'info');
    }
    
    $this->render('index');
}


The problem is that the record is inserted twice but the trace would seemingly indicate otherwise.

The action above is on the default SiteController that is generated when you run yiic webapp. The model was also generated by yiic which i didn't modify.

Attached File  Screenshot.png (97.97K)
Number of downloads: 25

Any ideas?
0

#2 User is offline   Jaz Manister 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 27-October 09
  • Location:Rural Western New York, USA

Posted 18 February 2010 - 09:39 PM

View Postparo, on 18 February 2010 - 12:22 PM, said:

Hi

So I've been trying to do a simple insert using active record with the following code.

public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $hasher = Yii::app()->passwordCreator;

    $user = new Customer();
    $user->email = 'test@test.com';
    $user->passwordSalt = $hasher->createSalt();
    $user->password = $hasher->createPassword('test', $user->passwordSalt);
    $user->isLocked = 0;
    $user->role = 'someRole';
    $user->creationDate = Date::utcnow()->getTimeStamp();
    $user->isVerified = 1;
    
    if($user->save()) {
        Yii::trace('save success', 'info');
    } else {
        Yii::trace('save failure: ' . print_r($user->getErrors(), true), 'info');
    }
    
    $this->render('index');
}


The problem is that the record is inserted twice but the trace would seemingly indicate otherwise.

The action above is on the default SiteController that is generated when you run yiic webapp. The model was also generated by yiic which i didn't modify.

Attachment Screenshot.png

Any ideas?


Hey, are you sure you didn't just visit the page twice.. because it seems like you're going to be creating this user every time you load that page.
0

#3 User is offline   paro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-February 10

Posted 18 February 2010 - 11:36 PM

Yes, each time I hit refresh two more rows appear in the database.
0

#4 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 19 February 2010 - 01:23 AM

put die() after save() and see what happens
http://php-thoughts.cubedwater.com - my bloggings about Yii
0

#5 User is offline   paro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-February 10

Posted 19 February 2010 - 02:21 AM

Putting die() after the call to save yields no output (obviously) but the model still gets inserted twice.
0

#6 User is offline   paro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-February 10

Posted 19 February 2010 - 02:31 AM

I fixed it! I knew it had to be something silly.

Turns out it was a left over .htaccess that I forgot to remove from some previous fiddling around I was doing with '^(.*)$ index.php?route=$1' in it.
0

#7 User is offline   Sivanthi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 14-February 13
  • Location:Nagercoil, Kanya Kumari, Tamilnadu. India

Posted 15 March 2013 - 02:33 AM

View Postparo, on 18 February 2010 - 12:22 PM, said:

Hi

So I've been trying to do a simple insert using active record with the following code.

public function actionIndex()
{
    // renders the view file 'protected/views/site/index.php'
    // using the default layout 'protected/views/layouts/main.php'
    $hasher = Yii::app()->passwordCreator;

    $user = new Customer();
    $user->email = 'test@test.com';
    $user->passwordSalt = $hasher->createSalt();
    $user->password = $hasher->createPassword('test', $user->passwordSalt);
    $user->isLocked = 0;
    $user->role = 'someRole';
    $user->creationDate = Date::utcnow()->getTimeStamp();
    $user->isVerified = 1;
    
    if($user->save()) {
        Yii::trace('save success', 'info');
    } else {
        Yii::trace('save failure: ' . print_r($user->getErrors(), true), 'info');
    }
    
    $this->render('index');
}


The problem is that the record is inserted twice but the trace would seemingly indicate otherwise.

The action above is on the default SiteController that is generated when you run yiic webapp. The model was also generated by yiic which i didn't modify.

Attachment Screenshot.png

Any ideas?


I had the same problem. i solved this problem by modified the line "'enableAjaxValidation'=>true," into "'enableAjaxValidation'=>false,". and this is my controller function
public function actionCreate()
	{
		$model=new Patients;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Patients']))
		{
			$model->attributes=$_POST['Patients'];
			
			$model->dat = date("Y-m-d",strtotime($model->dat));
			
			
			if($model->save())
			{
			$this->redirect(array('patients/admin'));
			}
			
			
			
		}

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

i found the reason "i changed ajax validation is true in my form [view] but i forgot to uncomment the line performAjaxvalidation($model)"
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