[Dbix-class] Constructing a resultset()->search() with variable number of ORs?

Moritz Onken onken at houseofdesign.de
Tue Oct 27 12:40:24 GMT 2009


Am 26.10.2009 um 17:36 schrieb Chris Cole:

> Hi,
>
> I'd like to convert the below code to using a variable number of ORs  
> on
> the 'name' column. i.e. there may be a data3 as well as data1 and  
> data2.
>
> my $schema = DB::Schema->connect();
>
> my @rows = $schema->resultset('Reads2expt')->search(
>   # select name rows on data1 OR data2 with abundance cutoffs
>   [
>      {
>         name => 'data1',
>         abundance => { '>' => 1 }
>      },
>      {
>         name => 'data2',
>         abundance => { '>' => 1 }
>      }
>
>   ],
>   {
>      join => [qw/ read_id expt_id /],
>      order_by => 'me.read_id',
>      group_by => 'me.read_id',
>      prefetch => 'read_id',
>      cache => 1
>   }
> );
>
> I've gotten this far:
> my @ors;
> foreach my $data (qw/data1 data2/) {
>   push @ors, { name => $data, abundance => { '>' => $abund } };
> }
> my @rows = $schema->resultset('Reads2expt')->search(
>   @ors,
>   {
>      join => [qw/ read_id expt_id /],
>      order_by => 'me.read_id',
>      group_by => 'me.read_id',
>      prefetch => 'read_id',
>      cache => 1
>   }
> );

The first parameter needs to be an array ref:

> $schema->resultset('Reads2expt')->search(
>   \@ors,
>   {
>      join => [qw/ read_id expt_id /],
>      order_by => 'me.read_id',
>      group_by => 'me.read_id',
>      prefetch => 'read_id',
>      cache => 1
>   }
> );




More information about the DBIx-Class mailing list