yii one form and multiple models

i am a new yii framwrok.

Purpose: Get user input and insert data into Order and OrderDetail table on the one form.

The problem is data inserted only one table, Order table. I have tried to follow the wiki tutorial, but i can’t fix that problem. Why it can’t insert into both tables, Order and OrderDetail? please help to advice for this.

i have five models as below:

Customers(customer_id,customer_name,phone, email, lat,lng,…)

Products(product_id,product_name,price,…)

Order(order_id,customer_id,order_status,staff_id,…)

OrderDetail(order_detail_id,order_id,product_id,qty,…)

Staff(staff_id,staff_name,…)

OrderController.php

public function actionCreate()

{

$model=new Order;


$orderdetail=new OrderDetail();


$product=new Products();





$date=date('y-m-d');





// Uncomment the following line if AJAX validation is needed


//$this->performAjaxValidation(array($model,$orderdetail));





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


{   





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


        $model->lat='12.53';


        $model->lng='13.2';


        $model->completed_date=$date;


        $model->received_date=$date;


        $model->created_date=$date;


        $model->upated_date=$date;











    if($model->save())





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


    $orderdetail->attributes=$_POST['OrderDetail'];


    $orderdetail->order_id= $model->order_id;


    $orderdetail->order_item_status=1;


    $orderdetail->created_date=$date;


    $orderdetail->updated_date=$date;





    if($orderdetail->save())


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


    }





        //$OrderDetailController=new OrderDetail();


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


}


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


    'model'=>$model,


    'orderdetail'=>$orderdetail,


    'product'=>$product,





));

}

views/order/create.php

<?php

$this->breadcrumbs=array(

'Orders'=&gt;array('index'),


'Create',

);

$this->menu=array(

array('label'=&gt;'List Order', 'url'=&gt;array('index')),


array('label'=&gt;'Manage Order', 'url'=&gt;array('admin')),

);

?>

<h1>Create Order</h1>

<?php echo $this->renderPartial(’_form’, array(‘model’=>$model,‘orderdetail’=>$orderdetail,‘product’=>$product)); ?>

views/order/_form.php

<div class="form">

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

'id'=&gt;'order-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'customer_id'); ?&gt;


    &lt;?php //echo &#036;form-&gt;textField(&#036;model,'customer_id'); ?&gt;


    &lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'customer_id',CHtml::listData(Customers::model()-&gt;findAll(),'customer_id','fullname'),


   array('empty' =&gt; '--- Choose---')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'customer_id'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'order_status'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'order_status'); ?&gt;

<?php //echo $form->dropDownList($model,‘order_id’, array(1=>‘Pending’, 2=>‘Processing’,3=>‘Completed’));?>

    &lt;?php echo &#036;form-&gt;error(&#036;model,'order_status'); ?&gt;


&lt;/div&gt;

<?php /*?>

&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'lat'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'lat'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'lat'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'lng'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'lng'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'lng'); ?&gt;


&lt;/div&gt;

<?php */?>

&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'address'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'address',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'address'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(Products::model(),'product_id'); ?&gt;


    &lt;?php //echo &#036;form-&gt;textField(&#036;model,'product_id'); ?&gt;


    &lt;?php echo &#036;form-&gt;dropDownList(Products::model(),'product_id',CHtml::listData(Products::model()-&gt;findAll(),'product_id','product_name'),


   array('empty' =&gt; '--- Choose---')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(Products::model(),'product_id'); ?&gt;


&lt;/div&gt;


&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(OrderDetail::model(),'qty'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(OrderDetail::model(),'qty'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(OrderDetail::model(),'qty'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'received_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'received_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'received_date'); ?&gt;


&lt;/div&gt;

<?php /*?>

&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'completed_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'completed_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'completed_date'); ?&gt;


&lt;/div&gt;

<?php */?>

&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'staff_id'); ?&gt;


    &lt;?php //echo &#036;form-&gt;textField(&#036;model,'staff_id'); ?&gt;


    &lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'staff_id',CHtml::listData(Staff::model()-&gt;findAll(),'staff_id','fullname'),


   array('empty' =&gt; '--- Choose---')); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'staff_id'); ?&gt;


&lt;/div&gt;

<?php /*?>

&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'created_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'created_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'created_date'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


    &lt;?php echo &#036;form-&gt;labelEx(&#036;model,'upated_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;textField(&#036;model,'upated_date'); ?&gt;


    &lt;?php echo &#036;form-&gt;error(&#036;model,'upated_date'); ?&gt;


&lt;/div&gt;

<?php */?>

&lt;div class=&quot;row buttons&quot;&gt;


    &lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

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

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

sorry, if my bad writing. Thanks you so much!

Hi, sakkona

Your form have no field for OrderDetail::product_id because the ‘product_id’ field in your form is not related to OrderDetail but to Products. So it should fail in validation of OrderDetail.

Try change them to …




<div class="row">

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

<?php echo $form->dropDownList($orderdetail,'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),

array('empty' => '--- Choose---')); ?>

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

</div>



And I think you should use the OrderDetail model instance, not OrderDetail::model().




<div class="row">

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

<?php echo $form->textField($orderdetail,'qty'); ?>

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

</div>



It won’t make difference when you are creating a new data, but it will when you are updating an existing data.

And, yes, welcome to the forum and have fun!

oh, great it works.

Thanks,for your solution!