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 $self->search_related($rel)->search_related(
21 $f_rel, @_ > 0 ? @_ : undef, { %{$rel_attrs||{}}, %$attrs }
25 *{"${class}::${add_meth}"} = sub {
27 @_ > 0 or $self->throw_exception(
28 "${add_meth} needs an object or hashref"
30 my $source = $self->result_source;
31 my $schema = $source->schema;
32 my $rel_source_name = $source->relationship_info($rel)->{source};
33 my $rel_source = $schema->resultset($rel_source_name)->result_source;
34 my $f_rel_source_name = $rel_source->relationship_info($f_rel)->{source};
35 my $f_rel_rs = $schema->resultset($f_rel_source_name);
37 ? ( ref $_[0] eq 'HASH' ? $f_rel_rs->create($_[0]) : $_[0] )
38 : ( $f_rel_rs->create({@_}) );
39 my $link_vals = @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {};
40 my $link = $self->search_related($rel)->new_result({});
41 $link->set_from_related($f_rel, $obj);
42 $link->set_columns($link_vals);
46 *{"${class}::${set_meth}"} = sub {
48 @_ > 0 or $self->throw_exception(
49 "{$set_meth} needs a list of objects or hashrefs"
51 $self->search_related($rel, {})->delete;
52 $self->$add_meth(shift) while (defined $_[0]);
55 *{"${class}::${remove_meth}"} = sub {
57 @_ > 0 && ref $_[0] ne 'HASH'
58 or $self->throw_exception("${remove_meth} needs an object");
60 my $rel_source = $self->search_related($rel)->result_source;
61 my $cond = $rel_source->relationship_info($f_rel)->{cond};
62 my $link_cond = $rel_source->resolve_condition(
65 $self->search_related($rel, $link_cond)->delete;