简体   繁体   English

使用CodeIgniter的Active Record函数在没有引号的MySQL语句中添加子句

[英]Add a clause to a MySQL statement without quotes using CodeIgniter's Active Record functions

I wanted to update a row using active records in codeigniter, and I only want to increment a field value (received_qty = received_qty +1) , I realized that I can do that in usual sql, but I cant in codeigniter active records 我想在codeigniter中使用活动记录更新一行,我只想增加一个字段值(received_qty = received_qty +1),我意识到我可以在通常的sql中做到这一点,但我不能在codeigniter活动记录中

$update['received_qty'] = 'received_qty + 1';
$update['received_date'] = date("Y-m-d");
$this->db->where($where);
$this->db->update('vrs_distribution', $update);

anyone know how to do it using active records? 有人知道如何使用活动记录吗?

This will work. 这会奏效。

$this->db->set('received_qty', 'received_qty + 1', FALSE);
$this->db->set('received_date', date("Y-m-d"));
$this->db->where($where);
$this->db->update('vrs_distribution');

ActiveRecord escapes everything put into a set(), where() and many other methods. ActiveRecord将所有内容放入set(),where()和许多其他方法中。 Where and set can both take an optional third parameter of $escape which is a boolean. where和set都可以使用$ escape的可选第三个参数,它是一个布尔值。 Set it to FALSE and CodeIgniter will not escape anything, meaning your field increment wont be treated as a string. 将其设置为FALSE并且CodeIgniter不会转义任何内容,这意味着您的字段增量不会被视为字符串。

Or you can do: 或者你可以这样做:

$this->db->query('UPDATE vrs_distribution SET received_qty = received_qty + 1, received_date = CURDATE() WHERE id = ' . $id);

You would need to modify WHERE clause to suit you though 您需要修改WHERE子句以适合您

I was going to ask a similar question just a little bit different, but the problem was the same: I needed to update a date with an interval ( expiry_date = expiry_date + interval 3 month ) and Phil Sturgeon 's answer did solve the problem. 我会问一个类似的问题,但问题是相同的:我需要更新一个间隔日期( expiry_date = expiry_date + interval 3 month ),而Phil Sturgeon的答案确实解决了这个问题。

However, what I realized is that you can still use the array for the fields that can have quotes, meaning that you could write: 但是,我意识到你仍然可以将数组用于可以有引号的字段,这意味着你可以写:

$this->db->set('received_qty', 'received_qty + 1', FALSE);
$this->db->set('expired_date', 'CURDATE() + INTERVAL 10 DAY', FALSE); //extra example 1
$update['received_date'] = date("Y-m-d");
$update['receiver'] = $receiver_name; //extra example 2
$this->db->where($where);
$this->db->update('vrs_distribution', $update);

Where $this->db->last_query() would output: $this->db->last_query()输出的位置:

UPDATE `vrs_distribution`
  SET `received_qty` = received_qty + 1,
      `expiry_date` = CURDATE() + INTERVAL 10 DAY, #extra example 1
      `received_date` = '2015-07-01 16:00:00',
      `receiver` = 'strike_noir', #extra example 2
  WHERE #where clause with backticks

Notice that the fields where set() was used do not have quotes and appear in the first place. 请注意,使用set()的字段没有引号并首先出现。 The rest of the query has backticks (letting " CodeIgniter protect the remaining fields "). 查询的其余部分有反引号(让“ CodeIgniter保护剩余的字段 ”)。

status set to zero(Update) 状态设置为零(更新)

$this->db->set('IsCurrent', '0');       
$this->db->where('AcademicYearID',$academicYearId);
$this->db->update('academicyear');  

You seem pretty close, there isn't an 'increment by one' command in CI's ActiveRecord (or in SQL for that matter). 你似乎非常接近,CI的ActiveRecord(或者在SQL中)没有“一个增量”命令。

$update['received_qty']++;
$update['received_date'] = date("Y-m-d");
$this->db->where($where);
$this->db->update('vrs_distribution', $update);

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

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