Search code examples
yii2yii2-advanced-appyii2-validation

Save multiple selections from a listbox - Yii2


I have made a Listbox depend on a dropDownList, when selecting an option from the dropDownList brings me a list of data that is added to the Listbox, it works to save a single option but the problem occurs when trying to save multiple selections, I cannot save more than 1 option, I have tried to add a foreach in my controller but it throws an error.

DropDownList

 <?php echo $form->field($model, 'group_id')->widget(Select2::classname(), [
    'data' => $seccion->lgrupo, //I get the group list
    'size' => Select2::MEDIUM,
    'theme' => Select2::THEME_BOOTSTRAP,

    'options' => [
        'placeholder' => '-- '.Yii::t('backend', 'Select').' --',
        'onchange'=>'
            $.post( "lists?id="+$(this).val(), function( data ) {//I get the list of people registered in the group and send it to the listbox
            $( "select#assignment-user_id" ).html( data );
        });',
        ],
    'pluginOptions' => [
        'allowClear' => true,
    ],
    'addon' => [
        'prepend' => [
            'content' => Html::icon('building')
        ],
    ]
]); ?>

ListBox

<?php echo $form->field($model2, 'users_id')->listBox([] ,['multiple'=>true,'size'=>17]
                    
 ); ?>

Groups Controller

public function actionCreate()
{
    $model = new Groups();
    $model2 = new Assignment();
    $seccion = new Group();

    if ($model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post())) {

        if ($model->save(false)) {
            foreach ($model2->users_id as $i => $as) {
                $as->assign_group_id = $model->id_group_list;
                if ($model2->save()) {
                    
                } else {
                    // error in saving model
                }
            }
            return $this->redirect(['view', 'id' => $model->id_group]);
        }
    }
    return $this->render('create', [
        'model' => $model,
        'model2' => $model2,
        'seccion' => $seccion,
    ]);
}

Tables

table

I hope your can tell me what I'm doing wrong.


Solution

  • public function actionCreate()
    {
        $model = new Groups();
        $model2 = new Assignment();
        $seccion = new Group();
    
        if ($model->load(Yii::$app->request->post()) && $model2->load(Yii::$app->request->post())) {
    
            if ($model->save(false)) {
                foreach ($model2->users_id as $user_id) {
                    $assignmentModel = new Assignment();
                    $assignmentModel->user_id= $user_id;
                    $assignmentModel->assign_group_id = $model->id_group_list;
                    //$assignmentModel->area= '';  //if you want to set some value to these fields
                    //$assignmentModel->assignment= '';
                    if ($assignmentModel->save()) {
                        
                    } else {
                        // error in saving model
                    }
                }
                return $this->redirect(['view', 'id' => $model->id_group]);
            }
        }
        return $this->render('create', [
            'model' => $model,
            'model2' => $model2,
            'seccion' => $seccion,
        ]);
    }