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}"; |
303cf522 |
25 | |
7141bdfc |
26 | $rel_attrs->{alias} ||= $f_rel; |
27 | |
78af1010 |
28 | *{"${class}::${meth}"} = sub { |
0f6ac8bb |
29 | my $self = shift; |
30 | my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; |
78060df8 |
31 | my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }); |
3a868fb2 |
32 | $self->search_related($rel)->search_related( |
33 | $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs } |
34 | ); |
78af1010 |
35 | }; |
4b3ab474 |
36 | |
b3f358b5 |
37 | *{"${class}::${add_meth}"} = sub { |
303cf522 |
38 | my $self = shift; |
39 | @_ > 0 or $self->throw_exception( |
b3f358b5 |
40 | "${add_meth} needs an object or hashref" |
303cf522 |
41 | ); |
42 | my $source = $self->result_source; |
43 | my $schema = $source->schema; |
44 | my $rel_source_name = $source->relationship_info($rel)->{source}; |
45 | my $rel_source = $schema->resultset($rel_source_name)->result_source; |
46 | my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source}; |
78060df8 |
47 | my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{}); |
48 | |
49 | my $obj; |
50 | if (ref $_[0]) { |
51 | if (ref $_[0] eq 'HASH') { |
52 | $obj = $f_rel_rs->create($_[0]); |
53 | } else { |
54 | $obj = $_[0]; |
55 | } |
56 | } else { |
57 | $obj = $f_rel_rs->create({@_}); |
58 | } |
59 | |
3bd6e3e0 |
60 | my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {}; |
303cf522 |
61 | my $link = $self->search_related($rel)->new_result({}); |
62 | $link->set_from_related($f_rel, $obj); |
3bd6e3e0 |
63 | $link->set_columns($link_vals); |
303cf522 |
64 | $link->insert(); |
4b3ab474 |
65 | }; |
66 | |
b3f358b5 |
67 | *{"${class}::${set_meth}"} = sub { |
68 | my $self = shift; |
69 | @_ > 0 or $self->throw_exception( |
70 | "{$set_meth} needs a list of objects or hashrefs" |
71 | ); |
f72f9361 |
72 | my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_); |
b3f358b5 |
73 | $self->search_related($rel, {})->delete; |
f72f9361 |
74 | $self->$add_meth($_) for (@to_set); |
b3f358b5 |
75 | }; |
76 | |
77 | *{"${class}::${remove_meth}"} = sub { |
303cf522 |
78 | my $self = shift; |
79 | @_ > 0 && ref $_[0] ne 'HASH' |
b3f358b5 |
80 | or $self->throw_exception("${remove_meth} needs an object"); |
303cf522 |
81 | my $obj = shift; |
82 | my $rel_source = $self->search_related($rel)->result_source; |
83 | my $cond = $rel_source->relationship_info($f_rel)->{cond}; |
84 | my $link_cond = $rel_source->resolve_condition( |
85 | $cond, $obj, $f_rel |
86 | ); |
87 | $self->search_related($rel, $link_cond)->delete; |
4b3ab474 |
88 | }; |
89 | |
8973b6f1 |
90 | } |
91 | } |
92 | |
93 | 1; |