DBIx::Class::ResultSet problems
Asked Answered
G

1

5

I've got the following code:

package MyPackage::ResultSet::Case;
use base 'DBIx::Class::ResultSet';

sub cases_last_fourteen_days {
    my ($self, $username) = @_; 

    return $self->search({
                username    => $username,
                date        => { '>=' => 'DATE_SUB(CURDATE(),INTERVAL 14 DAY)' },
    }); 
};

But when I try to use it this way:

$schema->resultset('Case')->cases_last_fourteen_days($username)

I always get zero results, can anyone tell what I'm doing wrong?

Thanks!

Galacto answered 25/11, 2010 at 19:39 Comment(0)
K
14

The way you use the SQL::Abstract condition would result in this where condition:

WHERE username = ? AND date >= 'DATE_SUB(CURDATE(),INTERVAL 14 DAY)'

When you wish to use database functions in a where clause you need to use a reference to a scalar, like this:

date        => { '>=' => \'DATE_SUB(CURDATE(),INTERVAL 14 DAY)' },

ProTip: if you set the environment variable DBIC_TRACE to 1, DBIx::Class will print the queries it generates to STDERR ... this way you can check if it really does what you wish.

Krilov answered 26/11, 2010 at 12:29 Comment(1)
For resultset it is possible to append ->as_query which will return resulting queryStrother

© 2022 - 2024 — McMap. All rights reserved.