【问题标题】:Batch Insert yii2 from checkbox从复选框批量插入 yii2
【发布时间】:2017-05-25 16:44:05
【问题描述】:

基本上我有两种形式的模型,我有一个来自复选框的数组值。 这是我的表格:

模型 1

$dataConditionContainer = ArrayHelper::map(ItemConditionIr::find()->all(), 'id', 'nama_condition');
echo $form->field($modelLinkItemConditionIrToIr, 'condition_id')->label("Container")->inline()->checkBoxList($dataConditionContainer);

模型2

<?= $form->field($modelInspectionReport, 'vehicle_no')->textInput(['maxlength' => true]) ?>

现在,由复选框解释的模型 1 给我一个这样的数组:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

我想使用批量插入来插入它们,但是这个数组需要一个来自 model2 主键的值,例如 $inspectionReport = $modelInspectionReport-&gt;id。

现在如何进行批量插入? 我知道这样的:

 $connection->createCommand()->batchInsert('link_item_condition_ir_to_ir',
       ['inspection_id', 'condition_id'], checkboxarraysuitable)->execute();

yii2 提供这个吗?

请指教。

【问题讨论】:

    标签: php checkbox yii2


    【解决方案1】:

    你可以试试这个:

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

    【讨论】:

    • 你能解释一下吗?
    • batchInsert($table, $columns, $rows ) $table: 你的表名 (ex: 'user') $columns: 要插入的列数组 $rows: 多维数组要批量插入的行数可能会有所帮助:link
    【解决方案2】:

    您的$checkboxarraysuitable 数组结构不正确。您的 array 必须在 array of arrays 结构中。 Here is an example of using BatchInsert() 具有正确的 array 结构。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 2017-01-28
      • 2015-02-05
      • 1970-01-01
      • 2013-04-11
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多