phildeman has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to query data where a column falls between two dates.

my @events = $schema->resultset( 'MyTable' )->search({ eventdate => { '>=' => $date1 }, eventdate => { '=<' => $date2 } });

I have also tried doing

my @events = $schema->resultset( 'MyTable' )->search({ eventdate => {between => [$date1,$date2]}});

Each time it fails. It gives me the following error:

expected 'returns', found 'date_range' at /my_app_path/my_app/lib/perl5/MooseX/Method/Signatures.pm line 191.

Do you know a way to query by date range with DBIx::Class?

Thanks

Replies are listed 'Best First'.
Re: Querying Date Ranges with DBIx::Class
by tangent (Parson) on Sep 09, 2014 at 02:33 UTC
    I'm not sure if any of this is causing the error you show but there are a couple of things wrong in your code.

    In the first case one of your eventdates is clobbered by the other, and you need to change '=<' to '<=':

    my @events = $schema->resultset( 'MyTable' )->search({ eventdate => { '>=' => $date1, '<=' => $date2 } });
    In the second case 'between' should be '-between':
    my @events = $schema->resultset( 'MyTable' )->search({ eventdate => { -between => [ $date1, $date2 ] } });

      Thanks for your feed back.

      Regarding the first example, I made the correction and I still got the same error. With the second example, I typed the code into the textarea of this form, rather than copying and pasting. So, in my actual code I do have the hyphen prefixed to between.

      Just another thought, $date1 and $date2 are form values. Should I be converting those values into DateTime objects, then passing it in to the query (in the first example)?