Question

I'm using Postgres with Kohana 3's ORM module and would like to run a SELECT using a postgres function to convert values already in the database to lower case before doing the comparison.

In SQL I would write:

select * from accounts where lower(email) = 'alice@spam.com';

In Kohana I would like to write something like this:

$user = ORM::factory('user')
    ->where('lower(email)', '=', strtolower('alice@spam.com'))
    ->find();

But this gives an error because ORM is trying to deduce the column name as 'lower(email)' rather than just 'email'.

I'm new to Kohana and ORM so alternatives that would give me the same result would be useful too.

Was it helpful?

Solution

Or IMHO even beter, try this:

$user = ORM::factory('user')
    ->where('LOWER("email")', '=', DB::expr("LOWER('alice@spam.com')"))
    ->find();

PS. I do not see any need to create a DB::lower() helper, but that might just be me...

EDIT:

$value = 'alice@spam.com';

$user = ORM::factory('user')
    ->where('LOWER("email")', '= LOWER', (array) $value)
    ->find();

The query will become something like (havent used ORM in a while) "SELECT users.id, users.email FROM users WHERE LOWER("email") = LOWER ('alice@spam.com') LIMIT 1". Notice the space, I just updated some of my code to use this since I just figured out this posibility.

I hope you will be as happy with it as I am.

OTHER TIPS

try this:

$user = ORM::factory('user')
    ->where(DB::expr('lower(email)'), '=', strtolower('alice@spam.com'))
    ->find();

I'm not completely happy with the use of a helper but I use it a couple other classes so it's nice to keep the logic in one location. Here is what I'm currently using.

class DB extends Kohana_DB
{
    public static function lower($value)
    {
        return DB::expr('lower('.Database::instance()->quote($value).')');
    }
}

class Model_User extends Model_Base
{
    public static function find_by_email($email)
    {
        $user = ORM::factory('user')
            ->where(DB::expr('lower(email)'), '=', DB::lower($email))
            ->find();
        return $user;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top