Codeigniter Active Record多次更新

时间:2013-08-01 14:25:05

标签: php codeigniter activerecord

如何使用活动记录更新多行?

我的阵列

foreach($services as $service){

        $this->service_batch[]=array(
            'service_id'=>$service->service_id,
            'service_count'=>$service->service_count,
            'id_customer'=>$service->id_customer,
            'id_section'=>$service->id_section);
        }

当我更新表时,我需要在WHERE子句中定义2个参数(id_customer AND id_section),但如果我使用update_batch,我只能指定一个参数。 如何在WHERE子句中设置一些参数?

2 个答案:

答案 0 :(得分:1)

来自Code Igniter Documentation - See Update Batch

的类似内容
 $data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

$this->db->where($this->db->where('id', $id);
$this->db->update_batch('mytable', $data, 'title'); 

在您的情况下,数据将被$this->service_batch替换为您所需列的标题。

答案 1 :(得分:0)

不太确定能够捕捉到你真正需要的东西,但我会这么做:

//controller

foreach($services as $service){

        $this->model->update(
          $service->service_id,
          array(
            'service_count'=>$service->service_count,
            'id_customer'=>$service->id_customer,
            'id_section'=>$service->id_section
          ));
        }

//model


 function update($id,$data){

    $this->db->where('id',$id);
    return  $this->db->update('my_table',$data);

}
相关问题