yii2动态表单actionUpdate问题

时间:2016-03-02 17:46:09

标签: php yii2 dynamic-forms

我在我的应用程序中使用wbraganca/yii2-dynamicform小部件。动作创建效果很好,但是当我尝试更新时,我得到this

这是我的控制者:

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $modelsResolucion = $model->resolucions;
    $modelsMedidas = $model->medidas;
    $modelsDeficiencias = $model->deficiencias;

    if ($model->load(Yii::$app->request->post()) && $model->save())
    {
        //--------resolucion 512--------------
        $oldIDsResolucion = ArrayHelper::map($modelsResolucion, 'id', 'id');
        $modelsResolucion = Model::createMultiple(Resolucion::classname(), $modelsResolucion);
        Model::loadMultiple($modelsResolucion, Yii::$app->request->post());
        $deletedIDsResolucion = array_diff($oldIDsResolucion, array_filter(ArrayHelper::map($modelsResolucion, 'id', 'id')));

        //--------medidas aplicadas--------------
        $oldIDsMedidas = ArrayHelper::map($modelsMedidas, 'id', 'id');
        $modelsMedidas = Model::createMultiple(Medidas::classname(), $modelsMedidas,$id);   
        Model::loadMultiple($modelsMedidas, Yii::$app->request->post());
        $deletedIDsMedidas = array_diff($oldIDsMedidas, array_filter(ArrayHelper::map($modelsMedidas, 'id', 'id')));

        //--------------------principales deficiencias------------------
        $oldIDsDeficiencias = ArrayHelper::map($modelsDeficiencias, 'id', 'id');
        $modelsDeficiencias = Model::createMultiple(Deficiencias::classname(), $modelsDeficiencias);
        Model::loadMultiple($modelsDeficiencias, Yii::$app->request->post());
        $deletedIDsDeficiencias = array_diff($oldIDsDeficiencias, array_filter(ArrayHelper::map($modelsDeficiencias, 'id', 'id')));

        // validate all models
        $valid = $model->validate();
        $valid = Model::validateMultiple($modelsResolucion) || Model::validateMultiple($modelsMedidas) || Model::validateMultiple($modelsDeficiencias) && $valid;

        //return $this->redirect(['view', 'id' => $model->codigo]);

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                     if (!empty($deletedIDsResolucion)) {
                            Resolucion::deleteAll(['id' => $deletedIDsResolucion]);
                        }
                        foreach ($modelsResolucion as $modelResolucion) {
                            $modelResolucion->id = $model->codigo;
                            if (! ($flag = $modelResolucion->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        if (!empty($deletedIDsMedidas)) {
                            Medidas::deleteAll(['id' => $deletedIDsMedidas]);
                        }
                        foreach ($modelsMedidas as $modelMedidas) {
                            $modelMedidas->id = $model->codigo;
                            if (! ($flag = $modelMedidas->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                        if (!empty($deletedIDsDeficiencias)) {
                            Deficiencias::deleteAll(['id' => $deletedIDsDeficiencias]);
                        }
                        foreach ($modelsDeficiencias as $modelDeficiencias) {
                            $modelDeficiencias->id = $model->codigo;
                            if (! ($flag = $modelDeficiencias->save(false))) {
                                $transaction->rollBack();
                                break;
                            }
                        }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view', 'id' => $model->codigo]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }
    } else {
        return $this->render('update', [
            'model' => $model,
            'modelsResolucion' => (empty($modelsResolucion)) ? [new Resolucion] : $modelsResolucion,
            'modelsMedidas' => (empty($modelsMedidas)) ? [new Medidas] : $modelsMedidas,
            'modelsDeficiencias' => (empty($modelsDeficiencias)) ? [new Deficiencias] : $modelsDeficiencias,
        ]);
    }
}

这是我的_form:

<div class="cumplimiento-form">

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

<?= $form->field($model, 'codigo')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'descripcion')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'extra_plan')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'organismo_id')->textInput() ?>

<?= $form->field($model, 'entidad_id')->textInput() ?>

<?= $form->field($model, 'tipo_accion_id')->textInput() ?>

<?= $form->field($model, 'particularidades_id')->textInput() ?>

<?= $form->field($model, 'fecha_inicio_p')->textInput() ?>

<?= $form->field($model, 'fecha_fin_p')->textInput() ?>

<?= $form->field($model, 'cant_auditores_p')->textInput() ?>

<?= $form->field($model, 'cant_dias_p')->textInput() ?>

<?= $form->field($model, 'orden_trabajo')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'directiva')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'fecha_inicio_r')->textInput() ?>

<?= $form->field($model, 'fecha_fin_r')->textInput() ?>

<?= $form->field($model, 'calificacion_id')->textInput() ?>

<?= $form->field($model, 'cant_auditores_r')->textInput() ?>

<?= $form->field($model, 'cant_dias_r')->textInput() ?>

<?= $form->field($model, 'cant_phc')->textInput() ?>

<?= $form->field($model, 'cant_phd')->textInput() ?>

<?= $form->field($model, 'conclusion_id')->textInput() ?>

<?= $form->field($model, 'pertenece_cgr')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'dic_id')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'registrado_por')->textInput(['maxlength' => true]) ?>

<!-- Perjuicios economicos-->

