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

时间:2012-03-06 05:15:26

标签: php sql codeigniter

这是我试图通过活动记录实现的查询:

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

这是我对活动记录的尝试:

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

这是实际生成的查询:

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

为什么此活动记录语句不应用我的join子句?

2 个答案:

答案 0 :(得分:16)

下面的解决方案怎么样?有点难看,但它达到了你在你的问题中所期望的。

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

答案 1 :(得分:1)

更干净,因为update接受第三个“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));