Make the many-to-many warning use warnings::register;
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Relationship / ManyToMany.pm
1 package # hide from PAUSE
2     DBIx::Class::Relationship::ManyToMany;
3
4 use strict;
5 use warnings;
6 use warnings::register;
7 use Sub::Name ();
8
9 sub many_to_many {
10   my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
11
12   $class->throw_exception(
13     "missing relation in many-to-many"
14   ) unless $rel;
15
16   $class->throw_exception(
17     "missing foreign relation in many-to-many"
18   ) unless $f_rel;
19
20   {
21     no strict 'refs';
22     no warnings 'redefine';
23
24     my $add_meth = "add_to_${meth}";
25     my $remove_meth = "remove_from_${meth}";
26     my $set_meth = "set_${meth}";
27     my $rs_meth = "${meth}_rs";
28
29     for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
30       warnings::warn(<<"EOW")
31 ***************************************************************************
32 The many-to-many relationship $meth is trying to create a utility method called
33 $_. This will overwrite the existing method on $class. You almost certainly
34 want to rename your method or the many-to-many relationship, as your method
35 will not be callable (it will use the one from the relationship instead.) 
36
37 no warnings 'DBIx::Class::Relationship::ManyToMany'; in 
38 $class to disable.
39 ***************************************************************************
40 EOW
41         if warnings::enabled() && $class->can($_);
42     }
43
44     $rel_attrs->{alias} ||= $f_rel;
45
46     my $rs_meth_name = join '::', $class, $rs_meth;
47     *$rs_meth_name = Sub::Name::subname $rs_meth_name, sub {
48       my $self = shift;
49       my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
50       my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
51       my $rs = $self->search_related($rel)->search_related(
52         $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
53       );
54           return $rs;
55     };
56
57     my $meth_name = join '::', $class, $meth;
58     *$meth_name = Sub::Name::subname $meth_name, sub {
59                 my $self = shift;
60                 my $rs = $self->$rs_meth( @_ );
61                 return (wantarray ? $rs->all : $rs);
62         };
63
64     my $add_meth_name = join '::', $class, $add_meth;
65     *$add_meth_name = Sub::Name::subname $add_meth_name, sub {
66       my $self = shift;
67       @_ > 0 or $self->throw_exception(
68         "${add_meth} needs an object or hashref"
69       );
70       my $source = $self->result_source;
71       my $schema = $source->schema;
72       my $rel_source_name = $source->relationship_info($rel)->{source};
73       my $rel_source = $schema->resultset($rel_source_name)->result_source;
74       my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
75       my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{});
76
77       my $obj;
78       if (ref $_[0]) {
79         if (ref $_[0] eq 'HASH') {
80           $obj = $f_rel_rs->create($_[0]);
81         } else {
82           $obj = $_[0];
83         }
84       } else {
85         $obj = $f_rel_rs->create({@_});
86       }
87
88       my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
89       my $link = $self->search_related($rel)->new_result($link_vals);
90       $link->set_from_related($f_rel, $obj);
91       $link->insert();
92           return $obj;
93     };
94
95     my $set_meth_name = join '::', $class, $set_meth;
96     *$set_meth_name = Sub::Name::subname $set_meth_name, sub {
97       my $self = shift;
98       @_ > 0 or $self->throw_exception(
99         "{$set_meth} needs a list of objects or hashrefs"
100       );
101       my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
102       $self->search_related($rel, {})->delete;
103       $self->$add_meth($_) for (@to_set);
104     };
105
106     my $remove_meth_name = join '::', $class, $remove_meth;
107     *$remove_meth_name = Sub::Name::subname $remove_meth_name, sub {
108       my $self = shift;
109       @_ > 0 && ref $_[0] ne 'HASH'
110         or $self->throw_exception("${remove_meth} needs an object");
111       my $obj = shift;
112       my $rel_source = $self->search_related($rel)->result_source;
113       my $cond = $rel_source->relationship_info($f_rel)->{cond};
114       my $link_cond = $rel_source->resolve_condition(
115         $cond, $obj, $f_rel
116       );
117       $self->search_related($rel, $link_cond)->delete;
118     };
119
120   }
121 }
122
123 1;