【问题标题】:Insert multiple records of a same table Yii2插入同一张表Yii2的多条记录
【发布时间】:2017-02-21 21:30:26
【问题描述】:

我想在一张表中插入相同的记录。我在下面的表格中只有一个输入数组,但我想为label input array 保存多个时间记录。 我的表格是

<div class="surveys-questions-form">

    <?php $form = ActiveForm::begin(); ?>

    <?php

        if(isset($_GET['option_id']) and $_GET['option_id'] > 0)
            $id= $_GET['option_id'];
        else 
            $id= $model->option_id;
        echo $form->field($model, 'question_id')->hiddenInput(['value' => $id])->label(false);
    ?>

   <div class="col-md-6">
    <div id="question_wrapper">
        <?= $form->field($model, 'type')->dropDownList([ 'text' => 'Text', 'numbers' => 'Numbers', 'date' => 'Date', 'texarea' => 'Texarea', 'checkbox' => 'Checkbox', 'radio' => 'Radio', 'rating' => 'Rating', ], ['prompt' => '']) ?>
        <div id="add_more_field">
            <?= $form->field($model, 'label[]')->textInput(['maxlength' => true]) ?>
        </div>
        <div class="form-group">
            <?php
                echo Html::a('Add more', 'javascript:void(0);', [
                    'id' => 'surveys-questions-new-button', 
                    'class' => 'pull-right btn btn-primary btn-xs'
                ])
            ?>
        </div>
    </div>
   </div>
    <div class="col-md-12">
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
     </div>
    <?php ActiveForm::end(); ?>

</div>

和控制器

public function actionCreate()
{
    $model = new QuestionsOptions();

   if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->option_id]);
    } else {
        return $this->renderAjax('create', [
            'model' => $model,
        ]);
    }
} 

当我尝试提交表单时出现以下错误。

Label must be a string. 

我的 $_POST 数组

  Array
(
    [_csrf-backend] => LXBhajI3YVpOIikeRWYHYkNCAD4Kb1ZrQzwER21GL2MdCTgkWm5ZDQ==
    [QuestionsOptions] => Array
        (
            [question_id] => 47
            [type] => numbers
            [label] => Array
                (
                    [0] => Label1
                    [1] => Label2
                    [2] => Labe3
                )

        )

)

【问题讨论】:

  • 您能否分享一下 print_r 结果,以便我了解您发布的数据,然后我可以指导您
  • @Dani 非常感谢。我已经修改了我的问题,您可以在最后一个帖子数组中看到

标签: yii2


【解决方案1】:

如果你想保存多条记录使用循环,我建议你使用 foreach 循环最好

    public function actionCreate()
 {
      $model = new QuestionsOptions();

     if ($model->load(Yii::$app->request->post())) {
           if(sizeof(array_filter($_POST['QuestionsOptions']['label'])) > 0){
             foreach($_POST['QuestionsOptions']['label'] as $key => $row){
                  $model->setIsNewRecord(true);
                  $model->id = null;
                  $model->label = $row;
                  $model->save();
             } 
          }
            return $this->redirect(['view', 'id' => $model->option_id]);
        } else {
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }
    }

【讨论】:

  • @非常感谢。但是当我更新它们时,我将如何处理另一件事。
  • 工作得很好,谢谢。您可以在我的代码中看到我使用 jquery clone 方法创建标签。如果我单击 2 次它会添加更多按钮,那么将附加 2 个额外的标签。这意味着将在数据库中插入 3 行。现在我如何以更新形式显示这些记录
  • @coder .. 发布正确的新答案 .. 清楚地描述您的需求..
  • 我创建了一个正确的问题@scaisEdge。不介意你可以在这里查看stackoverflow.com/questions/42417814/…
  • 这是一种非常糟糕的做法。如果您有数百行,则需要一分钟以上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多