1 package # hide from PAUSE
2 DBIx::Class::Relationship::ManyToMany;
8 use DBIx::Class::_Util qw( quote_sub perlstring );
10 # FIXME - this should go away
11 # instead Carp::Skip should export usable keywords or something like that
13 BEGIN { $unique_carper = \&carp_unique }
17 our %_pod_inherit_config =
19 class_map => { 'DBIx::Class::Relationship::ManyToMany' => 'DBIx::Class::Relationship' }
23 my ($class, $meth, $rel, $f_rel, $rel_attrs) = @_;
25 $class->throw_exception(
26 "missing relation in many-to-many"
29 $class->throw_exception(
30 "missing foreign relation in many-to-many"
33 my $add_meth = "add_to_${meth}";
34 my $remove_meth = "remove_from_${meth}";
35 my $set_meth = "set_${meth}";
36 my $rs_meth = "${meth}_rs";
38 for ($add_meth, $remove_meth, $set_meth, $rs_meth) {
39 if ( $class->can ($_) ) {
40 carp (<<"EOW") unless $ENV{DBIC_OVERWRITE_HELPER_METHODS_OK};
42 ***************************************************************************
43 The many-to-many relationship '$meth' is trying to create a utility method
45 This will completely overwrite one such already existing method on class
48 You almost certainly want to rename your method or the many-to-many
49 relationship, as the functionality of the original method will not be
52 To disable this warning set to a true value the environment variable
53 DBIC_OVERWRITE_HELPER_METHODS_OK
55 ***************************************************************************
60 my @main_meth_qsub_args = (
63 'DBIC_method_is_indirect_sugar',
64 ( keys( %{$rel_attrs||{}} )
65 ? 'DBIC_method_is_m2m_sugar_with_attrs'
66 : 'DBIC_method_is_m2m_sugar'
72 quote_sub "${class}::${meth}", sprintf( <<'EOC', $rs_meth ), @main_meth_qsub_args;
74 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call;
75 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
77 my $rs = shift->%s( @_ );
79 wantarray ? $rs->all : $rs;
83 my @extra_meth_qsub_args = (
85 '$rel_attrs' => \{ alias => $f_rel, %{ $rel_attrs||{} } },
86 '$carp_unique' => \$unique_carper,
89 'DBIC_method_is_indirect_sugar',
90 ( keys( %{$rel_attrs||{}} )
91 ? 'DBIC_method_is_m2m_extra_sugar_with_attrs'
92 : 'DBIC_method_is_m2m_extra_sugar'
98 quote_sub "${class}::${rs_meth}", sprintf( <<'EOC', map { perlstring $_ } ( "${class}::${meth}", $rel, $f_rel ) ), @extra_meth_qsub_args;
100 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS
102 # allow nested calls from our ->many_to_many, see comment below
103 ( (CORE::caller(1))[3] ne %s )
105 DBIx::Class::_Util::fail_on_internal_call;
107 # this little horror is there replicating a deprecation from
108 # within search_rs() itself
109 shift->related_resultset( %s )
110 ->related_resultset( %s )
113 ( @_ > 1 and ref $_[-1] eq 'HASH' )
114 ? { %%$rel_attrs, %%{ pop @_ } }
120 # the above is the only indirect method, the 3 below have too much logic
121 shift @{$extra_meth_qsub_args[1]{attributes}};
124 quote_sub "${class}::${add_meth}", sprintf( <<'EOC', $add_meth, $rel, $f_rel ), @extra_meth_qsub_args;
126 ( @_ >= 2 and @_ <= 3 ) or $_[0]->throw_exception(
127 "'%1$s' expects an object or hashref to link to, and an optional hashref of link data"
130 $_[0]->throw_exception(
131 "The optional link data supplied to '%1$s' is not a hashref (it was previously ignored)"
132 ) if $_[2] and ref $_[2] ne 'HASH';
134 my( $self, $far_obj ) = @_;
138 # the API is always expected to return the far object, possibly
139 # creating it in the process
140 if( not defined Scalar::Util::blessed( $far_obj ) ) {
142 $guard = $self->result_source->schema->storage->txn_scope_guard;
144 # reify the hash into an actual object
145 $far_obj = $self->result_source
146 ->related_source( q{%2$s} )
147 ->related_source( q{%3$s} )
149 ->search_rs( undef, $rel_attrs )
150 ->find_or_create( $far_obj );
153 my $link = $self->new_related(
158 $link->set_from_related( q{%3$s}, $far_obj );
162 $guard->commit if $guard;
168 quote_sub "${class}::${set_meth}", sprintf( <<'EOC', $set_meth, $add_meth, $rel, $f_rel ), @extra_meth_qsub_args;
172 my $set_to = ( ref $_[0] eq 'ARRAY' )
176 "Calling '%1$s' with a list of items to link to is deprecated, use an arrayref instead"
179 # gobble up everything from @_ into a new arrayref
184 # make sure folks are not invoking a bizarre mix of deprecated and curent syntax
185 $self->throw_exception(
186 "'%1$s' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
190 ( defined $_[0] and ref $_[0] ne 'HASH' )
195 # there will only be a single delete() op, unless we have what to set to
196 $guard = $self->result_source->schema->storage->txn_scope_guard
199 # if there is a where clause in the attributes, ensure we only delete
200 # rows that are within the where restriction
201 $self->related_resultset( q{%3$s} )
203 ( $rel_attrs->{where}
204 ? ( $rel_attrs->{where}, { join => q{%4$s} } )
209 # add in the set rel objects
212 @_, # at this point @_ is either empty or contains a lone link-data hash
215 $guard->commit if $guard;
219 # the last method needs no captures - just kill it all with fire
220 $extra_meth_qsub_args[0] = {};
223 quote_sub "${class}::${remove_meth}", sprintf( <<'EOC', $remove_meth, $rel, $f_rel ), @extra_meth_qsub_args;
225 $_[0]->throw_exception("'%1$s' expects an object")
226 unless defined Scalar::Util::blessed( $_[1] );
228 $_[0]->related_resultset( q{%2$s} )
229 ->search_rs( $_[1]->ident_condition( q{%3$s} ), { join => q{%3$s} } )