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