通过Yii2表单插入数据时获取空值

时间:2016-04-22 11:51:50

标签: javascript yii2

我在生产表单中添加了一个textinput字段。当我选择productname字段下拉列表时,单位价格会填满。但是当我保存数据时,我收到了以下错误 -

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'unitprice' cannot be null
The SQL being executed was: INSERT INTO `bottle` (`usedate`, `useqty`, `productname`, `bottlename`, `unitprice`) VALUES ('2016-04-21', '12', 'CEFO', 'Enter', NULL)

最后一个“NULL”是unitprice的值。 actionController中的actionCreate-

 public function actionCreate()
    {
        $model = new Production();
        $productname = new Productnames();
        $bottle = new Bottle();
        $bottlename = new Bottlename();

        if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post()))
        {
            $model->save();
            //$bottle->attributes = $model->attributes;
            $bottle->usedate = $model->productiondate;
            $bottle->useqty = $model->prodqty;
            $bottle->productname = $model->productname;
            $bottle->bottlename = $productname->bottletype;
            $bottle->unitprice = $bottlename->unitprice;
            // $employee->emp_mobile = $model->emp_mobile;
            $bottle->save();
            return $this->redirect(['create']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'bottle' => $bottle,
                'productname' => $productname,
                'bottlename' => $bottlename,
            ]);
        }
    }

生产_form

<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\web\View;
use frontend\assets\XyzAsset;
use yii\helpers\ArrayHelper;
use dosamigos\datepicker\DatePicker;
use kartik\select2\Select2;
use frontend\modules\production\models\Productbatch;
use frontend\modules\production\models\Productnames;
use kartik\depdrop\DepDrop;
use yii\helpers\Json;
use frontend\modules\production\models\Bottlename;


//XyzAsset::register($this);
/* @var $this yii\web\View */
/* @var $model frontend\modules\production\models\Production */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="production-form">


    <?php $form = ActiveForm::begin(); ?>


    <!--<?= Html::a('Select Product', ['/production/productbatch/index'], ['class'=>'btn btn-primary']) ?> -->

    <?= $form->field($model, 'productiondate')->widget(
    DatePicker::className(), [
        // inline too, not bad
         'inline' => false, 
         // modify template for custom rendering
        //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'yyyy-mm-dd'
        ]
    ]);?>

    <!-- echo CHtml::button("(+)",array('title'=>"Select Product",'onclick'=>'js:selectproductforproduction();')); -->   

    <?= $form->field($model, 'productname')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
    'language' => 'en',
    'options' => ['placeholder' => 'Select Product Name', 'id' => 'catid'],
    'pluginOptions' => [
        'allowClear' => true
    ],
    ]); ?>

    <?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
    'options'=>['id'=>'subcat-id'],
    'pluginOptions'=>[
        'depends'=>['catid'],
        'placeholder'=>'Select BatchNo',
        'url'=>Url::to(['/production/productbatch/subcat'])
    ]
    ]); ?>

    <?= $form->field($model, 'prodqty')->textInput() ?>

    <?= $form->field($productname, 'bottletype')->textInput() ?>

    <?= $form->field($bottlename, 'unitprice')->textInput() ?>




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

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

</div>
<?php
$script = <<< JS
$('#catid').change(function(){   
    var catid = $(this).val();

     $.get('index.php?r=production/productnames/get-for-production',{ catid : catid }, function(data){
        //alert(data);
        var data = $.parseJSON(data);
        $('#productnames-bottletype').attr('value',data.bottletype);
        $('#bottlename-unitprice').attr('value',data.bottletype0.unitprice);

    });
});
JS;
$this->registerJs($script);
?>

获取数据数组的操作

public function actionGetForProduction($catid)
{
    $bottle = Productnames::find()->with('bottletype0')->where(['productnames_productname'=>$catid])->asArray()->one();
    //$bottle -> select(['productnames.productnames_productname','productnames.bottletype','bottlename.unitprice'])->from('Productnames')->leftJoin('bottlename','productnames.bottletype = bottlename.bottlename')->where(['productnames_productname'=>$catid])->limit(1);
    echo Json::encode($bottle);

除最后一个单价外,此代码工作正常。请帮忙。

1 个答案:

答案 0 :(得分:1)

你可以在if条件下添加$bottlename->load(Yii::$app->request->post())。所以添加如,

if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post()) && $bottlename->load(Yii::$app->request->post())) {
 ....... 
}