9

I haven't found a way to do unions with DBIx::Class other than using a view and writing out the SQL manually. This seems strange to me. I feel like there should be some way to union two ResultSets without a lot of extra work because set addition and subtraction are such a core part of SQL. Is there an easier way to do unions? If not, why not?

cjm
  • 61,471
  • 9
  • 126
  • 175
Eric Johnson
  • 17,502
  • 10
  • 52
  • 59

2 Answers2

13

DBIx::Class::Helper::ResultSet::SetOperations

my $rs1 = $rs->search({ foo => 'bar' });  
my $rs2 = $rs->search({ baz => 'biff' });  
for ($rs1->union($rs2)->all) { ... }
user52889
  • 1,501
  • 8
  • 16
aartist
  • 3,145
  • 3
  • 33
  • 31
0

As a workaround (without having to load more modules) I did something like this:

$db->resultset("Foo")->search({ -or => [ 
                                        'me.id' => { -in => $result_set_a },
                                        'me.id' => { -in => $result_set_b } 
                                       ]
                              },
                              undef);
Blaskovicz
  • 6,122
  • 7
  • 41
  • 50