From: Luke Saunders Date: Mon, 5 Jun 2006 09:45:38 +0000 (+0000) Subject: fixed find on related resultset bug X-Git-Tag: v0.07002~75^2~130^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=08a39415df06d59dde0e545bac5c7dd46a8f4a03;p=dbsrgits%2FDBIx-Class.git fixed find on related resultset bug --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 6138871..d6d50cf 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -353,7 +353,8 @@ sub _unique_queries { # Add the ResultSet's alias foreach my $key (grep { ! m/\./ } keys %$unique_query) { - $unique_query->{"$self->{attrs}->{alias}.$key"} = delete $unique_query->{$key}; + my $alias = ($self->{attrs}->{_live_join}) ? $self->{attrs}->{_live_join} : $self->{attrs}->{alias}; + $unique_query->{"$alias.$key"} = delete $unique_query->{$key}; } push @unique_queries, $unique_query; @@ -483,8 +484,9 @@ sub _is_unique_query { my $collapsed = $self->_collapse_query($query); + my $alias = ($self->{attrs}->{_live_join}) ? $self->{attrs}->{_live_join} : $self->{attrs}->{alias}; foreach my $name ($self->result_source->unique_constraint_names) { - my @unique_cols = map { "$self->{attrs}->{alias}.$_" } + my @unique_cols = map { "$alias.$_" } $self->result_source->unique_constraint_columns($name); # Count the values for each unique column @@ -492,7 +494,7 @@ sub _is_unique_query { foreach my $key (keys %$collapsed) { my $aliased = $key; - $aliased = "$self->{attrs}->{alias}.$key" unless $key =~ /\./; + $aliased = "$alias.$key" unless $key =~ /\./; next unless exists $seen{$aliased}; # Additional constraints are okay $seen{$aliased} = scalar @{ $collapsed->{$key} }; @@ -783,7 +785,13 @@ sub _merge_attr { } } } - + if ($is_prefetch) { + my $final_array = []; + foreach my $element (@{$array}) { + push(@{$final_array}, $element) unless (exists $hash->{$element}); + } + $array = $final_array; + } if ((keys %{$hash}) && (scalar(@{$array} > 0))) { return [$hash, @{$array}]; } else { diff --git a/t/90join_torture.t b/t/90join_torture.t index 5ab2b83..532be1a 100644 --- a/t/90join_torture.t +++ b/t/90join_torture.t @@ -7,7 +7,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 6; +plan tests => 8; 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"); @@ -25,7 +25,7 @@ cmp_ok(scalar @cds, '==', 1, "condition based on inherited join okay"); # 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"); +cmp_ok($rs3->count, '==', 1, "Three artists returned"); my $rs4 = $schema->resultset("CD")->search({ 'artist.artistid' => '1' }, { join => ['tracks', 'artist'], prefetch => 'artist' }); my @rs4_results = $rs4->all; @@ -36,4 +36,12 @@ is($rs4_results[0]->cdid, 1, "correct artist returned"); my $rs5 = $rs4->search({'tracks.title' => 'Sticky Honey'}); is($rs5->count, 1, "search without using previous joins okay"); +my $record_rs = $schema->resultset("Artist")->search(undef, { join => 'cds' })->search(undef, { prefetch => { 'cds' => 'tracks' }}); +my $record_jp = $record_rs->next; +ok($record_jp, "prefetch on same rel okay"); + +my $cd = $schema->resultset("CD")->find(1); +my $producers = $cd->producers; +is($producers->find(2)->name, 'Bob The Builder', "find on many to many okay"); + 1;