批量从复选框插入yii2

问题描述 投票:0回答:2

基本上我在一个表单中有两个模型,我有一个复选框的数组值。这是我的表格:

模型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提供这个吗?

请指教。

php checkbox yii2
2个回答
0
投票

你可以试试这个:

Yii :: $ app-> db-> createCommand() - > batchInsert(ItemConditionIr :: tableName(),['inspection_id','condition_id',],[[1,2],[3,4]]) - >执行();


0
投票

您的$checkboxarraysuitable阵列结构不正确。你的array必须是array of arrays结构。 Here is an example of using BatchInsert()具有适当的array结构。

© www.soinside.com 2019 - 2024. All rights reserved.