2016-03-28 6 views
1

CodeigniterのActive Record CI2を使用してJOINされたテーブルでデータを更新する可能性はありますか?コードを実行していて、Codeigniterのアクティブレコードに変換したいと思っています。CodeigniterのActive Record CI2を使用してJOINされたテーブルでデータを更新する方法は?

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 hmmm、データを選択するときにのみ 'join'を使うことができると知っている限り...あなたは' t2.read_date'を更新していますので、基本的に 'where節'を't1.om_id'は' om_id'が既にあなたの 'table2の外部キーです。なぜ' t2.om_id'の 'where節'を直接しないのですか...あなたは自分でそれを難し​​くしています... –

関連する問題