Yii Framework Forum: Duplicate Entries Using Cactiveform And Transactions - Yii Framework Forum

Jump to content

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

Duplicate Entries Using Cactiveform And Transactions Rate Topic: -----

#1 User is offline   William Vaquerano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 13

Posted 09 January 2014 - 02:12 AM

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

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 09 January 2014 - 03:36 AM

Do you have ajax validation enabled ?
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   William Vaquerano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 13

Posted 09 January 2014 - 02:49 PM

View PostMaurizio Domba Cerin, on 09 January 2014 - 03:36 AM, said:

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

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 09 January 2014 - 03:11 PM

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.yiiframew...lidation-detail
Find more about me.... btw. Do you know your WAN IP?
1

#5 User is offline   William Vaquerano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 24-November 13

Posted 09 January 2014 - 11:11 PM

View PostMaurizio Domba Cerin, on 09 January 2014 - 03:11 PM, said:

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.yiiframew...lidation-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($B);
		$this->performAjaxValidation($c);

		if(isset($_POST['Persona'],$_POST['Empleado'],$_POST['PersonaTelefono']))
		{
		...
}
$this->render('create',array(
			'a'=>$a,
			'b'=>$b,			
			'c'=>$c,			
		));
	}

0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 10 January 2014 - 03:29 AM

Great... btw in the above code you put $B instead of $b...
Find more about me.... btw. Do you know your WAN IP?
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