how to insert data of a single form into multiple models(table)

hi all,

I am a beginner in yii.I have 2 tables users and memberinfo

now i want username and password entered in memberinfo to be stored in users table.

i have my code here

controller code:




public function actionCreate()

	{

		$model=new Memberinfo;


		// Uncomment the following line if AJAX validation is needed

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

          $user=new Users;


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

		{

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

			if($model->save())

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

		}

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

		{

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

			  			 if($user->save())

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


		}

          $valid=$model->validate();

        $valid=$user->validate() && $valid;


        if($valid)

        {

            // use false parameter to disable validation

            $model->save(false);

            $user->save(false);

            // ...redirect to another page

        }




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

			'Users'=>$user,


			'model'=>$model,

		));

	}



form code:




<tr>

    <div class="row">

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

		<td><?php echo $form->textField($user,'username',array('size'=>45,'maxlength'=>45)); ?>   </td>


	</div> </tr>



now error i am getting is "Undefined variable: user" and this is being shown in _form.php

as <td><?php echo $form->labelEx($user,‘username’); ?> </td>

plz help me

Take a look at this wiki.

I have done the same but its not working for me.please help

Is not the same, the code of the controller is not correct.

You should collect the data toghether and call only once render, passing both variables. Take carefully a look to the wiki.

i tried with that but my error remains the same

it says "undefined variable:user"

is something wrong with code?

You pass the user to the view create, you have to pass to the view _form to.

In the view create:


<?php $tis->renderPartial('_form', array('model'=>$model, 'user'=>$user)); ?>