Form Data To Two Tables

Hi, I am new member. I want to add form data to two tables. The main data is ‘nasabah’ (english: client), and ‘rekening’ for other data (english: bank account).

I create it with Gii. The code I added was bolded.

Controller:




	public function actionCreate()

	{

		$model=new Nasabah;

		[b]$rekening = new Rekening;[/b]


		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

			[b]'rekening'=>$rekening,[/b]

		));

	}



View:




<h1>Create New Nasabah</h1>


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



Originally it’s use _form, but I create _form_create, because for edit: nasabah and rekening at their individuals form.

_form_create.php




<div class="form">


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

	'id'=>'nasabah-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,'nama_lengkap'); ?>

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

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

	</div>


	<div class="row">

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

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

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

	</div>


<!-- other data //-->


<!-- Rekening data start //-->

<h1>Data Rekening</h1>




	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php echo $form->dropDownList($rekening,'jenis_tabungan', $rekening->getJenisTabunganOptions()); ?>

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

	</div>


<!-- Rekening data end //-->


	<div class="row buttons">

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

	</div>


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



The result is, below <h1>Data Rekening</h1> shown:

Fatal error: Call to a member function isAttributeRequired() on a non-object in C:\Program Files\xampp\htdocs\yii\framework\web\helpers\CHtml.php on line 1414

You do not pass the second model to the partial view ‘_form_create’.

In controller




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

                        'model'=>$model,

                        'rekening'=>$rekening

                ));



in view




<?php echo $this->renderPartial('_form_create', array('model'=>$model,'rekening'=>$rekening)); ?>



Oh yeah, it’s work. Thank you.