add_to returns inserted row in many_to_many relation
[dbsrgits/DBIx-Class.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;
6
7sub 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();
716c16a0 65 return $obj;
4b3ab474 66 };
67
b3f358b5 68 *{"${class}::${set_meth}"} = sub {
69 my $self = shift;
70 @_ > 0 or $self->throw_exception(
71 "{$set_meth} needs a list of objects or hashrefs"
72 );
f72f9361 73 my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
b3f358b5 74 $self->search_related($rel, {})->delete;
f72f9361 75 $self->$add_meth($_) for (@to_set);
b3f358b5 76 };
77
78 *{"${class}::${remove_meth}"} = sub {
303cf522 79 my $self = shift;
80 @_ > 0 && ref $_[0] ne 'HASH'
b3f358b5 81 or $self->throw_exception("${remove_meth} needs an object");
303cf522 82 my $obj = shift;
83 my $rel_source = $self->search_related($rel)->result_source;
84 my $cond = $rel_source->relationship_info($f_rel)->{cond};
85 my $link_cond = $rel_source->resolve_condition(
86 $cond, $obj, $f_rel
87 );
88 $self->search_related($rel, $link_cond)->delete;
4b3ab474 89 };
90
8973b6f1 91 }
92}
93
941;