Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save multiple values to database from Select2 Widget Yii2

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

like image 809
user2211486 Avatar asked Aug 31 '16 12:08

user2211486


Video Answer


1 Answers

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,
            ]);
        }
    }
like image 161
jithin Avatar answered Sep 19 '22 16:09

jithin