0

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.

Miguel Angel
  • 55
  • 10
  • move this `return $this->redirect(['view', 'id' => $model->id_group]);` after foreach. When you save first item you redirect user.. so foreach is interupted – vvpanchev Oct 22 '20 at 11:10
  • I have the error "Attempt to assign property 'assign_group_id' of non-object" – Miguel Angel Oct 22 '20 at 17:02
  • ‘$as’ is not an object, it’s selected ‘user_id’. Maybe You have to create object of your class and assign to it user_id and assign_group_id. I don’t know your db structure and logic – vvpanchev Oct 22 '20 at 19:25
  • I work with 2 models mainly, first I save in groups and then in group assignment. I have updated the post with the table relation. – Miguel Angel Oct 23 '20 at 06:11

1 Answers1

1
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,
    ]);
}
vvpanchev
  • 547
  • 1
  • 6
  • 14
  • Now if the multiple saved is working, Thank's. I have a problem in the modification part, I have created the function `$this->findModelAssignment($id);` but it does not bring me the data, any advice on how to implement it in this part? – Miguel Angel Oct 23 '20 at 07:12
  • Don't see this function – vvpanchev Oct 23 '20 at 07:40
  • I have added in controller `protected function findModelAsignacion($id) { if (($model = AsignarExamenAlumno::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException('The requested page does not exist.'); }` – Miguel Angel Oct 23 '20 at 08:05
  • where do you call this function? Check `$id` value. – vvpanchev Oct 23 '20 at 08:18
  • in the controller's modify function – Miguel Angel Oct 23 '20 at 17:40