Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Querying Date Ranges with DBIx::Class

by phildeman (Scribe)
on Sep 08, 2014 at 21:14 UTC ( [id://1099902]=perlquestion: print w/replies, xml ) Need Help??

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)?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1099902]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2024-03-28 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found