通过 Yii2 表单插入数据时获取空值
Getting null value when inserting data through Yii2 form
我在生产表单中添加了一个文本输入字段。当我选择产品名称字段下拉列表时,单价会填满。但是当我保存数据时,我收到以下错误 -
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())) {
.......
}
相关文章:
- 当值更改时,在servlet中自动获取textarea值,无需单击按钮
- 通过 Yii2 表单插入数据时获取空值
- 当浮点部分为空时,从 JQuery 中的输入元素获取数字值不起作用
- 获取返回空值的元素 { 绝对|相对|固定 }
- 如何在 JQuery 中的文本框中获取空值
- 我从 ascx 文件中获取空值
- 在使用 AJAX 时获取数据库的空值
- AJAX 获取向控制器发送空值的调用
- 中继:获取递归数据返回空值
- 表单提交后获取空的隐藏值
- 使用Javascript从url中获取空哈希值
- 如何在不向输出数组获取空值的情况下拆分字符串
- get(一个空字符串)时尝试在 js 中获取 marginTop 值
- 如何防止“无法获取属性值”'dir':对象为空或未定义&;在IE9中加载页面时出现错误
- 无法获取属性值'appendChild':对象为空或未定义"同时追加脚本到IE
- 不能在请求中获取TextArea值,总是空的
- JQPlot饼图抛出“无法获取属性值”'0':对象为空或未定义&;错误
- Selenium WebDriver,从DOM中获取空值
- 使用val()获取空值
- 自动填充空值在ng模型中,从ng选项获取数据