Fixed search/count_related to use Abstract (though not for _convert yet)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship.pm
index 87f8f11..10807dd 100644 (file)
@@ -104,6 +104,17 @@ sub _cond_value {
 
 sub search_related {
   my $self = shift;
+  return $self->_query_related('search', @_);
+}
+
+sub count_related {
+  my $self = shift;
+  return $self->_query_related('count', @_);
+}
+
+sub _query_related {
+  my $self = shift;
+  my $meth = shift;
   my $rel = shift;
   my $attrs = { };
   if (@_ > 1 && ref $_[$#_] eq 'HASH') {
@@ -112,17 +123,19 @@ sub search_related {
   my $rel_obj = $self->_relationships->{$rel};
   $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
   $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
-  my $s_cond;
-  if (@_) {
-    $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
-    my $query = ((@_ > 1) ? {@_} : shift);
-    $s_cond = $self->_cond_resolve($query, $attrs);
-  }
-  $attrs->{_action} = 'convert';
+
+  $self->throw( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
+  my $query = ((@_ > 1) ? {@_} : shift);
+
+  $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something
+                                 # to merge into the AST really?
   my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs);
-  $cond = "${s_cond} AND ${cond}" if $s_cond;
-  return $rel_obj->{class}->retrieve_from_sql($cond, @{$attrs->{bind} || []},
-                                                $attrs);
+  $query = ($query ? { '-and' => [ \$cond, $query ] } : \$cond);
+  #use Data::Dumper; warn Dumper($query);
+  #warn $rel_obj->{class}." $meth $cond ".join(', ', @{$attrs->{bind}});
+  delete $attrs->{_action};
+  return $self->resolve_class($rel_obj->{class}
+           )->$meth($query, $attrs);
 }
 
 sub create_related {
@@ -146,7 +159,7 @@ sub new_related {
     $self->_cond_value($attrs, $k => $v);
     $fields{$self->_cond_key($attrs, $k)} = (@{delete $attrs->{bind}})[0];
   }
-  return $rel_obj->{class}->new(\%fields);
+  return $self->resolve_class($rel_obj->{class})->new(\%fields);
 }
 
 sub find_or_create_related {
@@ -162,8 +175,9 @@ sub set_from_related {
   $self->throw( "set_from_related can only handle a hash condition; the "
     ."condition for $rel is of type ".(ref $cond ? ref $cond : 'plain scalar'))
       unless ref $cond eq 'HASH';
-  $self->throw( "Object $f_obj isn't a ".$rel_obj->{class} )
-    unless $f_obj->isa($rel_obj->{class});
+  my $f_class = $self->resolve_class($rel_obj->{class});
+  $self->throw( "Object $f_obj isn't a ".$f_class )
+    unless $f_obj->isa($f_class);
   foreach my $key (keys %$cond) {
     next if ref $cond->{$key}; # Skip literals and complex conditions
     $self->throw("set_from_related can't handle $key as key")
@@ -182,22 +196,6 @@ sub update_from_related {
   $self->update;
 }
 
-sub count_related {
-  my $self = shift;
-  my $rel = shift;
-  my $rel_obj = $self->_relationships->{$rel};
-  $self->throw( "No such relationship ${rel}" ) unless $rel_obj;
-  my $cond = $rel_obj->{cond};
-  my $count_cond = {};
-  foreach my $key (keys %$cond) {
-    $key =~ m/^foreign\.([^\.]+)$/;
-    my $count_key = $1;
-    $cond->{$key} =~ m/^self\.([^\.]+)$/;
-    $count_cond->{$count_key} = $self->get_column($1);
-  }
-  return $rel_obj->{class}->count( $count_cond );
-}
-
 1;
 
 =back