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

Getting null value when inserting data through Yii2 form

本文关键字:获取 空值 数据 插入 Yii2 表单 通过      更新时间:2023-09-26

我在生产表单中添加了一个文本输入字段。当我选择产品名称字段下拉列表时,单价会填满。但是当我保存数据时,我收到以下错误 -

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"是单价的值。操作在生产中创建控制器-

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

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

你必须在

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())) {
 ....... 
}