首页 > 解决方案 > update_batch 中的多个 where / 键

问题描述

有人可以帮我吗,我想update_batch用双键(no_trans 和 stock_id)更新这个数量?

 $data = array(
       array(
          'no_trans' => '001' ,
          'stock_id' => 'aaa' ,
          'qty' => '100'
       ),
       array(
          'no_trans' => '001' ,
          'stock_id' => 'aab' ,
          'qty' => '200'
       ),
       array(
          'no_trans' => '002' ,
          'stock_id' => 'aaa' ,
          'qty' => '300'
       ),
       array(
          'no_trans' => '002' ,
          'stock_id' => 'aac' ,
          'qty' => '400'
       )
    );

我尝试

$this->db->update_batch('table', $data, array('no_trans','stock_id'));

但不是工作

标签: phpcodeigniter

解决方案


update_batch命令与单个 where 子句一起使用。(刚测试过)

要归档您的目标,您必须foreach使用update/replace命令

foreach ($data  as $key => $item) {
    $update = array(
        'qty' => $item['qty']
        );

    $this->db->where('no_trans', $item['no_trans']);
    $this->db->where('stock_id', $item['stock_id']);
    $this->db->update('table', $update);

    # or simple way /One Line

    $this->db->update('table', $update, array('no_trans' => $item['no_trans'], 'stock_id' => $item['stock_id'] ));
}

推荐阅读