我将 Postgres 与 Kohana 3 的 ORM 模块一起使用,并希望在进行比较之前使用 postgres 函数运行 SELECT 以将数据库中已有的值转换为小写。
在 SQL 中我会写:
select * from accounts where lower(email) = 'alice@spam.com';
在 Kohana 我想写这样的东西:
$user = ORM::factory('user')
->where('lower(email)', '=', strtolower('alice@spam.com'))
->find();
但这会产生错误,因为 ORM 试图将列名推断为“lower(email)”而不仅仅是“email”。
我是 Kohana 和 ORM 的新手,所以能给我相同结果的替代方案也很有用。
最佳答案
或者恕我直言,试试这个:
$user = ORM::factory('user')
->where('LOWER("email")', '=', DB::expr("LOWER('alice@spam.com')"))
->find();
附言。我认为不需要创建 DB::lower() 助手,但这可能只是我...
编辑:
$value = 'alice@spam.com';
$user = ORM::factory('user')
->where('LOWER("email")', '= LOWER', (array) $value)
->find();
查询将变成类似(有一段时间没有使用 ORM)“SELECT users.id, users.email FROM users WHERE LOWER("email") = LOWER ('alice@spam.com') LIMIT 1”。 请注意空格,我刚刚更新了一些代码以使用它,因为我刚刚发现了这种可能性。
我希望你会像我一样对它感到满意。
关于postgresql - Kohana 3 ORM select查询中如何使用数据库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3161852/