reverted previous deploy change(r8255) - sorry matt, i wont break it again
[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) = @_;
8973b6f1 9 {
10 no strict 'refs';
11 no warnings 'redefine';
4b3ab474 12
b3f358b5 13 my $add_meth = "add_to_${meth}";
14 my $remove_meth = "remove_from_${meth}";
15 my $set_meth = "set_${meth}";
303cf522 16
7141bdfc 17 $rel_attrs->{alias} ||= $f_rel;
18
78af1010 19 *{"${class}::${meth}"} = sub {
0f6ac8bb 20 my $self = shift;
21 my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
78060df8 22 my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
3a868fb2 23 $self->search_related($rel)->search_related(
24 $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
25 );
78af1010 26 };
4b3ab474 27
b3f358b5 28 *{"${class}::${add_meth}"} = sub {
303cf522 29 my $self = shift;
30 @_ > 0 or $self->throw_exception(
b3f358b5 31 "${add_meth} needs an object or hashref"
303cf522 32 );
33 my $source = $self->result_source;
34 my $schema = $source->schema;
35 my $rel_source_name = $source->relationship_info($rel)->{source};
36 my $rel_source = $schema->resultset($rel_source_name)->result_source;
37 my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
78060df8 38 my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{});
39
40 my $obj;
41 if (ref $_[0]) {
42 if (ref $_[0] eq 'HASH') {
43 $obj = $f_rel_rs->create($_[0]);
44 } else {
45 $obj = $_[0];
46 }
47 } else {
48 $obj = $f_rel_rs->create({@_});
49 }
50
3bd6e3e0 51 my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
303cf522 52 my $link = $self->search_related($rel)->new_result({});
53 $link->set_from_related($f_rel, $obj);
3bd6e3e0 54 $link->set_columns($link_vals);
303cf522 55 $link->insert();
4b3ab474 56 };
57
b3f358b5 58 *{"${class}::${set_meth}"} = sub {
59 my $self = shift;
60 @_ > 0 or $self->throw_exception(
61 "{$set_meth} needs a list of objects or hashrefs"
62 );
f72f9361 63 my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_);
b3f358b5 64 $self->search_related($rel, {})->delete;
f72f9361 65 $self->$add_meth($_) for (@to_set);
b3f358b5 66 };
67
68 *{"${class}::${remove_meth}"} = sub {
303cf522 69 my $self = shift;
70 @_ > 0 && ref $_[0] ne 'HASH'
b3f358b5 71 or $self->throw_exception("${remove_meth} needs an object");
303cf522 72 my $obj = shift;
73 my $rel_source = $self->search_related($rel)->result_source;
74 my $cond = $rel_source->relationship_info($f_rel)->{cond};
75 my $link_cond = $rel_source->resolve_condition(
76 $cond, $obj, $f_rel
77 );
78 $self->search_related($rel, $link_cond)->delete;
4b3ab474 79 };
80
8973b6f1 81 }
82}
83
841;