Yii2动态表单,基于变量值的记录数

时间:2017-07-02 16:27:51

标签: yii2 dynamicform

我无法保存所有记录,只能记录最后一条记录。

在表单中,用户可以插入Year_beginYear_end

将变量$years(其中$years = Year_end - Year_begin)传递给动态表单。

示例:

  • 2010年和2011年工作我需要生成两个记录
  • 从2010年到2015年,我需要生成六个记录

我不想使用" +"和" - "按钮生成新记录。

每条记录都显示正确的

结果如下: Form image with two self-generated record

这是我的表单代码,周期为:

<?php foreach ($modelsComm as $i => $modelComm): ?>
<div class="item panel panel-default"><!-- widgetBody -->

    <div class="panel-heading">
        <h3 class="panel-title pull-left">id_calcolo</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">
    <!-- loop begin -->
        <?php
        $n=0;
        while ($n < $years) {
            // necessary for update action.
            if (! $modelComm->isNewRecord) {
                echo Html::activeHiddenInput($modelComm, "[{$i}]id");
            }
        ?>
        <div class="row">
            <div class="col-sm-2">
                <?= $form->field($modelComm, "[{$i}]year")->textInput(['value' => $year_begin+$n]) ?>
            </div>
            <div class="col-sm-4">
                <?= $form->field($modelComm, "[{$i}]worked")->textInput(['maxlength' => true]) ?>
            </div>
        <div class="col-sm-4">
                <?= $form->field($modelComm, "[{$i}]paid")->textInput(['maxlength' => true]) ?>
            </div>
        </div><!-- .row -->
        <?php
        $n++;
        }
        ?>   
        <!-- loop end -->  
    </div>

</div>
<?php endforeach; ?>

我的控制器功能:

public function actionCreate($years,$year_begin)
{
    $model = new CalcReference();
    $modelsComm = [new Comm];

    if ($model->load(Yii::$app->request->post()) && $model->save()) 
    {
        $modelsComm = Model::createMultiple(Comm::classname());
        Model::loadMultiple($modelsComm, Yii::$app->request->post());

        // ajax validation
        if (Yii::$app->request->isAjax) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ArrayHelper::merge(
                ActiveForm::validateMultiple($modelsComm),
                ActiveForm::validate($model)
            );
        }

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

        if ($valid) {
            $transaction = \Yii::$app->db->beginTransaction();
            try {
                if ($flag = $model->save(false)) {
                    foreach ($modelsComm as $modelComm) {
                        $modelComm->id_pratica = $model->id;
                        if (! ($flag = $modelComm->save(false))) {
                            $transaction->rollBack();
                            break;
                        }
                    }
                }
                if ($flag) {
                    $transaction->commit();
                    return $this->redirect(['view',
                    'id' => $model->id,
                    'year_begin' => $yeear_begin,
                    'years' => $years,
                    ]);
                }
            } catch (Exception $e) {
                $transaction->rollBack();
            }
        }
      /*  return $this->redirect(['view', 'id' => $model->id,
            'anno_inizio' => $anno_inizio,
            'qta_anni' => $qta_anni,
            'dal' => $dal,
            'al' => $al,
            'id_pratica' => $id_pratica,
            ]);*/
    } else 
    {
        return $this->render('create', [
            'model' => $model,
            'modelsComm' => (empty($modelsComm)) ? [new Comm] : $modelsComm,
            'year_begin' => $yeear_begin,
            'years' => $years,
        ]);
    }
}

我的模特:

public static function tableName()
{
    return 'comm';
}


public function rules()
{
    return [
        [['year', 'worked', ], 'required'],
        [['worked', 'paid'], 'integer'],
    ];
}


public function attributeLabels()
{
    return [
        'id' => 'ID',
        'year' => 'Year',
        'worked' => 'Worked days',
        'paid' => 'Total Paid',
     ];
}

这是型号Model.php:

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)

在年份循环中您使用$n但输入名称中包含$i。因此,所有输入都使用[0]前缀进行初始化。这就是为什么在服务器端你只有最后一条记录。

只需将[{$i}]id更改为[{$n}]id,将[{$i}]year更改为[{$n}]year,依此类推