Yii Framework Forum: yii one form and multiple models - Yii Framework Forum

Jump to content

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

yii one form and multiple models get data user input and save Rate Topic: -----

#1 User is offline   sakkona 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 20-January 12

Posted 02 February 2012 - 10:55 PM

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'=>array('index'),
'Create',
);

$this->menu=array(
array('label'=>'List Order', 'url'=>array('index')),
array('label'=>'Manage Order', 'url'=>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'=>'order-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,'customer_id'); ?>
<?php //echo $form->textField($model,'customer_id'); ?>
<?php echo $form->dropDownList($model,'customer_id',CHtml::listData(Customers::model()->findAll(),'customer_id','fullname'),
array('empty' => '--- Choose---')); ?>
<?php echo $form->error($model,'customer_id'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'order_status'); ?>
<?php echo $form->textField($model,'order_status'); ?>
<?php //echo $form->dropDownList($model,'order_id', array(1=>'Pending', 2=>'Processing',3=>'Completed'));?>

<?php echo $form->error($model,'order_status'); ?>
</div>

<?php /*?>
<div class="row">
<?php echo $form->labelEx($model,'lat'); ?>
<?php echo $form->textField($model,'lat'); ?>
<?php echo $form->error($model,'lat'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'lng'); ?>
<?php echo $form->textField($model,'lng'); ?>
<?php echo $form->error($model,'lng'); ?>
</div>
<?php */?>
<div class="row">
<?php echo $form->labelEx($model,'address'); ?>
<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'address'); ?>
</div>

<div class="row">
<?php echo $form->labelEx(Products::model(),'product_id'); ?>
<?php //echo $form->textField($model,'product_id'); ?>
<?php echo $form->dropDownList(Products::model(),'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),
array('empty' => '--- Choose---')); ?>
<?php echo $form->error(Products::model(),'product_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx(OrderDetail::model(),'qty'); ?>
<?php echo $form->textField(OrderDetail::model(),'qty'); ?>
<?php echo $form->error(OrderDetail::model(),'qty'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'received_date'); ?>
<?php echo $form->textField($model,'received_date'); ?>
<?php echo $form->error($model,'received_date'); ?>
</div>
<?php /*?>
<div class="row">
<?php echo $form->labelEx($model,'completed_date'); ?>
<?php echo $form->textField($model,'completed_date'); ?>
<?php echo $form->error($model,'completed_date'); ?>
</div>
<?php */?>
<div class="row">
<?php echo $form->labelEx($model,'staff_id'); ?>
<?php //echo $form->textField($model,'staff_id'); ?>
<?php echo $form->dropDownList($model,'staff_id',CHtml::listData(Staff::model()->findAll(),'staff_id','fullname'),
array('empty' => '--- Choose---')); ?>
<?php echo $form->error($model,'staff_id'); ?>
</div>

<?php /*?>
<div class="row">
<?php echo $form->labelEx($model,'created_date'); ?>
<?php echo $form->textField($model,'created_date'); ?>
<?php echo $form->error($model,'created_date'); ?>
</div>

<div class="row">
<?php echo $form->labelEx($model,'upated_date'); ?>
<?php echo $form->textField($model,'upated_date'); ?>
<?php echo $form->error($model,'upated_date'); ?>
</div>
<?php */?>



<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

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

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

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

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 02 February 2012 - 11:45 PM

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.

Quote

<div class="row">
<?php echo $form->labelEx(Products::model(),'product_id'); ?>
<?php //echo $form->textField($model,'product_id'); ?>
<?php echo $form->dropDownList(Products::model(),'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),
array('empty' => '--- Choose---')); ?>
<?php echo $form->error(Products::model(),'product_id'); ?>
</div>


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

#3 User is offline   sakkona 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 20-January 12

Posted 03 February 2012 - 01:13 AM

oh, great it works.
Thanks,for your solution!
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