Update For Two Table From One Form

Hi all, I am new to Yii Framework and now, I am facing a problem of updating details for two tables from one submitted form. I have two tables of User and Student. I wish to update the username from the submitted form (Student table) and update the User table as well. Previously, I have updated the Student table successfully but not the User table. How to update both table at the same time? I follow the link here example but it does not works to me. Please help me~ Thanks in advance.

** id (student table) = id_num (user table) **

Here’s my database:

[color="#FF0000"]User table[/color]

id | id_num | user type | username | password |

10 | 12 | student | aaa | 123 |

[color="#FF0000"]Student table[/color]

id | username | password |

12 | aaa | 123 |

Here’s my code:

[color="#FF0000"]controller:[/color]




public function actionUpdate($id)

	{

		$model=$this->loadModel($id);

		$model2=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

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

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

        {

		    $model->attributes=$_POST['Student'];

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

            $model2->username = $model->username;

			

			$model->save();

            $model2->save(); 

			

            $uploadedFile=CUploadedFile::getInstance($model,'foto');

 

                if(is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile')  // check if uploaded file is set or not

                { 

                    $model->foto = $uploadedFile;

                }

			if($model->save())

            {	

				if(is_object($uploadedFile))

				{

				    $model->foto->saveAs(Yii::app()->basePath.'/../images/'.$model->foto);

				}

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

		    }

		}


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

			'model'=>$model,

			'model2'=>$model2,

		));

	}



[color="#FF0000"]model[/color]




public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('foto, username, password, programme, intake, email, contact', 'required'),

			array('contact', 'numerical', 'integerOnly'=>true),

			array('username', 'length', 'max'=>10),

			array('password, programme, intake', 'length', 'max'=>30),

			array('email', 'length', 'max'=>50),

			array('foto', 'length', 'max'=>255, 'on'=>'insert,update'),

			array('foto', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, username, password, programme, intake, email, contact', 'safe', 'on'=>'search'),

		);

	}



Hello,

you may do like,




                $model=Student::model()->loadModel($id);

                $model2=User::model()->findByAttributes(array('id_num'=>$model->id));

                

instead of





                $model=$this->loadModel($id);

                $model2=$this->loadModel($id);



Thanks

Hi, Ghanshyam. Thanks for the reply. I have tried the code that u suggested but it returned an error of '[color="#FF0000"]Student and its behaviors do not have a method or closure named “loadModel”[/color]. ’ And I can’t update both student and user table too. What’s wrong with my code? Is there anything I miss out?

I wish to perform like this:

How to implement this by using Yii Framework?

Thanks.

anyone has any idea how to solve this problem? please help me~ thanks. :blink:

Hi,

If $id is primary key so,




$model=Student::model()->findByPk($id);



Or




$model=Student::model()->find('LOWER(id)=?',array($id));

$model2=User::model()->findByAttributes(array('id_num'=>$model->id));



Thanks.

Hi, Ghanshyam. U are right. $id is my auto increment and primary key for my student table. However, it is still not working properly after I have changed the code. The update page does not redirect to the view page. It is just remained to the update page after I click the Save button to submit the update form. Anything that I miss out? Thanks.




public function actionUpdate($id)

	{

		$model=Student::model()->findByPk($id);

        $model2=User::model()->findByAttributes(array('id_num'=>$model->id));




		// Uncomment the following line if AJAX validation is needed

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

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

        {

		    $model->attributes=$_POST['Student'];

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

            

			$model2->username = $model->username;

			$model2->password = $model->password;

			

			$model->save();

            $model2->save(); 

			

            $uploadedFile=CUploadedFile::getInstance($model,'foto');

 

                if(is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile')  // check if uploaded file is set or not

                { 

                    $model->foto = $uploadedFile;

                }

			if($model->save() && $model2->save())

            {	

				if(is_object($uploadedFile))

				{

				    $model->foto->saveAs(Yii::app()->basePath.'/../images/'.$model->foto);

				}

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

		    }

		}


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

			'model'=>$model,

			'model2'=>$model2,

		));

	}



view.php




<?php

/* @var $this StudentController */

/* @var $model Student */


$this->breadcrumbs=array(

	'Students'=>array('index'),

	$model->id,

);


$this->menu=array(

	array('label'=>'List Student', 'url'=>array('index')),

	array('label'=>'Create Student', 'url'=>array('create')),

	array('label'=>'Update Student', 'url'=>array('update', 'id'=>$model->id)),

	array('label'=>'Delete Student', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),

	array('label'=>'Manage Student', 'url'=>array('admin')),

);

?>


<h1>View Student #<?php echo $model->id; ?></h1>


<?php echo CHtml::image(Yii::app()->request->baseUrl.'/images/'.$model->foto,"foto" ,array('style'=>'margin-left:200px; width:300px; height:300px;'));?>

<br/><br/>


<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'username',

		'password',

		'programme',

		'intake',

		'email',

		'contact',

	),

)); ?>




_view.php




<?php

/* @var $this StudentController */

/* @var $data Student */

?>


<div class="view">

	

	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />

	

	<b><?php echo CHtml::encode($data->getAttributeLabel('username')); ?>:</b>

	<?php echo CHtml::encode($data->username); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('password')); ?>:</b>

	<?php echo CHtml::encode($data->password); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('programme')); ?>:</b>

	<?php echo CHtml::encode($data->programme); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('intake')); ?>:</b>

	<?php echo CHtml::encode($data->intake); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('email')); ?>:</b>

	<?php echo CHtml::encode($data->email); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('contact')); ?>:</b>

	<?php echo CHtml::encode($data->contact); ?>

	<br />

</div>



The wiki section of the website has more than 1 article on this subject. I recommend taking a look there.

Hi, Boaz. Ya, i did a lot of research on how to update details for two tables form one submitted form but I’m still having problem with it. I couldn’t get the results that I wish to have. Do I miss out anything? Please help me~ Thank you so much.

Does anyone has the same situation with me? Anyone can guide me how to solve this? Thanks.