Yii2表格形式数据未插入表中

时间:2017-07-11 06:12:31

标签: yii2 tabular tabular-form

对于项目,我需要根据Invoice Taxes表单中的Purchase order detailsInvoice生成表格形式。当我选择po_number字段时,它会在网址上附加值并刷新名为paticular的pjax div。我已经成功生成了表单,并且还为表格表单部分生成了行并插入到Invoice Taxes表中,但只插入了invoice_idinvoice_amt

这是Invoice

的表单
<div class="invoice-form">

<?php $form = ActiveForm::begin(['id' => 'dynamic-form','options' => ['enctype' => 'multipart/form-data']]);

<div class="col-md-12">

    <div class="col-md-4">
        <?= $form->field($model, 'po_number')->textInput(['readonly' => true,'maxlength' => true]) ?>
    </div>

    <div class="col-md-4">

<?= $form->field($model,'po_number')->dropDownList(
    ArrayHelper::map(purchaseorder::find()->where(['type'=> 2])->all(),'id','po'),
    ['prompt'=>'Select PO ', 'onchange' => '
        var poid;
        poid=$(this).val();  
        //alert(poid); 
        var url = "index.php?r=invoice/create&pod="+poid;
        $.pjax({url: url, container: "#particular"});
    '
    ]);     
?> 

    </div>
    <div class="col-md-4">
    <?= $form->field($model, 'invoice_no')->textInput(['maxlength' => true]) ?>

    </div>
    <div class="col-md-4"> 
    <?= $form->field($model, 'net_amt')->textInput() ?>   
    </div>
</div>

<div class="col-md-12">
    <div class="col-md-4">
    <?php
    if (isset(Yii::$app->request->queryParams['customer'])) {
        $customer= Yii::$app->request->queryParams['customer'];       
        $customers = customer::find()->where(['id'=>$customer])->all();
        $listdata =ArrayHelper::map($customers,'id','name'); 
    ?>
    <?= 
    $form->field($model,'customerid')->widget(Select2::classname(),
        [
           'data' => $listdata,
          //'data' => ArrayHelper::map(customer::find()->where(['id'=>$customer])->One(),'id','name'),
            'language' => 'english',
            'options' => ['placeholder' => 'Customer','id'=>'customer'],
            'pluginOptions' => [
            'allowClear' => true
            ],
        ]);
    ?> 

    <?php

    $serviceid= Yii::$app->request->queryParams['service'];       
    $servi = service::find()->where(['id'=>$serviceid])->all();
    $listdata =ArrayHelper::map($servi,'id','name'); 
    ?>

    <?php
    }
    else
    {
    $cust = new customer();
    $customers = customer::find()->where(['id'=>$cust->id])->all();   
    $listdata =ArrayHelper::map($customers,'id','name');  
    ?>

    <?php
    }
    ?>
    </div>
    <div class="col-md-4">
          <?= $form->field($model, 'po_desc')->textInput(['readonly' => true,'maxlength' => true]) ?>

        <?= $form->field($model, 'status')->dropDownList(['Sent'=>'Sent','On hold'=>'On hold', 'Partially Recieved'=>'Partially Recieved','Fully Paid'=>'Fully Paid']); ?>

    </div>

</div>
<div class='clearfix'></div>
<?php Pjax::begin(['id' => 'particular']) ?>
<div id=''>
<?php
    // **********************************
    // * Here the tabular form part start
    // **********************************
   if ( isset($_GET['pod']) ) {
        $id = $_GET['pod'];
        $modelPods = purchaseorderdetails::findAll(['purchaseorderid' => $id]);
    /*  $length = purchaseorderdetails::find()->where(['purchaseorderid' => $id])->count();
        $x = 0;
        foreach ( $modelPods as $pod ) {
            $tempNet[$x] = $pod->net_amt;
            $x++;
        } */
        foreach ( $modelPods as $pod ) {
        //for ( $i = 0; $i < $length; $i++ ) {
            echo '<div class="" style="border: 1px solid #ccc; border-radius: 2px; padding-top: 5px; margin-bottom:10px;">';
            foreach ( $modelTaxes as $i => $data ) {
?>
    <div class='col-md-2'>
    <?= $form->field($data, '[$i]net_amt')->textInput(['value' => $pod->net_amt]) ?>
    </div>

    <div class='col-md-1'>
    <?= $form->field($data, '[$i]ST')->textInput() ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, '[$i]SBC')->textInput(['maxlength' => true]) ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, '[$i]KKC')->textInput(['maxlength' => true]) ?>
    </div>

    <div class='col-md-1'>
    <?= $form->field($data, '[$i]Vat')->textInput() ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, '[$i]total')->textInput() ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, '[$i]invoice_amt')->textInput() ?>
    </div>

    <?php // $form->field($data, '[]invoice_id')->hiddenInput()->label(false) ?>
<?php
            }
            echo '<div class="clearfix"></div>';
            echo '</div>';
        }
    }
?>
</div>
<?php Pjax::end() ?>

<div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

