Cactiveform Saving Multiples Tables In Db

im new at yii and im trying to save datas in different tables on CactiveForm… this is my code on view


<?php

/* @var $this UserController */

/* @var $user User */

/* @var $form CActiveForm */

?>


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'user-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($user, $gunwcuser, $game, $cash); ?>


	<div class="row">

		<?php echo $form->labelEx($user,'user'); ?>

		<?php echo $form->textField($user,'user',array('size'=>16,'maxlength'=>16)); ?>

		<?php echo $form->error($user,'user'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($user,'Gender'); ?>

		<?php echo $form->radioButtonList($user,'Gender', array(0 => 'Male', 1 => 'Female'), array('labelOptions'=>array('style'=>'display:inline'), 'separator'=>' ',)); ?>

		<?php echo $form->error($user,'Gender'); ?>

	</div><br>


	<div class="row">

		<?php echo $form->labelEx($user,'NickName'); ?>

		<?php echo $form->textField($user,'NickName',array('size'=>16,'maxlength'=>16)); ?>

		<?php echo $form->error($user,'NickName'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($user,'Password'); ?>

		<?php echo $form->passwordField($user,'Password',array('size'=>16,'maxlength'=>16)); ?>

		<?php echo $form->error($user,'Password'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($user,'E_Mail'); ?>

		<?php echo $form->textField($user,'E_Mail',array('size'=>50,'maxlength'=>50)); ?>

		<?php echo $form->error($user,'E_Mail'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($user,'Country'); ?>

		<?php echo $form->textField($user,'Country',array('size'=>3,'maxlength'=>3)); ?>

		<?php echo $form->error($user,'Country'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($user->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

there are 4 tables in DB… which are (user, gunwcuser, game, cash). This form is for user table, the gunwcuser is going to have the same data thats put in user form, but not all the data thats being saved on the form is being requested…

this is my controller:


public function actionCreate()

	{

		$user = new User;

		$gunwcuser =new Gunwcuser;

		$game = new Game;

		$cash = new Cash;


		// Uncomment the following line if AJAX validation is needed

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


		$auth = 1;

		$time = '0000-00-00 00:00:00';


		if(isset($_POST['User']))

		{

			


			// Set data column in DB before saving

			$user->Status = '1';

			$user->MuteTime = $time;

			$user->RestrictTime = $time;

			$user->Authority = $auth;

			$user->User_Level = '1';

			$user->Authority2 = $auth;

			$user->attributes=$_POST['User'];


			$_POST['Gunwcuser'] = $_POST['User'];

			$gunwcuser->Status = '1';

			$gunwcuser->MuteTime = $time;

			$gunwcuser->RestrictTime = $time;

			$gunwcuser->Authority = $auth;

			$gunwcuser->User_Level = '1';

			$gunwcuser->Authority2 = $auth;

			$gunwcuser->AuthorityBackup = $auth;

			$gunwcuser->attributes=$_POST['Gunwcuser'];


			$_POST['Game'] = $_POST['User'];





			if($user->save())

				$this->redirect(array('view','id'=>$user->Id));

		}


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

			'user'=>$user, 'gunwcuser'=>$gunwcuser, 'game'=>$game, 'cash'=>$cash,

		));

	}

i tried to copy the attributes from user to gunwcuser but when i save the data from user, i check on the DB to see if also gunwcuser has been saved but it wasnt…

theres also a few data thats going to be the same on game table but most of it, is going to be different, same as cash…

the common data are (id, username, nickname, gender, email, country, password)…

anyone can tell me how to save data in different tables on the same form? also saving different data on the same time

but when i do


$_POST['Gunwcuser'] = $_POST['User'];

it will take everything taken on the user data?

please answer

Hi nosthertus,

As zqrt has pointed out, you have to save the Gunwcuser model instance.




public function actionCreate()

	{

		$user = new User;

		$gunwcuser =new Gunwcuser;

		...


		if(isset($_POST['User']))

		{

			...

			$user->attributes=$_POST['User'];


			// $_POST['Gunwcuser'] = $_POST['User']; // no need to do this

			...

			$gunwcuser->attributes=$_POST['User'];


			if($user->save() && $gunwcuser->save())

				$this->redirect(array('view','id'=>$user->Id));

		}

		...

	}



$_POST is just a container of user inputs.

By doing “$user->attributes=$_POST[‘User’];” we populate the attributes of the user model with the user inputs, and “$user->save()” does the saving to DB. So, the same thing applies to the Gunwcuser.

Note that you should use db transaction in your real app to ensure that both User and Gunwcuser are saved (or not saved).







 if(isset($_POST['User']))

                {

                  // Set data column in DB before saving

                        $user->Status = '1';

                        $user->MuteTime = $time;

                        $user->RestrictTime = $time;

                        $user->Authority = $auth;

                        $user->User_Level = '1';

                        $user->Authority2 = $auth;

                        $user->attributes=$_POST['User'];


                        $gunwcuser->AuthorityBackup = $auth;

                        $gunwcuser->attributes = $_POST['User'];


                        if($user->save() &&  $gunwcuser->save())

                                $this->redirect(array('view','id'=>$user->Id));

                }