Codeigniter active record update statement with a join

后端 未结 2 1025
夕颜
夕颜 2020-12-30 12:24

This is the query that i\'m trying to achieve via active record:

UPDATE `Customer_donations` cd 
join Invoices i on i.cd_id = cd.cd_id 
set cd.amount = \'4\'         


        
相关标签:
2条回答
  • 2020-12-30 12:45

    How about the solution below? A bit ugly but it achieved what you expected in your question.

    $invoiceId = 13;
    $amount = 4;
    $data = array('cd.amount'=>$amount, 'cd.amount_verified'=>'1');
    
    $this->db->where('i.invoice_id', $invoiceId);
    
    $this->db->update('Customer_donations cd join Invoices i on i.cd_id = cd.cd_id', $data);
    
    0 讨论(0)
  • 2020-12-30 12:51

    Even cleaner, since update accepts a third "where" array parameter:

    $invoiceId = 13;
    $amount = 4;
    $data = array('cd.amount'=>$amount, 'cd.amount_verified'=>'1');
    $this->db->update('Customer_donations cd join Invoices i on i.cd_id = cd.cd_id', 
      $data, array('i.invoice_id' => $invoiceId));
    
    0 讨论(0)
提交回复
热议问题