(no commit message)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / Base.pm
index e3f4258..c838d69 100644 (file)
@@ -69,7 +69,9 @@ created, which calls C<create_related> for the relationship.
 
 =back
 
-=head2 register_relationship($relname, $rel_info)
+=head2 register_relationship
+
+=head3 Arguments: ($relname, $rel_info)
 
 Registers a relationship on the class
 
@@ -84,31 +86,7 @@ sub register_relationship { }
 =cut
 
 sub search_related {
-  my $self = shift;
-  die "Can't call *_related as class methods" unless ref $self;
-  my $rel = shift;
-  my $attrs = { };
-  if (@_ > 1 && ref $_[$#_] eq 'HASH') {
-    $attrs = { %{ pop(@_) } };
-  }
-  my $rel_obj = $self->relationship_info($rel);
-  $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
-  $attrs = { %{$rel_obj->{attrs} || {}}, %{$attrs || {}} };
-
-  $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
-  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}||[]});
-  return $self->result_source->related_source($rel
-           )->resultset->search($query, $attrs);
+  return shift->related_resultset(shift)->search(@_);
 }
 
 =head2 count_related
@@ -131,7 +109,9 @@ sub count_related {
 sub create_related {
   my $self = shift;
   my $rel = shift;
-  return $self->search_related($rel)->create(@_);
+  my $obj = $self->search_related($rel)->create(@_);
+  delete $self->{related_resultsets}->{$rel};
+  return $obj;
 }
 
 =head2 new_related
@@ -185,15 +165,9 @@ sub set_from_related {
   my $f_class = $self->result_source->schema->class($rel_obj->{class});
   $self->throw_exception( "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_exception("set_from_related can't handle $key as key")
-      unless $key =~ m/^foreign\.([^\.]+)$/;
-    my $val = $f_obj->get_column($1);
-    $self->throw_exception("set_from_related can't handle ".$cond->{$key}." as value")
-      unless $cond->{$key} =~ m/^self\.([^\.]+)$/;
-    $self->set_column($1 => $val);
-  }
+  $self->set_columns(
+    $self->result_source->resolve_condition(
+       $rel_obj->{cond}, $f_obj, $rel));
   return 1;
 }
 
@@ -217,11 +191,52 @@ sub update_from_related {
 
 sub delete_related {
   my $self = shift;
-  return $self->search_related(@_)->delete;
+  my $obj = $self->search_related(@_)->delete;
+  delete $self->{related_resultsets}->{$_[0]};
+  return $obj;
 }
 
 1;
 
+=head2 related_resultset($name)
+
+Returns a L<DBIx::Class::ResultSet> for the relationship named $name.
+
+  $rs = $obj->related_resultset('related_table');
+
+=cut
+
+sub related_resultset {
+  my $self = shift;
+  $self->throw_exception("Can't call *_related as class methods") unless ref $self;
+  my $rel = shift;
+  my $rel_obj = $self->relationship_info($rel);
+  $self->throw_exception( "No such relationship ${rel}" ) unless $rel_obj;
+  
+  return $self->{related_resultsets}{$rel} ||= do {
+    my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
+    $attrs = { %{$rel_obj->{attrs} || {}}, %$attrs };
+
+    $self->throw_exception( "Invalid query: @_" ) if (@_ > 1 && (@_ % 2 == 1));
+    my $query = ((@_ > 1) ? {@_} : shift);
+
+    my $cond = $self->result_source->resolve_condition($rel_obj->{cond}, $rel, $self);
+    if (ref $cond eq 'ARRAY') {
+      $cond = [ map { my $hash;
+        foreach my $key (keys %$_) {
+          my $newkey = $key =~ /\./ ? "me.$key" : $key;
+          $hash->{$newkey} = $_->{$key};
+        }; $hash } @$cond ];
+    } else {
+      foreach my $key (grep { ! /\./ } keys %$cond) {
+        $cond->{"me.$key"} = delete $cond->{$key};
+      }
+    }
+    $query = ($query ? { '-and' => [ $cond, $query ] } : $cond);
+    $self->result_source->related_source($rel)->resultset->search($query, $attrs);
+  };
+}
+
 =head1 AUTHORS
 
 Matt S. Trout <mst@shadowcatsystems.co.uk>