我尝试在我的项目中使用wbraganca / yii2-dynamicform一切都做得很好,但是当我点击“创建”按钮来验证actionCreate时没有发生任何事情。
项目结构:
供应商(sup_id,sup_name .....)
购买(purchase_id,sup_id,purchase_date)sup_id是外键
purchases_items(purchase_item_id,purchase_id,item_id,qte)purchase_id和item_id是外键
项目(ITEM_ID,......)
我想要做的是当用户创建购买时,他将能够同时创建purchase_items,然后通过使用purchases_items的相应item_id的qte值递增来更新表项中的列库存。
注意:列库存是物料的库存水平。 该项目涉及库存管理。
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\datepicker\DatePicker;
use wbraganca\dynamicform\DynamicFormWidget;
//use yii\jui\DatePicker;
use yii\helpers\ArrayHelper;
use backend\models\suppliers;
use backend\models\Items;
/* @var $this yii\web\View */
/* @var $model backend\models\Purchases */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="purchases-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<?= $form->field($modelPurchases, 'purchase_date')->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' => 'dd-M-yyyy'
]
]);?>
<?= $form->field($modelPurchases, 'sup_id')->dropDownList(
ArrayHelper::map(suppliers::find()->all(),'sup_id','sup_name'),['prompt'=>'Select Fournisseur']
) ?>
<?= $form->field($modelPurchases, 'purchase_status')->textInput() ?>
<!--purchase item information-->
<div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> Contenu Achat</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' => 6, // 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' => $modelsPurchaseItems[0],
'formId' => 'dynamic-form',
'formFields' => [
//'purchase_id',
'item_id',
'qte',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsPurchaseItems as $i => $modelPurchaseItem): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Contenu Achat</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 (! $modelPurchaseItem->isNewRecord) {
echo Html::activeHiddenInput($modelPurchaseItem, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelPurchaseItem, "[{$i}]item_id")->dropDownList(
ArrayHelper::map(items::find()->all(),'item_id','item_name'),['prompt'=>'Select Articles']
) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelPurchaseItem, "[{$i}]qte")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($modelPurchaseItem->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
PurchasesController.php
<?php
namespace backend\controllers;
use Yii;
use backend\models\Model;
use backend\models\Purchases;
use backend\models\PurchasesItems;
//use backend\models\Items;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\widgets\ActiveForm;
use yii\web\Response;
use yii\helpers\ArrayHelper;
/**
* PurchasesController implements the CRUD actions for Purchases model.
*/
class PurchasesController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Purchases models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Purchases::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Purchases model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Purchases model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
/*public function actionCreate()
{
$model = new Purchases();
$modelPurchaseItem = new PurchasesItems();
if ($model->load(Yii::$app->request->post()) && $modelPurchaseItem->load(Yii::$app->request->post())) {
$model->save();
$modelPurchaseItem -> purchase_id = $model->purchase_id;
$modelPurchaseItem -> save();
return $this->redirect(['view', 'id' => $model->purchase_id]);
}
return $this->render('create', [
'model' => $model,
'purchaseItem' => $modelPurchaseItem,
]);
}*/
public function actionCreate()
{
$modelPurchases = new Purchases();
$modelsPurchaseItems = [new PurchasesItems];
if ($modelPurchases->load(Yii::$app->request->post())) {
$modelsPurchaseItems = Model::createMultiple(PurchasesItems::classname());
Model::loadMultiple($modelsPurchaseItems, Yii::$app->request->post());
// validate all models
$valid = $modelPurchases->validate();
$valid = Model::validateMultiple($modelsPurchaseItems) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $modelPurchases->save(false)) {
foreach ($modelsPurchaseItems as $modelPurchaseItem) {
$modelPurchaseItem->purchase_id = $modelPurchases->id;
if (! ($flag = $modelPurchaseItem->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $modelPurchases->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('create', [
'modelPurchases' => $modelPurchases,
'modelsPurchaseItems' => (empty($modelsPurchaseItems)) ? [new PurchasesItems] : $modelsPurchaseItems
]);
}
/**
* Updates an existing Purchases model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->purchase_id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Purchases model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Purchases model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Purchases the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Purchases::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
}
create.php
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\models\Purchases */
$this->title = Yii::t('app', 'Creer Achat');
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Achat'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="purchases-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'modelPurchases' => $modelPurchases,
'modelsPurchaseItems' => $modelsPurchaseItems,
]) ?>
</div>
Model.php
<?php
namespace backend\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;
}
}