From: Luke Saunders Date: Mon, 29 Oct 2007 20:26:02 +0000 (+0000) Subject: fixed _merge_attr bug X-Git-Tag: v0.08010~39 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aab9e887504907201b3cc0a8c40832efcb43e5dd;p=dbsrgits%2FDBIx-Class.git fixed _merge_attr bug --- diff --git a/Changes b/Changes index 8fea678..45cc7fb 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,5 @@ Revision history for DBIx::Class + - Fixed join merging bug (test from Zbi) - When adding relationships, it will throw an exception if you get the foreign and self parts the wrong way round in the condition - ResultSetColumn::func() now returns all results if called in list diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index c72b38b..cf82b83 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2078,10 +2078,10 @@ sub _merge_attr { $position++; } my ($b_key) = ( ref $b_element eq 'HASH' ) ? keys %{$b_element} : ($b_element); + if ($best_candidate->{score} == 0 || exists $seen_keys->{$b_key}) { push( @{$a}, $b_element ); } else { - $seen_keys->{$b_key} = 1; # don't merge the same key twice my $a_best = $a->[$best_candidate->{position}]; # merge a_best and b_element together and replace original with merged if (ref $a_best ne 'HASH') { @@ -2091,6 +2091,7 @@ sub _merge_attr { $a->[$best_candidate->{position}] = { $key => $self->_merge_attr($a_best->{$key}, $b_element->{$key}) }; } } + $seen_keys->{$b_key} = 1; # don't merge the same key twice } return $a; diff --git a/t/90join_torture.t b/t/90join_torture.t index 12d2cdf..a277475 100644 --- a/t/90join_torture.t +++ b/t/90join_torture.t @@ -6,7 +6,7 @@ use lib qw(t/lib); use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 20; +plan tests => 22; { my $rs = $schema->resultset( 'CD' )->search( @@ -119,4 +119,10 @@ eval { ok(!$@, "pathological prefetch ok"); +my $rs = $schema->resultset("Artist")->search({}, { join => 'twokeys' }); +my $second_search_rs = $rs->search({ 'cds_2.cdid' => '2' }, { join => +['cds', 'cds'] }); +is(scalar(@{$second_search_rs->{attrs}->{join}}), 3, 'both joins kept'); +ok($second_search_rs->next, 'query on double joined rel runs okay'); + 1; diff --git a/t/91merge_attr.t b/t/91merge_attr.t index 28101a9..6699150 100644 --- a/t/91merge_attr.t +++ b/t/91merge_attr.t @@ -6,7 +6,7 @@ use lib qw(t/lib); use DBICTest; use Test::More; -plan tests => 14; +plan tests => 15; my $schema = DBICTest->init_schema(); my $rs = $schema->resultset( 'CD' ); @@ -52,6 +52,14 @@ my $rs = $schema->resultset( 'CD' ); } { + my $a = [ 'twokeys' ]; + my $b = [ 'cds', 'cds' ]; + my $expected = [ 'twokeys', 'cds', 'cds' ]; + my $result = $rs->_merge_attr($a, $b); + is_deeply( $result, $expected ); +} + +{ my $a = [ 'artist', 'cd', { 'artist' => 'manager' } ]; my $b = 'artist'; my $expected = [ 'artist', 'cd', { 'artist' => 'manager' } ];