postgresql - Kohana 3 ORM select查询中如何使用数据库函数

标签 postgresql kohana kohana-3 kohana-orm

我将 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/

相关文章:

php - 在使用 Kohana 包装器的 Swift 邮件程序方面需要帮助

sql - 如何从批处理文件执行 postgres 的 sql 查询?

sql - PostgreSQL,在插入之前检查重复项

mysql - 在 Kohana 查询中使用表别名?

php - Kohana PHP - 具有共享模型的多个应用程序

mysql - 使用 MySQL 查找链接表中没有任何条目的项目

linux - 在部署脚本中查找已安装的包时,是否应该检查所有可能的路径?

node.js - Sequelize 错误 : Unhandled rejection TypeError: val. 替换不是函数?

php - 如何在 Kohana 中使用 MongoDB?

kohana-3 - Kohana ErrorException [ fatal error ]:调用未定义的方法 Request::redirect()