Yii Framework Forum: Collect data for two models in One Controller - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Collect data for two models in One Controller Rate Topic: -----

#1 User is offline   KCChing 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-October 10

Posted 25 October 2010 - 11:01 PM

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.yiiframew...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
0

#2 User is offline   steffen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 14-December 09
  • Location:Reutlingen, DE

Posted 26 October 2010 - 02:01 AM

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
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,354
  • Joined: 12-October 09
  • Location:Croatia

Posted 26 October 2010 - 02:03 AM

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?
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   KCChing 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-October 10

Posted 01 November 2010 - 10:53 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users