From 25e3f92a4978f24a0ff1a4ab5c83e3b830812098 Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Mon, 26 Mar 2012 10:32:43 -0400 Subject: [PATCH] Handle all ways of getting arrays at the leaves We can get arrays of values be either: - ending on a $field within a has_many reltype - passing through a path that is a has_many reltype This detects either and array-ifies the value on the flat row. Signed-off-by: Mike Rylander --- .../perlmods/lib/OpenILS/Application/Flattener.pm | 30 +++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Flattener.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Flattener.pm index 73d73be574..a6c77d347e 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Flattener.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Flattener.pm @@ -365,21 +365,37 @@ sub process_result { my @path = @{ $mapping->{path} }; my $field = pop @path; - my $obj = $fmobj; + my $objs = [$fmobj]; while (my $step = shift @path) { - $obj = $obj->$step; - last unless ref $obj; + $objs = [ map { $_->$step } @$objs ]; + last unless ref $$objs[0]; } - if (ref $obj eq 'ARRAY') { - # might_have links return arrays - $flatrow->{$key} = [ map {$_->$field} @$obj ]; + # We can get arrays of values be either: + # - ending on a $field within a has_many reltype + # - passing through a path that is a has_many reltype + if (@$objs > 1 or ref $$objs[0] eq 'ARRAY') { + $flatrow->{$key} = []; + for my $o (@$objs) { + push @{ $flatrow->{$key} }, extract_field_value( $o, $field ); + } } else { - $flatrow->{$key} = ref $obj ? $obj->$field : undef; + $flatrow->{$key} = extract_field_value( $$objs[0], $field ); } } return $flatrow; } +sub extract_field_value { + my $obj = shift; + my $field = shift; + + if (ref $obj eq 'ARRAY') { + # has_many links return arrays + return ( map {$_->$field} @$obj ); + } + return ref $obj ? $obj->$field : undef; +} + 1; -- 2.11.0