model is inserted twice?

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.

544

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.

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

put die() after save() and see what happens

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

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.

I had the same problem. i solved this problem by modified the line “’[color=”#A0522D"]enableAjaxValidation’=>true,[/color]" into “[color=”#A0522D"]‘enableAjaxValidation’=>false,[/color]". 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)"