Improvements to copy() and set_from_related() code
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Relationship / Base.pm
index e4d7488..7e6542d 100644 (file)
@@ -51,7 +51,7 @@ command immediately before C<JOIN>.
 An arrayref containing a list of accessors in the foreign class to proxy in
 the main class. If, for example, you do the following:
   
-  __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => qw[/ margle /] });
+  __PACKAGE__->might_have(bar => 'Bar', undef, { proxy => [ qw/margle/ ] });
   
 Then, assuming Bar has an accessor named margle, you can do:
 
@@ -99,6 +99,22 @@ sub search_related {
   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 %{$_}) {
+        unless ($key =~ m/\./) {
+          $hash{"me.$key"} = $_->{$key};
+        } else {
+          $hash{$key} = $_->{$key};
+        }
+      }; \%hash; } @$cond ];
+  } else {
+    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}||[]});
@@ -180,15 +196,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;
 }