Kohana 3 ORM compares 2 columns in where clause
-
07-02-2021 - |
Question
I need to generate query like that :
SELECT * FROM `table1` WHERE `date1` < `date2`
I can't find how to compare 2 columns in kohana ORM. Here date2 is considered as text.
$foo = ORM::factory('model1')->where('date1','<','date2');
How can I write this line ?
Thanks!
More info:
I use this for the moment:
$query = DB::query(Database::SELECT, "SELECT id FROM table1 WHERE `date1` < `date2`");
$result = $query->execute();
$foo = array();
foreach ($result as $r) {
$foo[] = ORM::factory("model1", $r['id']);
}
Solution
If you don't want Kohana to modify the string, as it would with the 3rd argument in the DB where function, you can use the DB::expr()
function which will leave what you pass unmodified. So with your example, you could use
$foo = ORM::factory('model1')->where('date1','<',DB::expr('date2'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow