From: Aran Deltac Date: Fri, 12 May 2006 20:58:07 +0000 (+0000) Subject: Initial work on helper methods for many_to_many relationships. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4b3ab474aa3b3e4ccae0843d58e8eaec29a4198d;p=dbsrgits%2FDBIx-Class-Historic.git Initial work on helper methods for many_to_many relationships. --- diff --git a/lib/DBIx/Class/Relationship/ManyToMany.pm b/lib/DBIx/Class/Relationship/ManyToMany.pm index 387fc0b..8818a69 100644 --- a/lib/DBIx/Class/Relationship/ManyToMany.pm +++ b/lib/DBIx/Class/Relationship/ManyToMany.pm @@ -9,11 +9,34 @@ sub many_to_many { { 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 }); }; + + *{"${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(); + }; + } }