在我的Web应用程序中,我需要从单个表单中的两个模型中收集数据。第一个模型表单类似于用于更改现有数据的更新操作,第二个表单用于创建新记录。下面是我尝试的代码,但是当我单击“保存”按钮时它不保存和重定向而只是停留在同一页面中,我所做的更改将恢复为之前的I模型值和第二个模型属性正在变为空。
我的控制器代码
public function actionBookvegetable($id){
$BookVegetable=new BookVegetable;
$model=$this->loadModel($id);
if(isset($_POST['ProducerOffer'])AND (isset($_POST['BookVegetable'])) ) {
$model->attributes=$_POST['ProducerOffer'];
$BookVegetable->attributes=$_POST['BookVegetable'];
if($ProducerOffer->validate() AND $BookVegetable->validate()) {
$BookVegetable->save();
$BookVegetable->booked_by=Yii::app()->user->id;
$BookVegetable->producer_offer_id=$model->id;
$model->save();
}
if (($model->hasErrors() === false)&&($BookVegetable->hasErrors===false))
{
$this->redirect(Yii::app()->user->returnUrl);
}
}
else
{
Yii::app()->user->setReturnUrl($_GET['returnUrl']);
}
$this->render('book',array('model'=>$model,'BookVegetable'=>$BookVegetable));
}
我的视图表单代码。
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array('id'=>'non-ajax_form','enableAjaxValidation'=>false,)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model,$BookVegetable); ?>
<?php echo "<br>" ?>
<?php echo $form->dropDownList($model,'vegetable_id', CHtml::listData(Vegetable::model()->findAll(),'id', 'name'), array('prompt'=>'Select Vegetable')); ?>
<?php echo CHtml::encode($model->getAttributeLabel('offered_qty')); ?>
<?php echo CHtml::textField("offered_qty",$model->offered_qty,array('readonly'=>true)); ?>
<?php echo CHtml::encode($model->getAttributeLabel('unit_cost')); ?>
<?php echo CHtml::textField("unit_cost",$model->unit_cost,array('readonly'=>true)); ?>
<?php echo CHtml::encode($model->getAttributeLabel('unit_delivery_cost')); ?>
<?php echo CHtml::textField("unit_delivery_cost",$model->unit_delivery_cost,array('readonly'=>true)); ?>
<?php echo CHtml::textField("booked_quantity",$BookVegetable->booked_quantity); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'primary', 'label'=> 'Save',)); ?
我应该如何解决这个问题任何人帮助我。我无法调试错误来源
答案 0 :(得分:1)
在分配请求的值之前保存值。
尝试使用此代码,如果有效:
public function actionBookvegetable($id){
$BookVegetable=new BookVegetable;
$model=$this->loadModel($id);
if(isset($_POST['ProducerOffer'])AND (isset($_POST['BookVegetable'])) )
{
$model->attributes=$_POST['ProducerOffer'];
$BookVegetable->attributes= $_POST['BookVegetable'];
/*** The below two lines should be before "$BookVegetable->save()" function ***/
$BookVegetable->booked_by=Yii::app()->user->id;
$BookVegetable->producer_offer_id=$model->id;
/*** Validate() function will check for the errors **/
if($ProducerOffer->validate() AND $BookVegetable->validate())
{
$BookVegetable->save();
$model->save();
$this->redirect(Yii::app()->user->returnUrl);
}
/***There is no need to check again for the errors as validate() will do that, So you can comment this three lines ***/
/**if (($model->hasErrors() === false)&&($BookVegetable->hasErrors===false))
{
$this->redirect(Yii::app()->user->returnUrl);
} */
}
else
{
Yii::app()->user->setReturnUrl($_GET['returnUrl']);
}
$this->render('book',array('model'=>$model,'BookVegetable'=>$BookVegetable));
}
尝试更改视图文件中的这些输入文本字段:
<?php echo $form->dropDownList($model,'vegetable_id', CHtml::listData(Vegetable::model()->findAll(),'id', 'name'), array('prompt'=>'Select Vegetable')); ?>
/*** The first field for below input text field is for "name" of the text fields, so this must follow "ModelName[field_name]" format **/
<?php echo CHtml::textField("ProducerOffer[offered_qty]",$model->offered_qty,array('readonly'=>true)); ?>
<?php echo CHtml::textField("ProducerOffer[unit_cost]",$model->unit_cost,array('readonly'=>true)); ?>
<?php echo CHtml::textField("ProducerOffer[unit_delivery_cost]",$model->unit_delivery_cost,array('readonly'=>true)); ?>
<?php echo CHtml::textField("BookVegetable[booked_quantity]",$BookVegetable->booked_quantity); ?>
我希望这肯定有用.. :)