Question

Hello, I am building queries with Kohana Query Builder, and I'm trying to get this kind of query:

UPDATE `report_count` SET `report_count`=  report_count + 1;

What i have right now is:

DB::update('report_count')->set(array('report_count' => 'report_count + 1'));

And it outputs this query:

UPDATE `report_count` SET `report_count` = 'report_count + 1'

So my problem is that it puts ' ' around report_count + 1. How can I remove these?

Was it helpful?

Solution

You need to use an expression object. Kohana's query builder lets you create expressions with DB::expr.

The query builder will normally escape all its input, as you'd want it to, but text supplied as an expression object will be included in the query as-is.

The example given in the documentation is basically your exact situation:

$query = DB::update('users')->set(array('login_count' => DB::expr('`login_count` + 1')))->where('id', '=', $id);

This generates a query like the following (the id value of 45 is just an example):

UPDATE `users` SET `login_count` = `login_count` + 1 WHERE `id` = 45
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top