From: Luke Saunders Date: Fri, 2 Jun 2006 20:07:43 +0000 (+0000) Subject: fixed a bug with search on related resultset X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=00a80124c31c66230ee825d6bfea040baf9a65ff;p=dbsrgits%2FDBIx-Class-Historic.git fixed a bug with search on related resultset --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index ec41bc5..6138871 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -314,7 +314,6 @@ sub find { } my @unique_queries = $self->_unique_queries($input_query, $attrs); -# use Data::Dumper; warn Dumper $self->result_source->name, $input_query, \@unique_queries, $self->{attrs}->{where}; # Handle cases where the ResultSet defines the query, or where the user is # abusing find @@ -483,7 +482,6 @@ sub _is_unique_query { my ($self, $query) = @_; my $collapsed = $self->_collapse_query($query); -# use Data::Dumper; warn Dumper $query, $collapsed; foreach my $name ($self->result_source->unique_constraint_names) { my @unique_cols = map { "$self->{attrs}->{alias}.$_" } @@ -700,15 +698,15 @@ sub _resolve { $attrs->{seen_join} ||= {}; my %seen; if (my $join = delete $attrs->{join}) { - foreach my $j (ref $join eq 'ARRAY' ? @$join : ($join)) { - if (ref $j eq 'HASH') { + foreach my $j (ref $join eq 'ARRAY' ? @$join : ($join)) { + if (ref $j eq 'HASH') { $seen{$_} = 1 foreach keys %$j; - } else { + } else { $seen{$j} = 1; - } - } + } + } - push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias}, $attrs->{seen_join})); + push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias}, $attrs->{seen_join})); } $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct}; $attrs->{order_by} = [ $attrs->{order_by} ] if @@ -927,7 +925,6 @@ sub count { my $self = shift; return $self->search(@_)->count if @_ and defined $_[0]; return scalar @{ $self->get_cache } if $self->get_cache; - my $count = $self->_count; return 0 unless $count; @@ -965,7 +962,10 @@ sub _count { # Separated out so pager can get the full count # offset, order by and page are not needed to count. record_filter is cdbi delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/; - my ($count) = (ref $self)->new($self->result_source, $attrs)->cursor->next; + my $tmp_rs = (ref $self)->new($self->result_source, $attrs); + $tmp_rs->{_parent_rs} = $self->{_parent_rs} if ($self->{_parent_rs}); #XXX - hack to pass through parent of related resultsets + + my ($count) = $tmp_rs->cursor->next; return $count; } @@ -1558,8 +1558,8 @@ sub related_resultset { { %{$self->{attrs}}, select => undef, as => undef, - join => $rel, - _live_join => $rel } + join => $rel, + _live_join => $rel } ); # keep reference of the original resultset diff --git a/t/90join_torture.t b/t/90join_torture.t index e6510e4..5ab2b83 100644 --- a/t/90join_torture.t +++ b/t/90join_torture.t @@ -7,7 +7,10 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 5; +plan tests => 6; + +my @rs1a_results = $schema->resultset("Artist")->search_related('cds', {title => 'Forkful of bees'}, {order_by => 'title'}); +is($rs1a_results[0]->title, 'Forkful of bees', "bare field conditions okay after search related"); my $rs1 = $schema->resultset("Artist")->search({ 'tags.tag' => 'Blue' }, { join => {'cds' => 'tracks'}, prefetch => {'cds' => 'tags'} }); my @artists = $rs1->all; @@ -19,9 +22,10 @@ my @artists2 = $rs2->search({ 'producer.name' => 'Matt S Trout' }); my @cds = $artists2[0]->cds; cmp_ok(scalar @cds, '==', 1, "condition based on inherited join okay"); -my $rs3 = $rs2->search_related('cds')->search({'title' => 'Forkful of bees'}); -cpm_ok($rs3->count, '==', 3, "Three artists returned"); -exit; +# this is wrong, should accept me.title really +my $rs3 = $rs2->search_related('cds')->search({'cds.title' => 'Forkful of bees'}); + +cmp_ok($rs3->count, '==', 3, "Three artists returned"); my $rs4 = $schema->resultset("CD")->search({ 'artist.artistid' => '1' }, { join => ['tracks', 'artist'], prefetch => 'artist' }); my @rs4_results = $rs4->all;