2013-08-01 56 views
0

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

我的阵列

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条款(id_customer和id_section)在2个参数,但如果我用update_batch,我只能指定一个参数。 如何在WHERE条款中设置几个参数?

+1

在这个Q上的外观和A - http://stackoverflow.com/questions/7426094/codeigniter-批量更新与多个条件 – Sharif

+0

使用多个'where'?像'$ this-> db-> where('id_section',$ id_section);''this-> db-> where('id_customer',$ id_customer);' – Bora

回答

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和标题你所需的列所取代。

+0

这是多种方式之一。 +1 – snh

+0

如果他需要从$ data数组中获取参数2,该怎么办?这只适用于给定的ID? – sinhix

+0

@lxark只需添加另一个'$ this-> db-> where($ this-> db-> where('id',$ id);'statement ... –

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); 

} 
相关问题