YII2创建函数不会将数据发送到数据库

时间:2017-09-07 08:03:26

标签: php yii yii2 yii2-basic-app dynamicform

在我的上一个问题之后,我一直在yii2上发票并遇到了一个新问题。在您填写表单并单击创建后,除了刷新页面和清除产品字段之外,它并没有做任何事情,但是公司名称和日期等个人信息在刷新后仍然填写...也在我检查数据库什么也没发生。你能看一下下面的代码吗?

发票表格

    <?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use kartik\datecontrol\Module;
use kartik\datecontrol\DateControl;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use app\models\Companies;
use app\models\Spokepersons;



/* @var $this yii\web\View */
/* @var $model app\models\Facturen */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="facturen-form">

    <?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>

    <?= $form->field($model, 'company_name')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Companies::find()->all(), 'company_id', 'company_name'),
    'language' => 'nl',
    'options' => ['placeholder' => 'Selecteer een bedrijf'],
    'pluginOptions' => [
        'allowClear' => true
    ],
])?>

    <?= $form->field($model, 'spokes_person')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Spokepersons::find()->all(), 'person_id', 'first_name'),
    'language' => 'nl',
    'options' => ['placeholder' => 'Selecteer een contactpersoon'],
    'pluginOptions' => [
        'allowClear' => true
    ],
])?>

    <?= $form->field($model, 'date')->widget(DateControl::classname(), [
    'type' => 'date',
    'ajaxConversion' => true,
    'autoWidget' => true,
    'widgetClass' => '',
    'displayFormat' => 'php:d-F-Y',
    'saveFormat' => 'php:Y-m-d',
    'saveTimezone' => 'UTC',
    'displayTimezone' => 'Europe/Amsterdam',
    'saveOptions' => [
        //'label' => 'Input saved as: ',
       // 'type' => 'text',
        'readonly' => true,
        'class' => 'hint-input text-muted'
    ],
    'widgetOptions' => [
        'pluginOptions' => [
            'autoclose' => true,
            'format' => 'php:d-F-Y'
        ]
    ],
    'language' => 'nl'
]) ?>

    <div class="row"> 
        <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' => 999, // 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' => $modelProducten[0],
                'formId' => 'dynamic-form',
                'formFields' => [
                    'product_id',
                    'product_name',
                    'amount',
                    'price',
                ],
            ]); ?>

            <div class="container-items"><!-- widgetContainer -->
            <?php foreach ($modelProducten as $i => $modelProduct): ?>
                <div class="item panel panel-default"><!-- widgetBody -->
                    <div class="panel-heading">
                        <label class="panel-title pull-left">Product</label>
                        <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 (! $modelProduct->isNewRecord) {
                                echo Html::activeHiddenInput($modelProduct, "[{$i}]id");
                            }
                        ?>
                        <div class="row">
                            <div class="col-sm-3">
                        <?= $form->field($modelProduct, "[{$i}]product_id")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-3">
                                <?= $form->field($modelProduct, "[{$i}]product_name")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-3">
                                <?= $form->field($modelProduct, "[{$i}]amount")->textInput(['maxlength' => true]) ?>
                            </div>
                            <div class="col-sm-3">
                                <?= $form->field($modelProduct, "[{$i}]price")->textInput(['maxlength' => true]) ?>
                            </div>
                        </div><!-- .row -->
                    </div>
                </div>
            <?php endforeach; ?>
            </div>
            <?php DynamicFormWidget::end(); ?>
        </div>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
</div>

我认为这不是问题所在,因为表单有效但仍然可以使用。

创建功能

public function actionCreate()
    {
        $model = new Facturen();
        $modelProducten = [new Producten];
        if ($model->load(Yii::$app->request->post()) && $model->save()) 
        {
            $modelProducten = Model::createMultiple(Producten::classname());
            Model::loadMultiple($modelProducten, Yii::$app->request->post());

            // validate all models
            $valid = $model->validate();
            $valid = Model::validateMultiple($modelProducten) && $valid;

            if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        foreach ($modelProducten as $modelProduct) 
                        {
                            $modelProduct->factuur_id = $model->id;
                            if (! ($flag = $modelProduct->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->id]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
        } 
        else {
            return $this->render('create', [
                'model' => $model,
                'modelProducten' => (empty($modelProducten)) ? [new Producten] : $modelProducten
            ]);
        }
    }

最后是Model.php

<?php

namespace app\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;
    }
}

我希望你们能在我的代码中发现问题,并告诉我当你找到一个探测器时我做错了什么。我试图学习这个框架而不是真的想放弃

1 个答案:

答案 0 :(得分:0)

您要为同一型号召唤$model->save()两次。 第二个例子: if ($flag = $model->save(false))将返回0,因为第二次更新不会影响表格中的任何行 使用if ( $model->save(false)!==false)检查更新是否成功。