Commit | Line | Data |
75d07914 |
1 | package # hide from PAUSE |
c0e7b4e5 |
2 | DBIx::Class::Relationship::ManyToMany; |
8973b6f1 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | sub many_to_many { |
78af1010 |
8 | my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_; |
d0ed3b55 |
9 | |
10 | $class->throw_exception( |
11 | "missing relation in many-to-many" |
12 | ) unless $rel; |
13 | |
14 | $class->throw_exception( |
15 | "missing foreign relation in many-to-many" |
16 | ) unless $f_rel; |
17 | |
8973b6f1 |
18 | { |
19 | no strict 'refs'; |
20 | no warnings 'redefine'; |
4b3ab474 |
21 | |
b3f358b5 |
22 | my $add_meth = "add_to_${meth}"; |
23 | my $remove_meth = "remove_from_${meth}"; |
24 | my $set_meth = "set_${meth}"; |
18788bf2 |
25 | my $rs_meth = "${meth}_rs"; |
303cf522 |
26 | |
7141bdfc |
27 | $rel_attrs->{alias} ||= $f_rel; |
28 | |
18788bf2 |
29 | *{"${class}::${meth}_rs"} = sub { |
0f6ac8bb |
30 | my $self = shift; |
31 | my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; |
78060df8 |
32 | my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }); |
18788bf2 |
33 | my $rs = $self->search_related($rel)->search_related( |
3a868fb2 |
34 | $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs } |
35 | ); |
18788bf2 |
36 | return $rs; |
78af1010 |
37 | }; |
4b3ab474 |
38 | |
18788bf2 |
39 | *{"${class}::${meth}"} = sub { |
40 | my $self = shift; |
41 | my $rs = $self->$rs_meth( @_ ); |
42 | return (wantarray ? $rs->all : $rs); |
43 | }; |
44 | |
b3f358b5 |
45 | *{"${class}::${add_meth}"} = sub { |
303cf522 |
46 | my $self = shift; |
47 | @_ > 0 or $self->throw_exception( |
b3f358b5 |
48 | "${add_meth} needs an object or hashref" |
303cf522 |
49 | ); |
50 | my $source = $self->result_source; |
51 | my $schema = $source->schema; |
52 | my $rel_source_name = $source->relationship_info($rel)->{source}; |
53 | my $rel_source = $schema->resultset($rel_source_name)->result_source; |
54 | my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source}; |
78060df8 |
55 | my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{}); |
56 | |
57 | my $obj; |
58 | if (ref $_[0]) { |
59 | if (ref $_[0] eq 'HASH') { |
60 | $obj = $f_rel_rs->create($_[0]); |
61 | } else { |
62 | $obj = $_[0]; |
63 | } |
64 | } else { |
65 | $obj = $f_rel_rs->create({@_}); |
66 | } |
67 | |
3bd6e3e0 |
68 | my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; |
6cc5b382 |
69 | my $link = $self->search_related($rel)->new_result($link_vals); |
303cf522 |
70 | $link->set_from_related($f_rel, $obj); |
71 | $link->insert(); |
716c16a0 |
72 | return $obj; |
4b3ab474 |
73 | }; |
74 | |
b3f358b5 |
75 | *{"${class}::${set_meth}"} = sub { |
76 | my $self = shift; |
77 | @_ > 0 or $self->throw_exception( |
78 | "{$set_meth} needs a list of objects or hashrefs" |
79 | ); |
f72f9361 |
80 | my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_); |
b3f358b5 |
81 | $self->search_related($rel, {})->delete; |
f72f9361 |
82 | $self->$add_meth($_) for (@to_set); |
b3f358b5 |
83 | }; |
84 | |
85 | *{"${class}::${remove_meth}"} = sub { |
303cf522 |
86 | my $self = shift; |
87 | @_ > 0 && ref $_[0] ne 'HASH' |
b3f358b5 |
88 | or $self->throw_exception("${remove_meth} needs an object"); |
303cf522 |
89 | my $obj = shift; |
90 | my $rel_source = $self->search_related($rel)->result_source; |
91 | my $cond = $rel_source->relationship_info($f_rel)->{cond}; |
92 | my $link_cond = $rel_source->resolve_condition( |
93 | $cond, $obj, $f_rel |
94 | ); |
95 | $self->search_related($rel, $link_cond)->delete; |
4b3ab474 |
96 | }; |
97 | |
8973b6f1 |
98 | } |
99 | } |
100 | |
101 | 1; |