how to create 1 view 2 model

i have 2 model (header and detail) 1 view (create) 1 controller (invoiceController)

how a way out of this problem?

error–>Undefined variable: id

header.php


<?php

 class header extends CActiveRecord{

	

	

    //put your code here

     

    public static function model($className = __CLASS__) {

        parent::model($className);

    }

     

    public function tablename(){

        return 'invoice_header';

    }

     

    public function attributeLabels() {

        return array('id_header'=>'id_header','no_invoice'=>'no_invoice','date'=>'date','descrip'=>'descrip','total'=>'total');

    }

    

 public function search()

 {

 $criteria=new CDbCriteria;

 $criteria->compare('id_header',$this->id_header);

 $criteria->compare('no_invoice',$this->no_invoice,true);

  $criteria->compare('date',$this->date,true);

   $criteria->compare('descrip',$this->descrip,true);

 $criteria->compare('total',$this->total,true);





 return new CActiveDataProvider(get_class($this), array(

     'criteria'=>$criteria,

));

 }

   

public function rules()

	{

		return array(

			array('id_header, no_invoice', 'required'),

			 array('id_header, no_invoice, date, descrip, total','safe', 'on'=>'search'),

					);

	}

}

 

?>




detail.php




<?php

 class detail extends CActiveRecord{


	

    //put your code here

     

    public static function model($className = __CLASS__) {

        parent::model($className);

    }

     

    public function tablename(){

        return 'invoice_detail';

    }

     

    public function attributeLabels() {

        return array('id_detail'=>'id_detail','id_header'=>'id_header','id_produk'=>'id_produk','qty'=>'qty','price'=>'price','total_price'=>'total_price');

    }

    

 public function search()

 {

 $criteria=new CDbCriteria;

  $criteria->compare('id_detail',$this->id_detail,true);

 $criteria->compare('id_header',$this->id_header,true);

 $criteria->compare('id_produk',$this->id_produk,true);

  $criteria->compare('qty',$this->qty,true);

 $criteria->compare('price',$this->price,true);

 $criteria->compare('total_price',$this->total_price,true);





 return new CActiveDataProvider(get_class($this), array(

     'criteria'=>$criteria,

));

 }

   

public function rules()

	{

	//	return array(

		//	array('id_detail,id_header,id_produk, qty', 'required'),

			// array('id_detail, id_header, id_produk, qty, price,total_price','safe', 'on'=>'search'),

				//	);

	}

}

 

?>




create.php




<?php

/* @var $this SiteController */

/* @var $model LoginForm */

/* @var $form CActiveForm  */


$this->pageTitle=Yii::app()->name . ' - create';

$this->breadcrumbs=array(

	'create',

);

?>

<div class="form">

	<?php

	echo CHtml::beginForm(array('invoice/create'));

	?>

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

	'id'=>'login-form',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>

    




	<div class="">

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

		<?php echo $form->textField($model,'no_invoice'); ?>

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

	</div>

	<div class="">

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

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

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

            'name'=>'header[date]',

            'value'=>$model->date,

            'options'=>array(

                'showAnim'=>'fold',

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

                 'changeMonth'=> 'true',

                'changeYear'=> 'true',

            ),

        ));

         ?>

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

	</div>

	<div class="">

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

		<?php echo $form->textfield($model,'descrip'); ?>

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

	</div>	

	


	

	<?php

$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'user-grid',

   'dataProvider'=>$model2->search(),

   'filter'=>$model2,

   'columns'=>array(

	'id_produk',

	'qty',

	'total_price',

   ),

));

?>




	<div class="">

		<?php echo CHtml::submitButton('Create');

			  echo CHtml::endForm();

		?>

	</div>

    

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

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



invoiceController.php




<?php

 

class InvoiceController extends Controller

{

	public $layout='column1';

    const INDEX = 'index';




	 public function actionCreate()

	{

		$model=new header;

		$data=new detail;

     $model2=$data->findByPk($id);

     

     

		// if it is ajax validation request

		if(isset($_POST['header'])){

		 $model->id_header= $_POST['header']['id_header'];

            $model->no_invoice= $_POST['header']['no_invoice'];

            $model->date = $_POST['header']['date'];

            $model->descrip = $_POST['header']['descrip'];

     		$model->total = $_POST['header']['total'];

			$model->save();

			

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

			$model->getErrors();

        }// display the login form

		if(isset($_POST['detail'])){

          $model2->id_detail = $_POST['detail']['id_detail'];

		  $model2->id_header = $_POST['detail']['id_header'];

		  $model2->id_produk = $_POST['detail']['id_produk'];

		  $model2->qty = $_POST['detail']['qty'];

		  $model2->price = $_POST['detail']['price'];

		  $model2->total_price = $_POST['detail']['total_price'];

		  

		  $model2->save();

		  $model2->getErrors();

     }

		$this->render('create',array('model'=>$model,'model2'=>$model2));

	}

	

	public function actionView($id){

     $data=new header;

     $model=$data->findByPk($id);

         

    // if it is ajax validation request

		if(isset($_POST['header'])){

            $model->id_header = $_POST['header']['id_header'];

            $model->no_invoice= $_POST['header']['no_invoice'];

            $model->date = $_POST['header']['date'];

            $model->descrip = $_POST['header']['descrip'];

            $model->total = $_POST['header']['total'];

		

     }

         

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

}

	public function actionUpdate($id){

     $data=new header;

     $model=$data->findByPk($id);

         

      // if it is ajax validation request

		if(isset($_POST['header'])){

           

            $model->no_invoice= $_POST['header']['no_invoice'];

            $model->date = $_POST['header']['date'];

            $model->descrip = $_POST['header']['descrip'];

			

			   

          if($model->save()){

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

          }

     }

         

     $this->render('update',array('model'=>$model));

}


public function actionDelete($id){

     $model = new header;

     if($model->deleteByPk($id)){

          $this->redirect(array(self::INDEX));

     }else{

        throw new CHttpException(404,'Data yang di minta tidak tersedia');

     }

}


	/**

	 * Declares class-based actions.

	 */

	public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			// page action renders "static" pages stored under 'protected/views/site/pages'

			// They can be accessed via: index.php?r=site/page&view=FileName

			'page'=>array(

				'class'=>'CViewAction',

			),

		);

	}


	/**

	 * This is the default 'index' action that is invoked

	 * when an action is not explicitly requested by users.

	 */

	

	

	


	public function actionIndex(){

            $model = new header('search');

			$model->unsetAttributes();

			if(isset($_GET['header']))

			$model->attributes=$_GET['header'];

            $data= $model->findAll();

            $this->render('index',array('model'=>$model));

		

    }

	/**

	 * This is the action to handle external exceptions.

	 */

	public function actionError()

	{

		if($error=Yii::app()->errorHandler->error)

		{

			if(Yii::app()->request->isAjaxRequest)

				echo $error['message'];

			else

				$this->render('error', $error);

		}

	}


}

Assuming that this is the line causing your error:


 $model2=$data->findByPk($id);

There is no $id defined there. Your “view” action gets an id as a parameter, your “create” doesn’t.

When posting sourcecode, please use the "code" tag, not the "quote".