Add missing deprecation warnings on m2m set_*, tighten up code
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / ManyToMany.pm
index 37c8461..c5f4570 100644 (file)
@@ -84,7 +84,7 @@ EOW
         "${add_meth} needs an object or hashref"
       );
 
-      my $link = $self->search_related_rs($rel)->new_result(
+      my $link = $self->new_related( $rel,
         ( @_ > 1 && ref $_[-1] eq 'HASH' )
           ? pop
           : {}
@@ -100,6 +100,7 @@ EOW
       ;
 
       $link->set_from_related($f_rel, $far_obj);
+
       $link->insert();
 
       return $far_obj;
@@ -107,41 +108,61 @@ EOW
 
     my $set_meth_name = join '::', $class, $set_meth;
     *$set_meth_name = subname $set_meth_name, sub {
+
       my $self = shift;
-      @_ > 0 or $self->throw_exception(
-        "{$set_meth} needs a list of objects or hashrefs"
+
+      my $set_to = ( ref $_[0] eq 'ARRAY' )
+        ? ( shift @_ )
+        : do {
+          carp_unique(
+            "Calling '$set_meth' with a list of items to link to is deprecated, use an arrayref instead"
+          );
+
+          # gobble up everything from @_ into a new arrayref
+          [ splice @_ ]
+        }
+      ;
+
+      # make sure folks are not invoking a bizarre mix of deprecated and curent syntax
+      $self->throw_exception(
+        "'$set_meth' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
+      ) if (
+        @_ > 1
+          or
+        ( @_ and ref $_[0] ne 'HASH' )
       );
-      my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
+
+      my $guard = $self->result_source->schema->storage->txn_scope_guard;
+
       # if there is a where clause in the attributes, ensure we only delete
       # rows that are within the where restriction
+
       if ($rel_attrs && $rel_attrs->{where}) {
         $self->search_related( $rel, $rel_attrs->{where},{join => $f_rel})->delete;
       } else {
         $self->search_related( $rel, {} )->delete;
       }
       # add in the set rel objects
-      $self->$add_meth($_, ref($_[1]) ? $_[1] : {}) for (@to_set);
+      $self->$add_meth(
+        $_,
+        @_, # at this point @_ is either empty or contains a lone link-data hash
+      ) for @$set_to;
+
+      $guard->commit;
     };
 
     my $remove_meth_name = join '::', $class, $remove_meth;
     *$remove_meth_name = subname $remove_meth_name, sub {
       my ($self, $obj) = @_;
+
       $self->throw_exception("${remove_meth} needs an object")
         unless blessed ($obj);
-      my $rel_source = $self->search_related($rel)->result_source;
-      my $cond = $rel_source->relationship_info($f_rel)->{cond};
-      my ($link_cond, $crosstable) = $rel_source->_resolve_condition(
-        $cond, $obj, $f_rel, $f_rel
-      );
-
-      $self->throw_exception(
-        "Relationship '$rel' does not resolve to a join-free condition, "
-       ."unable to use with the ManyToMany helper '$f_rel'"
-      ) if $crosstable;
 
-      $self->search_related($rel, $link_cond)->delete;
+      $self->search_related_rs($rel)->search_rs(
+        $obj->ident_condition( $f_rel ),
+        { join => $f_rel },
+      )->delete;
     };
-
   }
 }