如何在bachInsert中将数据与字段进行比较?

时间:2018-03-19 07:32:58

标签: yii2

public static  $columnsInsert =
    [
        'id',
        'customer_id',
        'summary',
        'description',
        'color',
        'favourite',
        'update_at',
        'create_at',
        'personEmail'
    ];


$sql = $db->queryBuilder->batchInsert(self::$table, self::$columnsInsert, $data);

从客户端,字符串中有相同键的行数组。但是为什么数据与查询中的字段不匹配。是否真的有必要在客户端的数据下选择订单。

1 个答案:

答案 0 :(得分:0)

如果您按顺序列出插入列

  public static  $columnsInsert =
      [
          'id',
          'customer_id',
          'summary',
          'description',
          'color',
          'favourite',
          'update_at',
          'create_at',
          'personEmail'
      ];

然后您的$数据应该具有相同的序列..

    $data =
      [
         [
          value_for_id,
          value_for_idfor_customer_id,
          value_for_summary  ,
          value_for_description,
          value_for_color,
          value_for_favourite,
          value_for_update_at,
          value_for_create_at,
          balue_personEmail
        ], 
        [
          value_for_id,
          value_for_idfor_customer_id,
          value_for_summary  ,
          value_for_description,
          value_for_color,
          value_for_favourite,
          value_for_update_at,
          value_for_create_at,
          balue_personEmail
        ], 
      ];

  so you should adapt your columns sequence or your data sequence for a correct match 

实际上$ columnInsert是insert

的列声明部分中使用的部分
INSERT  INTO your_table ('id',  'customer_id','summary','description',
    'color','favourite','update_at','create_at','personEmail')

,数据用于值

VALUES (value_for_id, value_for_id, for_customer_id,  value_for_summary, 
  value_for_description, value_for_color,  value_for_favourite, 
  value_for_update_at,  value_for_create_at,  balue_personEmail), 
      (value_for_id, value_for_id, for_customer_id,  value_for_summary, 
  value_for_description, value_for_color,  value_for_favourite,
  value_for_update_at,  value_for_create_at,  balue_personEmail), 
       (value_for_id, value_for_id, for_customer_id,  value_for_summary  ,
  value_for_description, value_for_color,  value_for_favourite, 
  value_for_update_at,  value_for_create_at,  balue_personEmail),
  ........ 

正如您所看到的那样,两部分具有相同的序列