简体   繁体   中英

Kohana query builder update

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM