Collect data for two models in One Controller

Hi all,

I’m new to Yii Framework and wish to use it to develop a web application.

Currently I’m using Yii 1.1.4 and encountered some problem with two models in one controller.

I refer to this

http://www.yiiframework.com/wiki/19/

and

http://www.yiiframework.com/forum/index.php?/topic/10698-many-models-in-one-controller/

but still having the same error : Undefined variable: email.

Below is the actionCreate() for my PersonalController:


	public function actionCreate()

	{

		$model=new Personal;

                $email=new Email; //I already create the Model for Email

                $contact=new Contact; //I already create the Model for Contact

                


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Personal'], $_POST['Contact'], $_POST['Email']))

		{

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

                        $contact->attributes=$_POST['Contact'];

                        $email->attributes=$_POST['Email'];

                        

			if($model->save() && $contact->save() && $email->save() )

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

		}


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

			'model'=>$model,

                        'contact'=>$contact,

                        'email'=>$email

		));

	}

In my _form :


<div class="form">

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

        'id'=>'personal-form',

	'enableAjaxValidation'=>false,        

)); ?>


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


	<?php echo $form->errorSummary($model) ; ?>


	<div class="row">

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

		<?php echo $form->textArea($model,'name',array('rows'=>2, 'cols'=>50)); ?>

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

	</div>


	<div class="row">

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

		<?php                 

                    echo $form->dropDownList($model,'nationality',$model->getCountryOptions());

                ?>

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

	</div>


	<div class="row">

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

		<?php 

                    //echo $form->textField($model,'gender');

                    echo $form->dropDownList($model,'gender',$model->getGenderOptions());

                ?>

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

	</div>


	<div class="row">

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

		<?php 

                    //echo $form->textField($model,'birthday');

                /*

                 * Use the build-in Date Picker

                 */

                $this->widget('zii.widgets.jui.CJuiDatePicker', array(

                    'name'=>'Personal[birthday]',

                    'model'=>$model,                    

                    'value'=>$model->birthday,

                    // additional javascript options for the date picker plugin

                    'options'=>array(

                        'showAnim'=>'fold',

                        'changeYear'=>'true',

                        'changeMonth'=>'true',

                        'yearRange'=>'1910:2100',

                        'dateFormat'=>'yy-mm-dd',

                    ),

                    'htmlOptions'=>array(

                        'style'=>'height:20px;',

                        'size'=>'10',

                    ),                    

                ));

?>


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

	</div>


<!-- This is the Email Model           -->

        <div class="row">		

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>




	<div class="row buttons">

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

	</div>


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


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

But the problem is, I always get an error with

Undefined variable: email

I separate Email and Contact from the Personal for the reason of one person might have a lot of contact numbers and emails.

So my database for email will have

id(PK),

personalId(FK),

email

The same goes to contact database:

id(PK),

personalId(FK),

number

Is my way correct to deal with this condition? Or I’m heading the wrong direction?

Thanks and regards,

KC

Hi KC,

did you add the email var to be passed to the sub form in your views/personal/create.php?




<?php echo $this->renderPartial('_form', array('model'=>$model, 'email'=>$email, 'contact'=>$contact)); ?>



cheers

Steffen

Would help if you would write what is the line number of the error and what is on that line?

I suppose it’s on the view where you use $email->email

Do you have the $email->email attribute in the Email model?

Thanks to steffen,

adding email var to be passed to the sub form in your views/personal/create.php works for me!

and of course mdomba for helping me out with this problem.