0

I've added a textinput field in my production form. The unitprice fills up when I select productname field drop down. But when I'm saving the data, I'm getting following error -

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)

The last "NULL" is the value for unitprice. actionCreate in productionController-

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

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

The action to get the data array

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

This code works fine except the last unitprice. Please help.

4
  • 1
    Try if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post()) && $bottlename->load(Yii::$app->request->post())) { ....... } Commented Apr 22, 2016 at 12:04
  • Working fine now. Thanks a lot. I thought of it once but was not sure. Thanks once again. Commented Apr 22, 2016 at 12:07
  • I put it as ans. so feel free to accept it. also vote up Commented Apr 22, 2016 at 12:07
  • Sure. You deserve it. Commented Apr 22, 2016 at 12:09

1 Answer 1

1

You fotgot to add $bottlename->load(Yii::$app->request->post()) in if condition. So add like as,

if ($model->load(Yii::$app->request->post()) && $productname->load(Yii::$app->request->post()) && $bottlename->load(Yii::$app->request->post())) {
 ....... 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.