From: David Kamholz Date: Tue, 4 Jul 2006 01:45:01 +0000 (+0000) Subject: rename _resolve to _resolved_attrs; fix multiple join conditions in related_resultset... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=af4cd3e568e12a5060078abbae021c8259e5512d;p=dbsrgits%2FDBIx-Class-Historic.git rename _resolve to _resolved_attrs; fix multiple join conditions in related_resultset and add test --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 73f6aef..f4e7f96 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -350,12 +350,10 @@ sub find { # Run the query if (keys %$attrs) { my $rs = $self->search($query, $attrs); - $rs->_resolve_attr; - return keys %{$rs->{_attrs}{collapse}} ? $rs->next : $rs->single; + return keys %{$rs->_resolved_attrs->{collapse}} ? $rs->next : $rs->single; } else { - $self->_resolve_attr; - return keys %{$self->{_attrs}{collapse}} + return keys %{$self->_resolved_attrs->{collapse}} ? $self->search($query)->next : $self->single($query); } @@ -446,8 +444,7 @@ L for more information. sub cursor { my ($self) = @_; - $self->_resolve_attr; - my $attrs = { %{$self->{_attrs}} }; + my $attrs = { %{$self->_resolved_attrs} }; return $self->{cursor} ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select}, $attrs->{where},$attrs); @@ -476,8 +473,7 @@ method; if you need to add extra joins or similar call ->search and then sub single { my ($self, $where) = @_; - $self->_resolve_attr; - my $attrs = { %{$self->{_attrs}} }; + my $attrs = { %{$self->_resolved_attrs} }; if ($where) { if (defined $attrs->{where}) { $attrs->{where} = { @@ -693,9 +689,9 @@ sub next { return $self->_construct_object(@row); } -sub _resolve_attr { +sub _resolved_attrs { my $self = shift; - return if exists $self->{_attrs}; #return if _resolve_attr has already been called + return $self->{_attrs} if $self->{_attrs}; my $attrs = $self->{attrs}; my $source = $self->{_parent_rs} || $self->{result_source}; @@ -785,7 +781,8 @@ sub _resolve_attr { push(@{$attrs->{order_by}}, @pre_order); } $attrs->{collapse} = $collapse; - $self->{_attrs} = $attrs; + + return $self->{_attrs} = $attrs; } sub _merge_attr { @@ -997,8 +994,7 @@ sub _count { # Separated out so pager can get the full count my $self = shift; my $select = { count => '*' }; - $self->_resolve_attr; - my $attrs = { %{ $self->{_attrs} } }; + my $attrs = { %{$self->_resolved_attrs} }; if (my $group_by = delete $attrs->{group_by}) { delete $attrs->{having}; my @distinct = (ref $group_by ? @$group_by : ($group_by)); @@ -1069,8 +1065,7 @@ sub all { my @obj; # TODO: don't call resolve here - $self->_resolve_attr; - if (keys %{$self->{_attrs}{collapse}}) { + if (keys %{$self->_resolved_attrs->{collapse}}) { # if ($self->{attrs}{prefetch}) { # Using $self->cursor->all is really just an optimisation. # If we're collapsing has_many prefetches it probably makes @@ -1609,6 +1604,10 @@ sub related_resultset { "search_related: result source '" . $self->result_source->name . "' has no such relationship $rel") unless $rel_obj; + + my $alias = $self->_resolved_attrs->{seen_join}{$rel} + ? join('_', $rel, $self->_resolved_attrs->{seen_join}{$rel}+1) + : $rel; my @live_join_stack = (@{$self->{attrs}{_live_join_stack}||[]}, $rel); @@ -1616,8 +1615,8 @@ sub related_resultset { undef, { select => undef, as => undef, - alias => $rel, #the most recent - _live_join_stack => \@live_join_stack, #the trail of rels + alias => $alias, + _live_join_stack => \@live_join_stack, _parent_attrs => $self->{attrs}} ); diff --git a/t/76joins.t b/t/76joins.t index 0fc9f53..6001598 100644 --- a/t/76joins.t +++ b/t/76joins.t @@ -13,7 +13,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 43 ); + : ( tests => 44 ); } # figure out if we've got a version of sqlite that is older than 3.2.6, in @@ -295,3 +295,6 @@ $schema->storage->debug(0); cmp_ok($queries, '==', 1, 'Only one query run'); +$tree_like = $schema->resultset('TreeLike')->find(1); +$tree_like = $tree_like->search_related('children')->search_related('children')->search_related('children')->first; +is($tree_like->name, 'quux', 'Tree search_related ok'); \ No newline at end of file diff --git a/t/lib/DBICTest/Schema/TreeLike.pm b/t/lib/DBICTest/Schema/TreeLike.pm index 63bb975..297cfc6 100644 --- a/t/lib/DBICTest/Schema/TreeLike.pm +++ b/t/lib/DBICTest/Schema/TreeLike.pm @@ -14,5 +14,6 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key(qw/id/); __PACKAGE__->belongs_to('parent', 'TreeLike', { 'foreign.id' => 'self.parent' }); +__PACKAGE__->has_many('children', 'TreeLike', { 'foreign.parent' => 'self.id' }); 1;