yii2-动态形式。我无法更新

yii2-dynamicform. I can't update

我想使用动态表单小部件 (wbraganca)。 我使用 'doingItEasy' 频道和 github 的教程进行了尝试。 我可以创建和删除,但我不能更新。:

控制器:

public function actionUpdate($id) {
    $model = $this->findModel($id);
    $modelsDentalist = $model->dentalists;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        $oldIDs = ArrayHelper::map($modelsDentalist, 'id', 'id');
        $modelsDentalist = Model::createMultiple(Dentalist::classname(), $modelsDentalist);
        Model::loadMultiple($modelsDentalist, Yii::$app->request->post());
        $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsDentalist, 'id', 'id')));

        $valid = $model->validate();
        $valid = Model::validateMultiple($modelsDentalist) && $valid;

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    if (!empty($deletedIDs)) {
                        CalItem::deleteAll(['id' => $deletedIDs]);
                    }
                    foreach ($modelsDentalist as $modelDentalist) {
                        $modelDentalist->denta_id = $model->denta_id;
                        if (!($flag = $modelDentalist->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['index']);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }
    } else {
        return $this->render('update', [
                    'model' => $model,
                    'modelsDentalist' => (empty($modelsDentalist)) ? [new Dentalist] : $modelsDentalist
        ]);
    }
}

_From.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use yii\helpers\ArrayHelper;

use app\models\School;
use app\models\Schoolclass;
use app\models\Schoolterm;
use app\models\Year;
use app\models\Dentalist;


    <div class="denta-form">

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

        <div class="row">
            <div class="alert alert-success" role="alert">
                <div class="row">
                    <div class="col-md-2">
                        <?=
                        $form->field($model, 'school_id')->dropDownList(
                                ArrayHelper::map(School::find()->andWhere('school_type_id in(1,2) ')->all(), 'school_id', 'school_name')
                                , ['prompt' => 'เลือกรายการ...'])
                        ?>
                    </div>

                    <div class="col-md-2">
                        <?=
                        $form->field($model, 'school_term_id')->dropDownList(
                                ArrayHelper::map(Schoolterm::find()->all(), 'school_term_id', 'school_term_name')
                                , ['prompt' => 'เลือกรายการ...'])
                        ?>
                    </div>
                    <div class="col-md-2">
                        <?=
                        $form->field($model, 'year')->dropDownList(
                                ArrayHelper::map(Year::find()->all(), 'year', 'year')
                                , ['prompt' => 'เลือกรายการ...'])
                        ?>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="panel panel-default">
                    <?php //   <div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i> DentA list</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' => 4, // 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' => $modelsDentalist[0],
                            'formId' => 'dynamic-form',
                            'formFields' => [
                                'school_class_id',
                                'student_total',
                                'deciduous_caries',
                                'permanent_no',
                                'permanent_caries',
                                'gingivitis',
                                'disorder',
                            ],
                        ]);
                        ?>

                        <div class="container-items"><!-- widgetContainer -->
                            <?php foreach ($modelsDentalist as $i => $modelDentalist): ?>
                                <div class="item panel panel-default"><!-- widgetBody -->


                                    <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 class="panel-body">
                                        <?php
                                        //necessary for update action.
                                        if (!$modelDentalist->isNewRecord) {
                                            echo Html::activeHiddenInput($modelDentalist, "[{$i}]id");
                                        }
                                        ?>

                                        <div class="row">
                                            <div class="col-sm-2">
                                                <?=
                                                $form->field($modelDentalist, "[{$i}]school_class_id")->dropDownList(
                                                        ArrayHelper::map(Schoolclass::find()
                                                                        ->where("school_class_id IN(1,2,3)")
                                                                        ->all(), 'school_class_id', 'school_class_name')
                                                        , ['prompt' => 'เลือกรายการ...'])
                                                ?>
                                            </div>
                                            <div class="col-sm-2">
                                                <?= $form->field($modelDentalist, "[{$i}]student_total")->textInput(['maxlength' => true]) ?>
                                            </div>
                                            <div class="col-sm-1">
                                                <?= $form->field($modelDentalist, "[{$i}]deciduous_caries")->textInput(['maxlength' => true]) ?>
                                            </div>
                                            <div class="col-sm-2">
                                                <?= $form->field($modelDentalist, "[{$i}]permanent_no")->textInput(['maxlength' => true]) ?>
                                            </div>
                                            <div class="col-sm-1">
                                                <?= $form->field($modelDentalist, "[{$i}]permanent_caries")->textInput(['maxlength' => true]) ?>
                                            </div>
                                            <div class="col-sm-2">
                                                <?= $form->field($modelDentalist, "[{$i}]gingivitis")->textInput(['maxlength' => true]) ?>
                                            </div>
                                            <div class="col-sm-2">
                                                <?= $form->field($modelDentalist, "[{$i}]disorder")->textInput(['maxlength' => true]) ?>
                                            </div>

                                        </div><!-- .row -->
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <?php DynamicFormWidget::end(); ?>
                    </div>
                </div>
            </div>
            <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

    JS;
        $this->registerJs($script);
        ?> 
    </div>

Update.php

<?php

use yii\helpers\Html;
use app\models\DentaList;

/* @var $this yii\web\View */
/* @var $model app\models\Denta */

$this->title = Yii::t('app', 'Update {modelClass}: ', [
            'modelClass' => 'Denta',
        ]) . $model->denta_id;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Dentas'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->denta_id, 'url' => ['view', 'id' => $model->denta_id]];
$this->params['breadcrumbs'][] = Yii::t('app', 'Update');
?>
<div class="denta-update">

    <h1><?= Html::encode($this->title) ?></h1>

    <?=
    $this->render('_form', [
        'model' => $model,
        'modelsDentalist' => $modelsDentalist,
    ])
    ?>

</div>

我无法更新。 Error:Getting 未知 属性: app\models\Dentalist::id

我认为您可以在 Controller 中编辑以下行:

$oldIDs = ArrayHelper::map($modelsDentalist, 'denta_id', 'denta_id');
$modelsDentalist = Model::createMultiple(Dentalist::classname(), $modelsDentalist);
Model::loadMultiple($modelsDentalist, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsDentalist, 'denta_id', 'denta_id')));

并编辑 _form 中的行:

echo Html::activeHiddenInput($modelDentalist, "[{$i}]denta_id");