我正在尝试在yii2中加入wbraganca动态表单。但它只在bill表中插入数据(相当于customer表),但不在productsales表中插入数据(相当于地址表)。我没有得到错误的地方。控制台中没有错误。点击“创建”按钮后,页面变为空白,并显示白色屏幕。我可以看到数据插入到账单表中但不在productales表中。 账单中的_form.php -
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use kartik\form\ActiveForm;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use kartik\select2\Select2;
use yii\helpers\Json;
use yii\web\View;
use frontend\modules\invoice\models\Parties;
use frontend\modules\invoice\models\Productbatch;
use frontend\modules\invoice\models\Year;
use frontend\modules\invoice\models\Console;
use wbraganca\dynamicform\DynamicFormWidget;
use kartik\depdrop\DepDrop;
/* @var $this yii\web\View */
/* @var $model frontend\modules\invoice\models\Bills */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="bills-form">
<?php $form = ActiveForm::begin([
'id' => 'dynamic-form',
'type' => ActiveForm::TYPE_HORIZONTAL,
'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL]
]); ?>
<div class="row">
<div class="form-group">
<div class="col-xs-9 col-sm-9 col-lg-9">
<?= $form->field($model, 'bills_partyname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Parties::find()->orderBy(['parties_partyname' => SORT_ASC,])->all(),'parties_partyname','partyDetails'),
'language' => 'en',
'options' => ['placeholder' => 'Select Party Name', 'id' => 'partyid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'transport')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'bills_year')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Year::find()->orderBy(['yid' => SORT_DESC,])->all(),'year_year','year_year'),
'language' => 'en',
'options' => ['placeholder' => 'Select Year', 'id' => 'yearid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'console')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Console::find()->orderBy(['consoleid' => SORT_ASC,])->all(),'console','console'),
'language' => 'en',
'options' => ['placeholder' => 'Select Console','id' => 'consoleid'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'billno')->textInput(['maxlength' => true,'readOnly'=>true]) ?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($model, 'billdate')->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,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
</div>
<?= $form->field($model, 'bills_ebillid')->textInput()->hiddenInput()->label(false) ?>
<?= $form->field($model, 'num')->textInput()->hiddenInput()->label(false) ?>
</div>
<div class="row">
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Products</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 20, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsProductsales[0],
'formId' => 'dynamic-form',
'formFields' => [
//'itemid',
'productname',
//'batchno',
// 'expdate',
// 'mrp',
// 'rate',
// 'qty',
// 'free',
// 'total',
// 'discount',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsProductsales as $i => $modelsProductsales): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Products</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelsProductsales->isNewRecord) {
echo Html::activeHiddenInput($modelsProductsales, "[{$i}]id");
}
?>
<div class="row-fluid">
<div class="form-group">
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= $form->field($modelsProductsales, "[{$i}]productname")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Productname']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]batchno")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Batchno']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding" >
<?= $form->field($modelsProductsales, "[{$i}]expdate")->label(false)->textInput(['maxlength' => true,'placeholder' => 'ExpDate']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]mrp")->label(false)->textInput(['maxlength' => true,'placeholder' => 'MRP']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Rate']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Qty']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Free']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]discount")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Discount']) ?>
</div>
</div>
<?= $form->field($modelsProductsales, "[{$i}]itemid")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelsProductsales, "[{$i}]total")->textInput(['maxlength' => true]) ?>
</div><!-- .row -->
</div>
</div><!-- item panel default -->
<?php endforeach; ?>
</div><!-- container item -->
<?php DynamicFormWidget::end(); ?>
</div><!-- panel body -->
</div><!-- panel default -->
</div><!-- outer row -->
<?= $form->field($model, 'billamount')->textInput() ?>
<?= $form->field($model, 'overdue')->textInput() ?>
<?= $form->field($model, 'cst')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'wbst')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'caseno')->textInput() ?>
<?= $form->field($model, 'amount')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'discount')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'tot')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'surcharge')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'total')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'tax')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'mrpvalue')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'cstpercent')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'wbstpercent')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'surpercent')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'totpercent')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'transport')->textInput(['maxlength' => true]) ?>
<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
/* start getting the transportdata */
$script = <<< JS
$('#partyid').change(function(){
var partyid = $(this).val();
$.get('index.php?r=invoice/bills/get-for-party',{ partyid : partyid }, function(data){
//alert(data);
var data = $.parseJSON(data);
$('#bills-transport').attr('value',data.transport);
});
});
JS;
$this->registerJs($script);
/* end getting the transportdata */
?>
<?php
/* start getting the ebilid */
$script = <<<EOD
$(window).load(function(){
$.get('index.php?r=invoice/bills/get-for-ebillid',{ ebillid : 1 }, function(data){
//alert(data);
var data = $.parseJSON(data);
$('#bills-bills_ebillid').attr('value',data.ebillid);
}
);
});
EOD;
$this->registerJs($script);
/*end getting the ebillid */
?>
<?php
/* start getting the num */
$script = <<< JS
$(function(){
$('#yearid').change(function(){
getNum();
});
$('#consoleid').change(function(){
getNum();
});
var yearid = $(this).val();
var consoleid = $(this).val();
var getNum = function(){
var yearid = String($('#yearid').val());
var consoleid = String($('#consoleid').val());
$.get('index.php?r=invoice/bills/get-for-num',{ yearid : yearid, consoleid : consoleid }, function(data){
//alert(data);
var data = $.parseJSON(data);
var getNum = data;
$('#bills-num').val(getNum["num"]);
});
} ;
});
JS;
$this->registerJs($script);
/* end getting the num */
?>
<?php
/* start getting the billno */
$script = <<< JS
$(function(){
$('#yearid').change(function(){
getBillno();
});
$('#consoleid').change(function(){
getBillno();
});
var yearid = $(this).val();
var consoleid = $(this).val();
var getBillno = function(){
var yearid = String($('#yearid').val());
var consoleid = String($('#consoleid').val());
$.get('index.php?r=invoice/bills/get-for-billno',{ yearid : yearid, consoleid : consoleid }, function(data){
//alert(data);
var data = $.parseJSON(data);
var getBillno = data;
$('#bills-billno').val(getBillno["billno"]);
});
} ;
});
JS;
$this->registerJs($script);
/* end getting the billno */
?>
BillsController
<?php
namespace frontend\modules\invoice\controllers;
use Yii;
use frontend\modules\invoice\models\Bills;
use frontend\modules\invoice\models\BillsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\modules\invoice\models\Parties;
use frontend\modules\invoice\models\Productsales;
use frontend\modules\invoice\models\Productbatch;
use frontend\modules\invoice\models\Model;
use yii\helpers\Json;
/**
* BillsController implements the CRUD actions for Bills model.
*/
class BillsController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Bills models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new BillsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Bills model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Bills model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Bills();
$modelsProductsales = [new Productsales];
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$modelsProductsales = Model::createMultiple(Productsales::classname());
Model::loadMultiple($modelsProductsales, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsProductsales) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsProductsales as $modelProductsales) {
$modelProductsales->productsales_ebillid = $model->bills_ebillid;
$modelProductsales->year = $model->bills_year;
$modelProductsales->console = $model->console;
$modelProductsales->billno = $model->billno;
$modelProductsales->billdate = $model->billdate;
$modelProductsales->productsales_partyname = $model->bills_partyname;
if (! ($flag = $modelProductsales->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
//return $this->redirect(['view', 'id' => $model->billid]);
else {
return $this->render('create', [
'model' => $model,
'modelsProductsales' => (empty($modelsProductsales)) ? [new Productsales] : $modelsProductsales
]);
}
}
/**
* Updates an existing Bills model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->billid]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Bills model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionGetForParty($partyid)
{
$party = Parties::find()->where(['parties_partyname'=>$partyid])->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($party);
}
public function actionGetForEbillid($ebillid)
{
$ebillidvar = Bills::find()->select('(max(bills_ebillid) + 1) as ebillid')->asArray()->one();
echo Json::encode($ebillidvar);
}
public function actionGetForNum($yearid , $consoleid)
{
$num = Bills::find()->select('(max(num) + 1) as num')->where(['bills_year'=>$yearid])->andWhere(['console'=>$consoleid])->asArray()->one();
echo Json::encode($num);
}
public function actionGetForItemid($productid , $batchid)
{
$item = Productbatch::find()->select('max(itemid) as itemid')->where(['productname'=>$productid])->andWhere(['batchno'=>$batchid])->asArray()->one();
echo Json::encode($item);
}
public function actionGetForBillno($yearid , $consoleid)
{
if($consoleid == 'GM1' || $consoleid == 'GM2'){
$num = Bills::find()->select(['concat("GM/",(max(num) + 1)) as billno'])->where(['bills_year'=>$yearid])->andWhere(['console'=>$consoleid])->asArray()->one();
echo Json::encode($num);
}elseif($consoleid == 'SM1' || $consoleid == 'SM2'){
$num = Bills::find()->select(['concat("SM/",(max(num) + 1)) as billno'])->where(['bills_year'=>$yearid])->andWhere(['console'=>$consoleid])->asArray()->one();
echo Json::encode($num);
}elseif($consoleid == 'CN'){
$num = Bills::find()->select(['concat("CM/",(max(num) + 1)) as billno'])->where(['bills_year'=>$yearid])->andWhere(['console'=>$consoleid])->asArray()->one();
echo Json::encode($num);
}
}
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = Productbatch::getBatchNo($cat_id);
echo Json::encode($out);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
//echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
/**
* Finds the Bills model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Bills the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Bills::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Create.php
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model frontend\modules\invoice\models\Bills */
$this->title = 'Create Bills';
$this->params['breadcrumbs'][] = ['label' => 'Bills', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="bills-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
'modelsProductsales' => $modelsProductsales,
]) ?>
</div>
Model.php
<?php
namespace frontend\modules\invoice\models;
use Yii;
use yii\helpers\ArrayHelper;
class Model extends \yii\base\Model
{
/**
* Creates and populates a set of models.
*
* @param string $modelClass
* @param array $multipleModels
* @return array
*/
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels)) {
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post)) {
foreach ($post as $i => $item) {
if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
$models[] = $multipleModels[$item['id']];
} else {
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
}
}