Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re-using DBIx ResultSet with different parameters

by MattLG (Sexton)
on Oct 13, 2016 at 10:40 UTC ( #1173924=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to work out a way of executing a ResultSet query mutiple times with a different parameter, but despite searching everywhere, I can't find anything that covers the subject.

I have a reasonably complex query:

my $items = $self->db->resultset('Item')->search({},{ '+select' => ... 'join' => ... where => { -or => ... 'start_datetime' => {'<=' => \'NOW()'}, 'end_datetime' => {'>=' => \'NOW()'}, }, group_by => ... order_by => ... });

But I want to get the results multiple times in a loop:

foreach my $value(@values) { my $results = $items->search({ 'joined_table.column' => $value }); # Process and output results of this search ... }

I'd rather not have the query re-prepared multiple times when the only thing that has changed is one piece of data. Is DBIx clever enough to realise that it has created the same underlying query twice and not re-prepare it?

I'm assuming no.

If not, how could I do something like this?

my $results_query = $items->search({ 'joined_table.column' => '?' }); foreach my $value(@values) { my $results = $results_query->bind($value); # Process and output results of this search ... }

The only reference to this sort of thing that I can find is by creating a ResultSource::View class, which seems like overkill just for the sake of some bind parameters.

Replies are listed 'Best First'.
Re: Re-using DBIx ResultSet with different parameters
by NetWallah (Canon) on Oct 14, 2016 at 00:14 UTC
Re: Re-using DBIx ResultSet with different parameters
by ablanke (Vicar) on Oct 15, 2016 at 10:34 UTC

    Hi MattLG,

    have you thought about using

    IN

    in your select?

    In the doc DBIx::ResultSet::Cookbook under subselects.

    Adapted from the doc you could do this (untested):

    $items->search({ 'joined_table.column' => {-in => \@values } });

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1173924]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-07-04 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?