CodeIgniter Forums
Active Record; adding to an existing value? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record; adding to an existing value? (/showthread.php?tid=5580)



Active Record; adding to an existing value? - El Forum - 01-27-2008

[eluser]_asdf[/eluser]
Using the activerecord class, I'm trying to update an existing value (think post count, etc) by N.

In theory, it'd go like:
Code:
$this->db->set('columnName', 'columnName + 1');
$this->db->where('columnId', $id);

But of course, the AR itself escapes it, so you end up with a query like
Code:
UPDATE TableName SET columnName = 'columnName + 1' WHERE columnId = '1'

Then I figured maybe I could do it the way one does where comparisons, like so:
Code:
$this->db->set('columnName = columnName + ', 1);
but that produces queries like:
Code:
UPDATE TableName SET columnName = columnName + = 1 WHERE columnId = '1'

Is there a way to get this to produce the correct query, eg:
Code:
UPDATE TableName SET columnName = columnName + 1 WHERE columnId = '1'
or do I really need to do it the non-ActiveRecord way? (ick). It'd be the only place in the application where I've had to roll normal SQL, so I'd like to avoid it for consistency, if possible.

Any help & Suggestions most appreciated.

(Its been ages since I used CI, but its so nice to be back. Aside from this one sticking issue.)


Active Record; adding to an existing value? - El Forum - 01-27-2008

[eluser]xwero[/eluser]
version 1.6 (SVN) solves this issue with an extra parameter
Code:
$this->db->set('columnName', 'columnName + 1',false);