2017-07-11 17 views
0

プロジェクトの場合、InvoiceフォームのPurchase order detailsに基づいてInvoice Taxesの表形式を生成する必要があります。 po_numberフィールドを選択すると、URLに値が追加され、paticularという名前のpjax divがリフレッシュされます。私は正常にフォームを生成し、行もテーブル形式の部分のために生成され、Invoice Taxesテーブルに挿入されているが、invoice_idinvoice_amtだけが挿入されています。Yii2テーブル形式のデータがテーブルに挿入されていない

はここ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, 

       ]); 
      } 

答えて

0

[OK]をコントローラーです!私は私の問題を解決しました。私が表形式を実装していたのはこれが初めてのことでしたので、問題が発生していました。私はコードを書き直しました。 ここに表形式の部分があります。

// 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; 
         } 
        } 
       } 
関連する問題