<?php DynamicFormWidget::begin([
    'widgetContainer' => 'dynamicform_wrapper_resolucion', // 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 added (default 999)
    'min' => 0, // 0 or 1 (default 1)
    'insertButton' => '.add-item', // css class
    'deleteButton' => '.remove-item', // css class
    'model' => $modelsResolucion[0],
    'formId' => 'dynamic-form',
    'formFields' => [
        'cargo_id',
        'medida_aplicada_id',
        'cantidad',
    ],
]); ?>

<div class="panel panel-default">
    <div class="panel-heading">
        <h4>
            <i class="glyphicon glyphicon-envelope"></i> Perjuicio Económico
            <button type="button" class="add-item btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Adicionar</button>
        </h4>
    </div>
    <div class="panel-body">
        <div class="container-items"><!-- widgetBody -->
        <?php foreach ($modelsResolucion as $i => $modelResolucion): ?>
            <div class="item panel panel-default"><!-- widgetItem -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left">Perjuicio Económico</h3>
                    <div class="pull-right">
                        <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 (! $modelResolucion->isNewRecord) {
                            echo Html::activeHiddenInput($modelResolucion, "[{$i}]id");
                        }
                    ?>
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelResolucion, "[{$i}]cat_dannos_id")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($modelResolucion, "[{$i}]valor")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($modelResolucion, "[{$i}]conceptos_dannos_id")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelResolucion, "[{$i}]dannos_id")->textInput(['maxlength' => true]) ?>
                        </div>                                                        
                        <div class="col-sm-6">
                            <?= $form->field($modelResolucion, "[{$i}]moneda_id")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>

<!-- Medidas Aplicadas-->

<?php DynamicFormWidget::begin([
    'widgetContainer' => 'dynamicform_wrapper_medidas', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
    'widgetBody' => '.container-items1', // required: css class selector
    'widgetItem' => '.item1', // required: css class
    'limit' => 999, // the maximum times, an element can be added (default 999)
    'min' => 0, // 0 or 1 (default 1)
    'insertButton' => '.add-item1', // css class
    'deleteButton' => '.remove-item1', // css class
    'model' => $modelsMedidas[0],
    'formId' => 'dynamic-form',
    'formFields' => [
        'cargo_id',
        'medida_aplicada_id',
        'cantidad',
    ],
]); ?>

<div class="panel panel-default">
    <div class="panel-heading">
        <h4>
            <i class="glyphicon glyphicon-envelope"></i> Medidas Aplicadas
            <button type="button" class="add-item1 btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Adicionar</button>
        </h4>
    </div>
    <div class="panel-body">
        <div class="container-items1"><!-- widgetBody -->
        <?php foreach ($modelsMedidas as $i => $modelMedidas): ?>
            <div class="item1 panel panel-default"><!-- widgetItem -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left">Medidas Aplicadas</h3>
                    <div class="pull-right">
                        <button type="button" class="remove-item1 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 (! $modelMedidas->isNewRecord) {
                            echo Html::activeHiddenInput($modelMedidas, "[{$i}]id");
                        }
                    ?>
                    <?= $form->field($modelMedidas, "[{$i}]cargo_id")->textInput(['maxlength' => true]) ?>
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelMedidas, "[{$i}]medida_aplicada_id")->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-sm-6">
                            <?= $form->field($modelMedidas, "[{$i}]cantidad")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>

<!-- Principales deficiencias-->

<?php DynamicFormWidget::begin([
    'widgetContainer' => 'dynamicform_wrapper_deficiencias', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
    'widgetBody' => '.container-items2', // required: css class selector
    'widgetItem' => '.item2', // required: css class
    'limit' => 999, // the maximum times, an element can be added (default 999)
    'min' => 0, // 0 or 1 (default 1)
    'insertButton' => '.add-item2', // css class
    'deleteButton' => '.remove-item2', // css class
    'model' => $modelsDeficiencias[0],
    'formId' => 'dynamic-form',
    'formFields' => [
        'categoria_id',
        'violacion_os',
    ],
]); ?>

<div class="panel panel-default">
    <div class="panel-heading">
        <h4>
            <i class="glyphicon glyphicon-envelope"></i> Principales deficiencias
            <button type="button" class="add-item2 btn btn-success btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i> Adicionar</button>
        </h4>
    </div>
    <div class="panel-body">
        <div class="container-items2"><!-- widgetBody -->
        <?php foreach ($modelsDeficiencias as $i => $modelDeficiencias): ?>
            <div class="item2 panel panel-default"><!-- widgetItem -->
                <div class="panel-heading">
                    <h3 class="panel-title pull-left">Principales deficiencias</h3>
                    <div class="pull-right">
                        <button type="button" class="remove-item2 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 (! $modelDeficiencias->isNewRecord) {
                            echo Html::activeHiddenInput($modelDeficiencias, "[{$i}]id");
                        }
                    ?>
                    <?= $form->field($modelDeficiencias, "[{$i}]categoria_id")->textInput(['maxlength' => true]) ?>
                    <div class="row">
                        <div class="col-sm-6">
                            <?= $form->field($modelDeficiencias, "[{$i}]violacion_os")->textInput(['maxlength' => true]) ?>
                        </div>
                    </div><!-- .row -->
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
</div><!-- .panel -->
<?php DynamicFormWidget::end(); ?>

<!-- Presuntos Hecho Delictivos(PHD)-->



<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Insertar') : Yii::t('app', 'Actualizar'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

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

知道为什么会这样吗?感谢

0 个答案:

没有答案