How to save multiple values to database from Select2 Widget Yii2
Asked Answered
P

1

6

I am using the kartik - yii2-widget-select2 to allow the option to select multiple options while creating in form.

I want to enter two or more rows to the database depending on the the number of options selected in the select2 widget.

The select2 widget in the form is below.

<?= $form->field($model, 'Assign_task_to')->widget(Select2::classname(), 
//$data = ArrayHelper::map(Employee::find()->all(),'Employee_ID','employee_name'),
[
'data' => ArrayHelper::map(Employee::find()->all(),'Employee_ID','employee_name'),
'language' => 'en',
'options' => ['placeholder' => 'Select an employee ...', 'multiple' => true],
'pluginOptions' => [
    'allowClear' => true
],
]); ?>

The controller actionCreate is below.

public function actionCreate()
{

    // set default values
    $empModel = new Tasksemp();
    $model = new Tasks();
    $model->priotiy_level = 'medium';
    $model->start_date = date('Y-m-d'); //to get default date
    if ($model->load(Yii::$app->request->post())) {


        //Check if field input is empty

        $model->file = UploadedFile::getInstance($model, 'upload_documents');


        if($model->file)
        {
            //get instance of uploaded file
            $fName = time();
            $model->file = UploadedFile::getInstance($model, 'upload_documents');
            $model->file->saveAs('uploads/'.$fName.'.'.$model->file->extension);

            $model->upload_documents = 'uploads/'.$fName.'.'.$model->file->extension;
        }



        $model->save();

        //to save in task_emp table

            $empModel->Task_ID = $model->Task_ID;   
            $empModel->Employee_ID = $model->Assign_task_to;  //<---want to save multiple employee IDs with the same Task ID (multiple rows)
            $empModel->save(false);
            return $this->redirect(['view', 'id' => $model->Task_ID]);      

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

How can I save two or more values to database? How do I iterate or how do I know how many values are selected so that I can use a loop to insert?

Can someone please help me or give any suggestions? Thank you

Palpate answered 31/8, 2016 at 12:0 Comment(0)
O
4

add an foreach to save items to db

 public function actionCreate()
    {

        // set default values
        $empModel = new Tasksemp();
        $model = new Tasks();
        $model->priotiy_level = 'medium';
        $model->start_date = date('Y-m-d'); //to get default date
        if ($model->load(Yii::$app->request->post())) {


            //Check if field input is empty

            $model->file = UploadedFile::getInstance($model, 'upload_documents');


            if($model->file)
            {
                //get instance of uploaded file
                $fName = time();
                $model->file = UploadedFile::getInstance($model, 'upload_documents');
                $model->file->saveAs('uploads/'.$fName.'.'.$model->file->extension);

                $model->upload_documents = 'uploads/'.$fName.'.'.$model->file->extension;
            }


            $model->save();

            //to save in task_emp table

               $array=$model->Assign_task_to; 
               foreach ($array as $value) { 
                   $empModel = new Tasksemp(); 
                   $empModel->Task_ID = $model->Task_ID; 
                   $empModel->Employee_ID = $value; 
                   $empModel->save(false); 
                  }

                return $this->redirect(['view', 'id' => $model->Task_ID]);      

        } else {
            return $this->render('create', [
                'model' => $model,
                'emp' => $empModel,
            ]);
        }
    }
Oxus answered 31/8, 2016 at 12:14 Comment(9)
Hi Jithin. I tried exactly what you said but it isn't working. Its still taking only one value to the database.Palpate
ur Assign_task_to feild is integer or not?Oxus
Yes it is integer.Palpate
change to varchar or text type and update model alsoOxus
But its an ID. Ill try what you said thoughPalpate
you selected multiple id's?Oxus
yiiframework.com/forum/index.php/topic/…Oxus
Yes. I want to take multiple IDs and make two inserts or more depending on the number of values selected. So the $empModel has to be saved the number of times cause I want to save The Assign_task_to with the task_ID to a different table.Palpate
Let us continue this discussion in chat.Palpate

© 2022 - 2024 — McMap. All rights reserved.