Duplicate Entries Using Cactiveform And Transactions

Hi,

I´m having issues with an ActiveForm that is used to save data into 3 tables which are related. Here is the create action from the controler:




public function actionCreate()

	{

		$a=new Persona;

		$b=new Empleado;

		$c=new PersonaTelefono;

		

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Persona'],$_POST['Empleado'],$_POST['PersonaTelefono']))

		{

			$a->attributes=$_POST['Persona'];

			$b->attributes=$_POST['Empleado'];

			$c->attributes=$_POST['PersonaTelefono'];

			

			$error=false;

			$transaction = Yii::app()->db->beginTransaction();

			try{

				if(!$a->save()){

					throw new CException(CHtml::errorsummary($a));

				}

				$b->persona_id=$a->id;

				$c->persona_id=$a->id;

				if(!$b->save()){

					throw new CException(CHtml::errorsummary($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />);

				}

				if(!$c->save()){

					throw new CException(CHtml::errorsummary($c));

				}

				$transaction->commit();

				Yii::app()->user->setFlash('success', "Registro creado con éxito");	

			}

			catch(Exception $e){

				$transaction->rollback();

				$error = $e->getMessage();

				Yii::app()->user->setFlash('error', "No se creó el registro");				

			}

			if (!$error) {

				$this->redirect(array('create'));

			}

}


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

			'a'=>$a,

			'b'=>$b,			

			'c'=>$c,			

		));

	}



The code is working but it genereates a double entry in postgresql DB, I´ve been struggling for a while an searching on the forum but with no luck so far.

Do you have ajax validation enabled ?

I’m still pretty new to Yii; how do I verify if the ajax validation is enabled for my application?

if you did not do until now, I would recomend you to read the Definitive Guide to Yii - http://www.yiiframework.com/doc/guide/

It will help you to understand all the basics…

Regarding ajax validation, check this property - http://www.yiiframework.com/doc/api/1.1/CActiveForm#enableAjaxValidation-detail

Thank you Maurizio,

After reading your reply i verified my _form view and in fact enableAjaxValidation was set to true.

I did the following modification on my controller and it worked:




public function actionCreate()

	{

		$a=new Persona;

		$b=new Empleado;

		$c=new PersonaTelefono;

		

		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($a);

		$this->performAjaxValidation($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />;

		$this->performAjaxValidation($c);


		if(isset($_POST['Persona'],$_POST['Empleado'],$_POST['PersonaTelefono']))

		{

		...

}

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

			'a'=>$a,

			'b'=>$b,			

			'c'=>$c,			

		));

	}



Great… btw in the above code you put $B instead of $b…