How to use BETWEEN clause in Kohana ORM
I am new to kohana. I am using ORM for getting data.
I want to build a query in which BETWEEN
clause is there. as following
SELECT `rooms`.* FROM `rooms` LEFT JOIN `events` ON (`rooms`.`id` = `events`.`room_id`)
WHERE `events`.`room_id` IS NULL
OR (`events`.`eventstart` NOT BETWEEN 1312210800 AND 1312218000)
for that i am doing the following
$rooms = $room->join('events', 'LEFT')
->on('rooms.id', '=', 'events.room_id')
->where('events.room_id', 'IS', NULL)
->and_where_open()
->or_where('events.eventstart' , '开发者_JS百科NOT BETWEEN', $from)
->and_where_close()
->find_all();
But i am getting the the query like this
SELECT `rooms`.* FROM `rooms` LEFT JOIN `events` ON (`rooms`.`id` = `events`.`room_id`)
WHERE `events`.`room_id` IS NULL AND (`events`.`eventstart` NOT BETWEEN 1312210800)
Can someone point out how to use BETWEEN clause
I think you should use or_where('events.eventstart', 'BETWEEN', array($from, $to));
Documentation on Kohana Query Builder can be found here -> http://kohanaframework.org/3.2/guide/database/query/builder
ORM uses Query Builder.
The only way I've found that works reliably is to use DB::expr()
.
Consider the following:
or_where('events.eventstart', 'NOT BETWEEN', DB::expr('1312210800 AND 1312218000'));
Or you can stick it all in variables:
$range = "{$from} AND {$to}";
or_where('events.eventstart', 'NOT BETWEEN', DB::expr($range));
精彩评论