这是控制器

$model = new invoice(); 
    // $model_po = new purchaseorder(); 
    $modelTaxes = [new InvoiceTaxes];
if ($model->load(Yii::$app->request->post())) {
                $modelTaxes = Model::createMultiple(InvoiceTaxes::classname());
                Model::loadMultiple($modelTaxes, Yii::$app->request->post());

                $p =purchaseorder::find()->where(['id'=>$model->abc])->one();     

                $poid = $model->abc;               
                $model->poid =$poid;
                $model->type ='External';
                $model->po_date = $p->orderdate;
                $po_noo = purchaseorder::find()->where(['id'=>$model->poid])->One();
                $model->po_number =$po_noo->po;

                $valid = $model->validate();
                $valid = Model::validateMultiple($modelTaxes) && $valid;

                $count = invoice::find()->where(['invoice_no' => $model->invoice_no])->count();        
                if($count > 0) {
                    Yii::$app->getSession()->setFlash("error", "Same Invoice No. is already in used, please use another one");
                    return $this->render('create', [
                    'model' => $model,
                ]);
                } 
                if ($flag = $model->save(false)) {
                    foreach ($modelTaxes as $data) {
                        $data->invoice_id = $model->id;
                        if (! ($flag = $data->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                return $this->redirect(['index']);
            } else {
                return $this->render('create', [
                    'model' => $model,

                    'modelTaxes' => (empty($modelTaxes)) ? [new InvoiceTaxes] : $modelTaxes,

                ]);
            }

1 个答案:

答案 0 :(得分:0)

确定!我已经解决了我的问题。这是我第一次实现表格形式,所以我在使用它时遇到了一些麻烦。我重写了代码。 这是表格形式的一部分。

// tabular form for invoice taxes
    if ( isset($_GET['pod']) ) {
        $id = $_GET['pod'];
        if ($model->isNewRecord) {
            $modelPods = purchaseorderdetails::find()->where(['purchaseorderid' => $id]);
            $length = $modelPods->count();
            $modelPods = $modelPods->all();
            $x = 0;
            foreach ( $modelPods as $pod ) {
                $tempAmt[$x] = $pod->net_amt;
                $x++;
            }
            for ( $i = 0; $i < $length; $i++ ) {
                echo '<div class="" style="border: 1px solid #ccc; border-radius: 2px; padding-top: 10px; margin-bottom:10px;">';

                foreach ( $modelTaxes as $data ) {
?>
    <div class='col-md-2'>
    <?= $form->field($data, "[$i]net_amt")->textInput(['value' => $tempAmt[$i], 'disabled' => 'true'])->label(false) ?>
    </div>

    <div class='col-md-1'>
    <?= $form->field($data, "[$i]ST")->textInput(['placeholder' => 'ST'])->label(false) ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, "[$i]SBC")->textInput(['maxlength' => true, 'placeholder' => 'SBC'])->label(false) ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, "[$i]KKC")->textInput(['maxlength' => true, 'placeholder' => 'KKC'])->label(false) ?>
    </div>

    <div class='col-md-1'>
    <?= $form->field($data, "[$i]Vat")->textInput(['placeholder' => 'Vat'])->label(false) ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, "[$i]total")->textInput(['placeholder' => 'Total'])->label(false) ?>
    </div>

    <div class='col-md-2'>
    <?= $form->field($data, "[$i]invoice_amt")->textInput(['placeholder' => 'Invoice Amt'])->label(false) ?>
    </div>

    <?php // $form->field($data, '[]invoice_id')->hiddenInput()->label(false) ?>
<?php
                }

                echo '<div class="clearfix"></div>';
                echo '</div>';
            }
        }
    }

这是控制器代码。控制器代码与发票型号代码混合在一起。

if ($model->load(Yii::$app->request->post())) {

                // For tabular form
                $count = count(Yii::$app->request->post('InvoiceTaxes', []));
                for($i = 1; $i < $count; $i++) {
                    $modelTaxes[] = new InvoiceTaxes();
                }

                Model::loadMultiple($modelTaxes, Yii::$app->request->post());
                Model::validateMultiple($modelTaxes);
                // end tabular form part

                $p =purchaseorder::find()->where(['id'=>$model->abc])->one();     

                $poid = $model->abc;               
                $model->poid =$poid;
                $model->type ='External';
                $model->po_date = $p->orderdate;
                $po_noo = purchaseorder::find()->where(['id'=>$model->poid])->One();
                $model->po_number =$po_noo->po;

                $count = invoice::find()->where(['invoice_no' => $model->invoice_no])->count();        
                if($count > 0) {
                    Yii::$app->getSession()->setFlash("error", "Same Invoice No. is already in used, please use another one");
                    return $this->render('create', [
                    'model' => $model,
                ]);
                } 
                if ($flag = $model->save(false)) {
                    // for tabular form
                    foreach ($modelTaxes as $data) {
                        $data->invoice_id = $model->id;
                        $flag = $data->save(false);
                    // end tabular form
                        if (!($flag)) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }