Initial work on helper methods for many_to_many relationships.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / ManyToMany.pm
index 0ecc915..8818a69 100644 (file)
@@ -1,20 +1,42 @@
-package DBIx::Class::Relationship::ManyToMany;
+package # hide from PAUSE
+    DBIx::Class::Relationship::ManyToMany;
 
 use strict;
 use warnings;
 
 sub many_to_many {
   my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
-  $rel_attrs ||= {};
-  
   {
     no strict 'refs';
     no warnings 'redefine';
+
     *{"${class}::${meth}"} = sub {
       my $self = shift;
       my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
-      $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %$rel_attrs, %$attrs });
+      $self->search_related($rel)->search_related($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
+    };
+
+    *{"${class}::add_to_${meth}"} = sub {
+      my( $self, $obj ) = @_;
+      my $vals = @_ > 2 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
+      return $self->search_related($rel)->create({
+        map { $_=>$self->get_column($_) } $self->primary_columns(),
+        map { $_=>$obj->get_column($_) } $obj->primary_columns(),
+        %$vals,
+      });
     };
+
+    *{"${class}::remove_from_${meth}"} = sub {
+      my( $self, $obj ) = @_;
+      return $self->search_related(
+        $rel,
+        {
+            map { $_=>$self->get_column($_) } $self->primary_columns(),
+            map { $_=>$obj->get_column($_) } $obj->primary_columns(),
+        },
+      )->delete();
+    };
+
   }
 }