0

Hi I am using the Helper::ResultSet::SetOperations libraries to find the union and intersection of some resultsets. If I know the number of the resultsets then things work fine, however I'm trying to get it to work for an unknown number of resultsets.

The following works when processing 3 'devices'

my $firstdevice = shift @{$devices};
my $rs1 = $self->search({ 'devices.devicename' => $firstdevice },  {  join => { devicetype => 'devices' },  result_class => 'DBIx::Class::ResultClass::HashRefInflator' } ); 


my $seconddevice = shift @{$devices};
my $rs2 = $self->search({ 'devices.devicename' => $seconddevice },  {  join => { devicetype => 'devices' },result_class => 'DBIx::Class::ResultClass::HashRefInflator' }  );   

my $thirddevice= shift @{$devices};
my $rs3 = $self->search({ 'devices.devicename' => $thirddevice },  {  join => { devicetype => 'devices' },result_class => 'DBIx::Class::ResultClass::HashRefInflator' }  );            

my $data = [$rs1->union([$rs2, $rs3])->all];

However if I try to handle it for an unknown amount like below, I'm getting

Can't call method "result_class" on unblessed reference at /usr/local/share/perl/5.18.2/DBIx/Class/Helper/ResultSet/SetOperations.pm line 63.

when I run:

my $data = [$rs1->union([@rslist])->all];

Below is my attempt at getting it to work:

#shift off the first device as we still need $rs1
my $firstdevice = shift @{$devices};
my $rs1 = $self->search({ 'devices.devicename' => $firstdevice },  {  join => { devicetype => 'devices' },  result_class => 'DBIx::Class::ResultClass::HashRefInflator' } ); 


my @rslist;

for my $device (@{$devices}) {
    push @rslist, $self->search({ 'devices.devicename' => $device },  {  join => { devicetype => 'devices' },  result_class => 'DBIx::Class::ResultClass::HashRefInflator' } );          
}

my $data = [$rs1->union([@rslist])->all];
Alexander Hartmaier
  • 2,178
  • 12
  • 21
user1768233
  • 1,409
  • 3
  • 20
  • 28

1 Answers1

1

When you're pushing the return values of ->search to @rslist that's list context so search doesn't return a resultset but a list of the result objects which are hashrefs because of the HRI result_class. Using search_rs instead will fix your issue.

As union takes an arrayref of resultsets I'd pass \@rslist instead of constructing a new arrayref.

Alexander Hartmaier
  • 2,178
  • 12
  • 21