Help with single form to update multiple tables please.

I have followed the example here Yii Wiki but the result i am getting is an empty page followed by the Application Log.

I have two tables users and businesses, users contains personal info and businesses contains the username/password data.

My FK is set in the database where the PK user_id is a FK in businesses.

My code is as follows

UsersController.php


public function actionCreate()

	{

		$users = new Users;

		$businesses = new Businesses;

		

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

		{

			// populate input data to $users and $businesses

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

			$businesses->attributes=$_POST['Businesses'];

			

			// validate both $users and $businesses

			$valid=$users->validate();

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

			

			if($valid)

			{

				$users->save();

				$businesses->save();

			}

			else

			{

				// redirect user to form

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

			}

			

			// sign new user in and redirect to private area

			$this->render('private/index', array(

			'users'=>$users,

			'businesses'=>$businesses,

			));

		}

	}

Users.php to show my relationships


public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'accounts' => array(self::HAS_MANY, 'Accounts', 'user_id'),

			'businesses' => array(self::HAS_MANY, 'Businesses', 'user_id'),

			'country' => array(self::BELONGS_TO, 'Countries', 'country_id'),

		);

	}

users/_form.php


<?php echo CHtml::beginForm() ?>


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


	<?php echo CHtml::errorSummary(array($users, $businesses)); ?>


	<div class="row">

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

		<?php echo $form->textField($users, 'sponsor_id'); ?>

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

	</div>

	

	<div class="row">

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

		<?php echo $form->textField($businesses, 'username'); ?>

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

	</div>

	

	<div class="row">

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

		<?php echo $form->textField($businesses, 'password'); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'email',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'first_name',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'last_name',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'phone'); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'street',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'city',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($users,'zip',array('size'=>60,'maxlength'=>255)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->dropDownList($users,'country_id', CHtml::listData(Countries::model()->findAll(), 'country_id', 'country_name'), array('empty'=>'Please Select a Country')); ?>

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

	</div>


	<div class="row buttons">

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

	</div>


<?php echo CHtml::endForm(); ?>

and finally users/create.php


<?php echo $this->renderPartial('_form', array('users'=>$users, 'businesses'=>$businesses)); ?>

Because I’m not getting any PHP errors I can only conclude it has to do with the Html render part of the example.

Could someone help me with this issue please…

You’re sure that the variables $_POST[‘Users’] and $_POST[‘Businesses’] are set?

First problem I can spot is that you don’t pass a $model variable to the form view which is used on the following lines:




<div class="row">

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

        <?php echo $form->textField($model,'street',array('size'=>60,'maxlength'=>255)); ?>

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

</div>



You should set YII_DEBUG to true on the first line in your entry script (index.php) to see the proper trace stack if you haven’t already.

I started again and it now works, the only difference i made was to the UserController.


public function actionCreate()

	{

		$users=new Users;

		$businesses=new Businesses;


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			$businesses->attributes=$_POST['Businesses'];

			if($users->save())

				$this->redirect(array('private/index'));

		}


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

			'users'=>$users, 'businesses'=>$businesses,

		));

	}

Perhaps it was the validate code i had in there, and also yes, i changed the $model to $users in the form first but still no change until i removed the validate code…

Can anyone work out what i had done wrong, its one thing to fix something but another to understand how you fixed it :)

Could you post your validation rules for Businesses?