CodeIgniter Forums
Active Record - Incrementing a field - [resolved] - 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 - Incrementing a field - [resolved] (/showthread.php?tid=29519)



Active Record - Incrementing a field - [resolved] - El Forum - 04-12-2010

[eluser]Unknown[/eluser]
Hi,

I am writing some code to increment a field in my database.

Code:
$data = array(
    'inc_field' => '(incField+1)'
);

$this->db->where('id', $id);
$this->db->update('my_table', $data);

I would normally write this as:
Code:
UPDATE my_table SET inc_field = (inc_field+1) WHERE id='{$id}'

CodeIgniter adds single quotes around (incField+1), as you would expect. However, this defeats my objective.

Is there a recommended way to achieve my goals? Or is my best alternative to modify the Active Record code?

Thanks,
David Edwards.

edit:

Code:
$this->db->set('inc_field', '(inc_field+1)', false);

This code filled my need. The false prevents escaping.


Active Record - Incrementing a field - [resolved] - El Forum - 04-12-2010

[eluser]cahva[/eluser]
Yep thats it. Also you dont actually need the parentheses so you can leave them out.