1 package # hide from PAUSE
2 DBIx::Class::Relationship::ManyToMany;
8 my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
11 no warnings 'redefine';
13 my $add_meth = "add_to_${meth}";
14 my $remove_meth = "remove_from_${meth}";
15 my $set_meth = "set_${meth}";
17 *{"${class}::${meth}"} = sub {
19 my $attrs = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
20 my @args = ($f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs });
21 $self->search_related($rel)->search_related(
22 $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
26 *{"${class}::${add_meth}"} = sub {
28 @_ > 0 or $self->throw_exception(
29 "${add_meth} needs an object or hashref"
31 my $source = $self->result_source;
32 my $schema = $source->schema;
33 my $rel_source_name = $source->relationship_info($rel)->{source};
34 my $rel_source = $schema->resultset($rel_source_name)->result_source;
35 my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
36 my $f_rel_rs = $schema->resultset($f_rel_source_name)->search({}, $rel_attrs||{});
40 if (ref $_[0] eq 'HASH') {
41 $obj = $f_rel_rs->create($_[0]);
46 $obj = $f_rel_rs->create({@_});
49 my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
50 my $link = $self->search_related($rel)->new_result({});
51 $link->set_from_related($f_rel, $obj);
52 $link->set_columns($link_vals);
56 *{"${class}::${set_meth}"} = sub {
58 @_ > 0 or $self->throw_exception(
59 "{$set_meth} needs a list of objects or hashrefs"
61 $self->search_related($rel, {})->delete;
62 $self->$add_meth(shift) while (defined $_[0]);
65 *{"${class}::${remove_meth}"} = sub {
67 @_ > 0 && ref $_[0] ne 'HASH'
68 or $self->throw_exception("${remove_meth} needs an object");
70 my $rel_source = $self->search_related($rel)->result_source;
71 my $cond = $rel_source->relationship_info($f_rel)->{cond};
72 my $link_cond = $rel_source->resolve_condition(
75 $self->search_related($rel, $link_cond)->delete;