基本上我在一个表单中有两个模型,我有一个复选框的数组值。这是我的表格:
模型1
$dataConditionContainer = ArrayHelper::map(ItemConditionIr::find()->all(), 'id', 'nama_condition');
echo $form->field($modelLinkItemConditionIrToIr, 'condition_id')->label("Container")->inline()->checkBoxList($dataConditionContainer);
MODEL2
<?= $form->field($modelInspectionReport, 'vehicle_no')->textInput(['maxlength' => true]) ?>
现在,从复选框中插入的模型1给我一个这样的数组:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
我想使用批量插入插入它们,但是这个数组需要来自model2主键的值,如$inspectionReport = $modelInspectionReport->id
。
现在如何进行批量插入?我知道这样的:
$connection->createCommand()->batchInsert('link_item_condition_ir_to_ir',
['inspection_id', 'condition_id'], checkboxarraysuitable)->execute();
是的yii2提供这个吗?
请指教。
你可以试试这个:
Yii :: $ app-> db-> createCommand() - > batchInsert(ItemConditionIr :: tableName(),['inspection_id','condition_id',],[[1,2],[3,4]]) - >执行();
您的$checkboxarraysuitable
阵列结构不正确。你的array
必须是array of arrays
结构。 Here is an example of using BatchInsert()具有适当的array
结构。