如何从 Select2 Widget Yii2 将多个值保存到数据库

How to save multiple values to database from Select2 Widget Yii2

我正在使用 kartik - yii2-widget-select2 以允许在创建表单时选择 select 多个选项。

我想根据 select 在 select2 小部件中编辑的选项数量向数据库输入两行或更多行。

表单中的 select2 小部件如下。

<?= $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
],
]); ?>

下面是控制器actionCreate。

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,
        ]);
    }
}

如何将两个或多个值保存到数据库?我如何迭代或如何知道有多少值被 selected 以便我可以使用循环插入?

有人可以帮助我或提出任何建议吗?谢谢

添加一个 foreach 以将项目保存到数据库

 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,
            ]);
        }
    }