添加一个条款,到MySQL的语句,而无需使用codeIgniter的活动记录功能报价语句、条款、功能、MySQL

2023-09-09 22:00:37 作者:触不可及

我想在codeigniter使用活动记录,以更新行,我只想增加一个字段值(received_qty = received_qty +1),我意识到,我可以做,在通常的SQL,但我不能在codeigniter的活动记录

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

$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->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的逃逸一切摆成一组(),其中()和许多其他的方法。在哪里,设置既可以利用$一个可选的第三个参数逃脱这是一个布尔值。它设置为false,codeIgniter不会逃避任何东西,这意味着你的领域增量不会被视为一个字符串。

ActiveRecord escapes everything put into a set(), where() and many other methods. Where and set can both take an optional third parameter of $escape which is a boolean. Set it to FALSE and CodeIgniter will not escape anything, meaning your field increment wont be treated as a string.