2013-08-01 55 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); 

} 
相關問題