From: Peter Rabbitson Date: Thu, 26 Nov 2009 11:52:05 +0000 (+0000) Subject: Another count() quirk down X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5978a0e8eea03cedc3ad471b84d8eac4769f47eb;p=dbsrgits%2FDBIx-Class-Historic.git Another count() quirk down --- diff --git a/Changes b/Changes index 08cd863..c9ac811 100644 --- a/Changes +++ b/Changes @@ -7,6 +7,7 @@ Revision history for DBIx::Class dispatcher, and a sybase-specific ::DBI::Sybase::ASE - Make sure populate() inherits the resultset conditions just like create() does + - Fix count/objects from search_related on limited resultset 0.08114 2009-11-14 17:45:00 (UTC) - Preliminary support for MSSQL via DBD::ADO diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 97fe8ad..468816b 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2619,27 +2619,45 @@ sub current_source_alias { # The increments happen twice per join. An even number means a # relationship specified via a search_related, whereas an odd # number indicates a join/prefetch added via attributes +# +# Also this code will wrap the current resultset (the one we +# chain to) in a subselect IFF it contains limiting attributes sub _chain_relationship { my ($self, $rel) = @_; my $source = $self->result_source; my $attrs = { %{$self->{attrs}||{}} }; - my $from = [ @{ - $attrs->{from} - || - [{ - -source_handle => $source->handle, - -alias => $attrs->{alias}, - $attrs->{alias} => $source->from, - }] - }]; + my $from; + my @force_subq_attrs = qw/offset rows/; + + if ( + ($attrs->{from} && ref $attrs->{from} ne 'ARRAY') + || + $self->_has_resolved_attr (@force_subq_attrs) + ) { + $from = [{ + -source_handle => $source->handle, + -alias => $attrs->{alias}, + $attrs->{alias} => $self->as_query, + }]; + delete @{$attrs}{@force_subq_attrs}; + } + elsif ($attrs->{from}) { #shallow copy suffices + $from = [ @{$attrs->{from}} ]; + } + else { + $from = [{ + -source_handle => $source->handle, + -alias => $attrs->{alias}, + $attrs->{alias} => $source->from, + }]; + } my $seen = { %{$attrs->{seen_join} || {} } }; my $jpath = ($attrs->{seen_join} && keys %{$attrs->{seen_join}}) ? $from->[-1][0]{-join_path} : []; - # we need to take the prefetch the attrs into account before we # ->_resolve_join as otherwise they get lost - captainL my $merged = $self->_merge_attr( $attrs->{join}, $attrs->{prefetch} ); diff --git a/t/count/search_related.t b/t/count/search_related.t new file mode 100644 index 0000000..b9804ea --- /dev/null +++ b/t/count/search_related.t @@ -0,0 +1,20 @@ +use strict; +use warnings; + +use Test::More; + +use lib qw(t/lib); + +use DBICTest; + +my $schema = DBICTest->init_schema(); +my $cd_rs = $schema->resultset('CD')->search ({}, { rows => 1, order_by => 'cdid' }); + +my $track_count = $cd_rs->first->tracks->count; + +cmp_ok ($track_count, '>', 1, 'First CD has several tracks'); + +is ($cd_rs->search_related ('tracks')->count, $track_count, 'related->count returns correct number'); +is (scalar ($cd_rs->search_related ('tracks')->all), $track_count, 'related->all returns correct number of objects'); + +done_testing;