From: Matt S Trout Date: Mon, 6 Feb 2006 17:45:09 +0000 (+0000) Subject: Fixes to deep search and search_related X-Git-Tag: v0.05005~31 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8fab5eef0c0f7f138fdd5f5f23d8cadd48887ba9;p=dbsrgits%2FDBIx-Class.git Fixes to deep search and search_related --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index ddc11b5..e3f4258 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -99,6 +99,11 @@ sub search_related { my $query = ((@_ > 1) ? {@_} : shift); my ($cond) = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self); + foreach my $key (keys %$cond) { + unless ($key =~ m/\./) { + $cond->{"me.$key"} = delete $cond->{$key}; + } + } $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); #use Data::Dumper; warn Dumper($cond); #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}||[]}); diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 4af9b38..acd228c 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -84,6 +84,7 @@ sub new { } #use Data::Dumper; warn Dumper(@{$attrs}{qw/select as/}); $attrs->{from} ||= [ { $alias => $source->from } ]; + $attrs->{seen_join} ||= {}; if (my $join = delete $attrs->{join}) { foreach my $j (ref $join eq 'ARRAY' ? (@{$join}) : ($join)) { @@ -93,7 +94,7 @@ sub new { $seen{$j} = 1; } } - push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias})); + push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias}, $attrs->{seen_join})); } $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct}; @@ -258,10 +259,13 @@ sub search_related { "No such relationship ${rel} in search_related") unless $rel_obj; my $rs = $self->search(undef, { join => $rel }); + my $alias = ($rs->{attrs}{seen_join}{$rel} > 1 + ? join('_', $rel, $rs->{attrs}{seen_join}{$rel}) + : $rel); return $self->result_source->schema->resultset($rel_obj->{class} )->search( undef, { %{$rs->{attrs}}, - alias => $rel, + alias => $alias, select => undef(), as => undef() } )->search(@rest);