如何从 Select2 Widget Yii2 将多个值保存到数据库
Posted
技术标签:
【中文标题】如何从 Select2 Widget Yii2 将多个值保存到数据库【英文标题】:How to save multiple values to database from Select2 Widget Yii2 【发布时间】:2017-01-08 00:19:37 【问题描述】:我正在使用kartik - yii2-widget-select2 来允许在表单中创建时选择多个选项。
我想根据在 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,
]);
如何将两个或多个值保存到数据库?如何迭代或如何知道选择了多少个值以便使用循环插入?
有人可以帮助我或提供任何建议吗?谢谢
【问题讨论】:
【参考方案1】:添加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,
]);
【讨论】:
你好,Jithin。我完全按照你说的尝试了,但它不起作用。它仍然只向数据库取一个值。 ur Assign_task_to 字段是否为整数? 是的,它是整数。 更改为 varchar 或 text 类型并更新模型 但它是一个 ID。不过我会试试你说的以上是关于如何从 Select2 Widget Yii2 将多个值保存到数据库的主要内容,如果未能解决你的问题,请参考以下文章
yii2-widget、Select2 和 taggest 支持