Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::Relationship::ManyToMany; |
8973b6f1 |
3 | |
4 | use strict; |
5 | use warnings; |
b234e9d9 |
6 | |
70c28808 |
7 | use DBIx::Class::Carp; |
a9da9b6a |
8 | use Sub::Name 'subname'; |
9 | use Scalar::Util 'blessed'; |
10 | use DBIx::Class::_Util 'fail_on_internal_wantarray'; |
8f4b5c08 |
11 | use namespace::clean; |
12 | |
13 | our %_pod_inherit_config = |
044e70c7 |
14 | ( |
15 | class_map => { 'DBIx::Class::Relationship::ManyToMany' => 'DBIx::Class::Relationship' } |
16 | ); |
17 | |
8973b6f1 |
18 | sub many_to_many { |
78af1010 |
19 | my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_; |
d0ed3b55 |
20 | |
21 | $class->throw_exception( |
22 | "missing relation in many-to-many" |
23 | ) unless $rel; |
24 | |
25 | $class->throw_exception( |
26 | "missing foreign relation in many-to-many" |
27 | ) unless $f_rel; |
28 | |
8973b6f1 |
29 | { |
30 | no strict 'refs'; |
31 | no warnings 'redefine'; |
4b3ab474 |
32 | |
b3f358b5 |
33 | my $add_meth = "add_to_${meth}"; |
34 | my $remove_meth = "remove_from_${meth}"; |
35 | my $set_meth = "set_${meth}"; |
18788bf2 |
36 | my $rs_meth = "${meth}_rs"; |
303cf522 |
37 | |
35210a5d |
38 | for ($add_meth, $remove_meth, $set_meth, $rs_meth) { |
d81b2771 |
39 | if ( $class->can ($_) ) { |
b7d1831a |
40 | carp (<<"EOW") unless $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK}; |
b234e9d9 |
41 | |
35678f0b |
42 | *************************************************************************** |
b234e9d9 |
43 | The many-to-many relationship '$meth' is trying to create a utility method |
44 | called $_. |
45 | This will completely overwrite one such already existing method on class |
46 | $class. |
d81b2771 |
47 | |
b234e9d9 |
48 | You almost certainly want to rename your method or the many-to-many |
49 | relationship, as the functionality of the original method will not be |
50 | accessible anymore. |
d81b2771 |
51 | |
b7d1831a |
52 | To disable this warning set to a true value the environment variable |
53 | DBIC_OVERWRITE_HELPER_METHODS_OK |
35678f0b |
54 | |
35678f0b |
55 | *************************************************************************** |
56 | EOW |
d81b2771 |
57 | } |
35210a5d |
58 | } |
59 | |
7141bdfc |
60 | $rel_attrs->{alias} ||= $f_rel; |
61 | |
ddc0a6c8 |
62 | my $rs_meth_name = join '::', $class, $rs_meth; |
8f4b5c08 |
63 | *$rs_meth_name = subname $rs_meth_name, sub { |
0f6ac8bb |
64 | my $self = shift; |
65 | my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; |
18788bf2 |
66 | my $rs = $self->search_related($rel)->search_related( |
3a868fb2 |
67 | $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs } |
68 | ); |
d7a58a29 |
69 | return $rs; |
78af1010 |
70 | }; |
4b3ab474 |
71 | |
ddc0a6c8 |
72 | my $meth_name = join '::', $class, $meth; |
8f4b5c08 |
73 | *$meth_name = subname $meth_name, sub { |
e89c7968 |
74 | DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray; |
d7a58a29 |
75 | my $self = shift; |
76 | my $rs = $self->$rs_meth( @_ ); |
77 | return (wantarray ? $rs->all : $rs); |
78 | }; |
18788bf2 |
79 | |
ddc0a6c8 |
80 | my $add_meth_name = join '::', $class, $add_meth; |
8f4b5c08 |
81 | *$add_meth_name = subname $add_meth_name, sub { |
303cf522 |
82 | my $self = shift; |
86ed11c9 |
83 | @_ or $self->throw_exception( |
b3f358b5 |
84 | "${add_meth} needs an object or hashref" |
303cf522 |
85 | ); |
78060df8 |
86 | |
12d4ace4 |
87 | my $link = $self->new_related( $rel, |
86ed11c9 |
88 | ( @_ > 1 && ref $_[-1] eq 'HASH' ) |
89 | ? pop |
90 | : {} |
91 | ); |
92 | |
93 | my $far_obj = defined blessed $_[0] |
94 | ? $_[0] |
95 | : $self->result_source |
96 | ->related_source( $rel ) |
97 | ->related_source( $f_rel ) |
98 | ->resultset->search_rs( {}, $rel_attrs||{} ) |
99 | ->find_or_create( ref $_[0] eq 'HASH' ? $_[0] : {@_} ) |
100 | ; |
101 | |
102 | $link->set_from_related($f_rel, $far_obj); |
12d4ace4 |
103 | |
303cf522 |
104 | $link->insert(); |
86ed11c9 |
105 | |
106 | return $far_obj; |
4b3ab474 |
107 | }; |
108 | |
ddc0a6c8 |
109 | my $set_meth_name = join '::', $class, $set_meth; |
8f4b5c08 |
110 | *$set_meth_name = subname $set_meth_name, sub { |
8a67d9cf |
111 | |
b3f358b5 |
112 | my $self = shift; |
8a67d9cf |
113 | |
114 | my $set_to = ( ref $_[0] eq 'ARRAY' ) |
115 | ? ( shift @_ ) |
116 | : do { |
117 | carp_unique( |
118 | "Calling '$set_meth' with a list of items to link to is deprecated, use an arrayref instead" |
119 | ); |
120 | |
121 | # gobble up everything from @_ into a new arrayref |
122 | [ splice @_ ] |
123 | } |
124 | ; |
125 | |
126 | # make sure folks are not invoking a bizarre mix of deprecated and curent syntax |
127 | $self->throw_exception( |
128 | "'$set_meth' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data" |
129 | ) if ( |
130 | @_ > 1 |
131 | or |
132 | ( @_ and ref $_[0] ne 'HASH' ) |
b3f358b5 |
133 | ); |
12d4ace4 |
134 | |
135 | my $guard = $self->result_source->schema->storage->txn_scope_guard; |
136 | |
f1f9ee17 |
137 | # if there is a where clause in the attributes, ensure we only delete |
138 | # rows that are within the where restriction |
12d4ace4 |
139 | |
f1f9ee17 |
140 | if ($rel_attrs && $rel_attrs->{where}) { |
141 | $self->search_related( $rel, $rel_attrs->{where},{join => $f_rel})->delete; |
142 | } else { |
143 | $self->search_related( $rel, {} )->delete; |
144 | } |
145 | # add in the set rel objects |
8a67d9cf |
146 | $self->$add_meth( |
147 | $_, |
148 | @_, # at this point @_ is either empty or contains a lone link-data hash |
149 | ) for @$set_to; |
12d4ace4 |
150 | |
151 | $guard->commit; |
b3f358b5 |
152 | }; |
153 | |
ddc0a6c8 |
154 | my $remove_meth_name = join '::', $class, $remove_meth; |
8f4b5c08 |
155 | *$remove_meth_name = subname $remove_meth_name, sub { |
156 | my ($self, $obj) = @_; |
12d4ace4 |
157 | |
8f4b5c08 |
158 | $self->throw_exception("${remove_meth} needs an object") |
159 | unless blessed ($obj); |
aa56106b |
160 | |
12d4ace4 |
161 | $self->search_related_rs($rel)->search_rs( |
162 | $obj->ident_condition( $f_rel ), |
163 | { join => $f_rel }, |
164 | )->delete; |
4b3ab474 |
165 | }; |
8973b6f1 |
166 | } |
167 | } |
168 | |
169 | 1; |