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;