2016-03-28 29 views
1

使用Codeigniter的活動記錄CI2更新JOINed表的數據是否可行?我有運行代碼,我想轉換爲Codeigniter的活動記錄。如何使用Codeigniter的活動記錄CI2更新JOINed表的數據?

public function update_table($job_id, $om_id, $recipient_id) 
{ 
    $query = "UPDATE table1 AS t1 
         INNER JOIN table2 AS t2 
           ON t1.om_id = t2.id 
       SET t2.read_date = NOW() 
       WHERE t2.ref_table_id = $job_id 
       AND t1.om_id = $om_id 
       AND t2.recipient_id = $recipient_id 
       AND t2.read_date = '0000-00-00 00:00:00'"; 

    $result = $this->db->query($query); 
    return $result; 
} 

我嘗試這樣,但不起作用。

public function update_table($job_id, $om_id, $recipient_id) 
{ 
    $this->db->set('t2.read_date', NOW()); 

    $this->db->where('t2.ref_table_id', $job_id); 
    $this->db->where('t1.om_id', $om_id); 
    $this->db->where('t2.recipient_id', $recipient_id); 
    $this->db->where('t2.read_date', '0000-00-00 00:00:00'); 
    $this->db->where('t2.om_id = t1.id'); 
    $this->db->update('table1 AS t1, table AS t2'); 

} 

任何幫助將不勝感激。由於

回答

0

你可以試試這個..

提供$條件爲陣......同與你想更新數據。

function update_data($table, $data, $condition) 
{ 
    $this->db->where($condition); 
    $this->db->update($table, $data); 
} 
+0

我想這只是CI上的簡單方法,我應該在哪裏放置連接子句? –

+0

@VonnGarcia嗯,據我所知你只能在選擇數據時使用'join'...你只是更新't2.read_date' ...所以基本上你只需要在where子句中't1.om_id'因爲'om_id'已經是你的'table2上的外鍵了,爲什麼不直接在't2.om_id'上做'where子句'....你讓自己很難... –

相關問題