X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSource.pm;h=9265d329bdfb2bbaf1cbd678a1f36bf9e75e0b6f;hb=9c808a1bbd6ee5425d14866f165b12c9d40290b9;hp=1b9baa87a98e3fd0e889c17160ec3b775994bdc1;hpb=a77f5ab1c0f242d27d3f77018574c56172bdd95c;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index 1b9baa8..9265d32 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -1424,19 +1424,7 @@ sub _resolve_prefetch { "Can't prefetch has_many ${pre} (join cond too complex)") unless ref($rel_info->{cond}) eq 'HASH'; my $dots = @{[$as_prefix =~ m/\./g]} + 1; # +1 to match the ".${as_prefix}" - if (my ($fail) = grep { @{[$_ =~ m/\./g]} == $dots } - keys %{$collapse}) { - my ($last) = ($fail =~ /([^\.]+)$/); - carp ( - "Prefetching multiple has_many rels ${last} and ${pre} " - .(length($as_prefix) - ? "at the same level (${as_prefix}) " - : "at top level " - ) - . 'will explode the number of row objects retrievable via ->next or ->all. ' - . 'Use at your own risk.' - ); - } + #my @col = map { (/^self\.(.+)$/ ? ("${as_prefix}.$1") : ()); } # values %{$rel_info->{cond}}; $collapse->{".${as_prefix}${pre}"} = [ $rel_source->primary_columns ]; @@ -1458,6 +1446,46 @@ sub _resolve_prefetch { } } +# Takes a hashref of $sth->fetchrow values keyed to the corresponding +# {as} dbic aliases, and splits it into a native columns hashref +# (as in $row->get_columns), followed by any non-native (prefetched) +# columns, presented in a nested structure resembling an HRI dump. +# The structure is constructed taking into account relationship metadata +# (single vs multi). +# The resulting arrayref resembles the arguments to ::Row::inflate_result +# For an example look at t/prefetch/_util.t +# +# The will collapse flag is for backwards compatibility only - if it is +# set, all relationship row-parts are returned as hashes, even if some +# of these relationships are has_many's +# +sub _parse_row { + my ( $self, $row, $will_collapse ) = @_; + + my ($me, $pref); + + foreach my $column ( keys %$row ) { + if ( $column =~ /^ ([^\.]+) \. (.*) $/x ) { + $pref->{$1}{$2} = $row->{$column}; + } + else { + $me->{$column} = $row->{$column}; + } + } + + foreach my $rel ( keys %{$pref||{}} ) { + my $rel_info = $self->relationship_info($rel); + + $pref->{$rel} = + $self->related_source($rel)->_parse_row( $pref->{$rel}, $will_collapse ); + + $pref->{$rel} = [ $pref->{$rel} ] + if ( $will_collapse && $rel_info->{attrs}{accessor} eq 'multi' ); + } + + return [ $me||{}, $pref||() ]; +} + =head2 related_source =over 4