Everton Z. P.
Everton Z. P.

Reputation: 371

Kohana ORM - mysql conditional inside order_by() method

Today I have an specific "issue" to solve with Kohana (3.3) orm order_by method. I need rewrite the order_by() method to accept anything like that:

->order_by("IF(ISNULL(foo) OR foo = '', 1, 0), foo", "ASC")

(This conditional put all null values in the end of query result). But, the orm escape the query with " ` " then it not work.

ORDER BY `IF(ISNULL(foo) OR foo = "", 1, 0), foo` ASC

I can mount this query using DB::query(), but I need use that conditional in all order_by query on my system.

Upvotes: 0

Views: 257

Answers (1)

Vladimir Cherepinskiy
Vladimir Cherepinskiy

Reputation: 337

Try DB::expr(), its work

->order_by(DB::expr("IF(ISNULL(foo) OR foo = '', 1, 0), foo"), "ASC")

Upvotes: 2

Related Questions