简体   繁体   English

具有连接的Codeigniter活动记录更新语句

[英]Codeigniter active record update statement with a join

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', cd.amount_verified = '1' 
WHERE i.invoice_id =  '13';

This is my attempt at the active record: 这是我对活动记录的尝试:

$data = array('cd.amount'=>$amount, 'cd.amount_verified'=>'1');
$this->db->join('Invoices i', 'i.cd_id = cd.cd_id')
     ->where('i.invoice_id', $invoiceId);


// update the table with the new data
if($this->db->update('Customer_donations cd', $data)) {
  return true;
}

And this is the query that's actually getting produced: 这是实际生成的查询:

UPDATE `Customer_donations` cd 
SET `cd`.`amount` = '1', `cd`.`amount_verified` = '1' 
WHERE `i`.`invoice_id` =  '13'

Why is this active record statement not applying my join clause? 为什么这个活动记录语句没有应用我的join子句?

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

Even cleaner, since update accepts a third "where" array parameter: 甚至更干净,因为更新接受第三个“where”数组参数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM