How to create update action for multiple select2 in yii2

Hi, I’m new with yii2 and now just seriously with update action for multiple select2 in yii2 these below is my code for update action in controller. It doesn’t work, when I update then it created new record . Any recommendation would be appreciated!!

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

       if ($model->load(Yii::$app->request->post())) {
            $uid = $model->user_id;
            $array = $model->division_id;

               foreach ($array as $key => $var) {
                   $newModel  = new Userdivision();
                   $newModel->id = $model->id;
                   $newModel->user_id = $uid;
                   $newModel->division_id = $var;
                   $newModel->save(false);
                  }
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
}

Hi @laocyberspaces, welcome to the forum.

Could you post your view code?
We need to read it in order to understand what you are trying to achieve and what could be the problem.

Thanks for your kindly helping. this is my _form.php

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

    <?= $form->field($model, 'user_id')->widget(Select2::classname(), [
      'data' => ArrayHelper::map(User::find()->all(), 'id', 'username'),
      'language' => 'en',
    //  'options' => ['placeholder' => 'ເລືອກຜູ້ຮັບຜິດຊອບຂະແໜງການ'],
        'pluginOptions' => [
        'allowClear' => true
      ],
      ]);
    ?>


    <?= $form->field($model, 'division_id')->widget(Select2::classname(), [
      'model' => $model,
      'name' => 'division_id',
      'attribute' => 'division_id',
      'data' => ArrayHelper::map(Division::find()->orderBy('code_division')->all(), 'id_division', 'name_division'),
      'language' => 'en',
      'options' => ['multiple' => true, 'placeholder' => 'ເລືອກຂະແໜງການ'],
        'pluginOptions' => [
          'allowClear' => true
        ],
      ]);
    ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

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

Hi lao, you don’t need this piece of code. Replace it with this:

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

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['index']);
    }

    return $this->render('create', [
        'model' => $model,
    ]);
}

On the load method, Yii2 ActiveRecord is already loading the data from for you. You just have to use the save to insert it on DB.

But this won’t solve for update, on your update action you should first load the model from the database, like this:

$model = Userdivision::findOne(['id' => $user_id]);

Where $user_id will be the variable passed as a parameter on the request.
If you do this, when drawing the Select2, the data will be loaded from the model, which is what your are trying to achieve, right?

Suggest some reading on the following topics:

1 Like

Thank you Sir, for your response me. the main point is I want to save multiple select to DB by each row

Oh ok, so you should use what is called a “tabular input”. There’s a great topic here: https://www.yiiframework.com/doc/guide/2.0/en/input-tabular-input#updating-a-fixed-set-of-records

The update will “break” because of the content in your $model->division_id.

I would change your form, and place these select in a User form, having the User model as the “baseModel” then loading those user divisions with the loadMultiple.

Oh, I got it Sir, thank you so much for your kindly technical support and helping me, it’s really useful and valuable lessons I got from you. I hope to have your advice again next time.
Best regards,

1 Like