简体   繁体   English

Yii从两个模型收集数据成单一形式

[英]Yii collecting data from two models into single form

In my web application I need to collect data from two models in a single form. 在我的Web应用程序中,我需要以单一形式从两个模型收集数据。 The first model form is like an update action for making changes in existing data and the second form is for creating a new record. 第一种模型形式类似于用于更改现有数据的更新操作,而第二种形式则用于创建新记录。 Below is the code I tried but when I click "save" button Its not saving and redirecting instead it is just staying in the same page and the changes I made are reverting back to their previous values for I model and second model the attribute are becoming empty. 下面是我尝试的代码,但是当我单击“保存”按钮时,它不保存并重定向,而是停留在同一页面中,并且所做的更改将恢复为我的模型和第二个模型的先前值,该属性将变为空。

My code for the controller 我的控制器代码

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));
        }

My code for the view form. 我的视图表单代码。

<?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',)); ?

How should I resolve this Anybody help me out .I am unable to debug the source of error 我应该如何解决这个问题任何人都可以帮助我。我无法调试错误源

You are saving the values before assigning the requested values. 您要在分配请求的值之前保存这些值。

Try this code, if it works: 如果可行,请尝试以下代码:

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));
}

Try changing these input text fields in your view file: 尝试更改视图文件中的这些输入文本字段:

<?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); ?>

I hope this would definitely work.. :) 我希望这一定可以.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM