Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

$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?

like image 645
strike_noir Avatar asked Jan 16 '10 02:01

strike_noir


2 Answers

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. 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.

like image 163
Phil Sturgeon Avatar answered Nov 13 '22 00:11

Phil Sturgeon


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

like image 36
LukeP Avatar answered Nov 13 '22 01:11

LukeP