1 package FS::m2m_Common;
4 use vars qw( @ISA $DEBUG );
5 use FS::Schema qw( dbdef );
6 use FS::Record qw( qsearch qsearchs dbh );
8 #hmm. well. we seem to be used as a mixin.
9 #@ISA = qw( FS::Record );
15 FS::m2m_Common - Mixin class for classes in a many-to-many relationship
21 @ISA = qw( FS::m2m_Common FS::Record );
25 FS::m2m_Common is intended as a mixin class for classes which have a
26 many-to-many relationship with another table (via a linking table).
28 It is currently assumed that the link table contains two fields named the same
29 as the primary keys of the base and target tables, but you can ovverride this
30 assumption if your table is different.
36 =item process_m2m OPTION => VALUE, ...
42 =item link_table (required)
44 =item target_table (required)
46 =item params (required)
48 hashref; keys are primary key values in target_table (values are boolean). For convenience, keys may optionally be prefixed with the name
49 of the primary key, as in "agentnum54" instead of "54", or passed as an arrayref
52 =item base_field (optional)
54 base field, defaults to primary key of this base table
56 =item target_field (optional)
58 target field, defaults to the primary key of the target table
60 =item hashref (optional)
62 static hashref further qualifying the m2m fields
67 my( $self, %opt ) = @_;
69 my $self_pkey = $self->dbdef_table->primary_key;
70 my $base_field = $opt{'base_field'} || $self_pkey;
71 my %hash = $opt{'hashref'} || {};
72 $hash{$base_field} = $self->$self_pkey();
74 my $link_table = $self->_load_table($opt{'link_table'});
76 my $target_table = $self->_load_table($opt{'target_table'});
77 my $target_field = $opt{'target_field'}
78 || dbdef->table($target_table)->primary_key;
80 if ( ref($opt{'params'}) eq 'ARRAY' ) {
81 $opt{'params'} = { map { $_=>1 } @{$opt{'params'}} };
84 local $SIG{HUP} = 'IGNORE';
85 local $SIG{INT} = 'IGNORE';
86 local $SIG{QUIT} = 'IGNORE';
87 local $SIG{TERM} = 'IGNORE';
88 local $SIG{TSTP} = 'IGNORE';
89 local $SIG{PIPE} = 'IGNORE';
91 my $oldAutoCommit = $FS::UID::AutoCommit;
92 local $FS::UID::AutoCommit = 0;
97 my $targetnum = $_->$target_field();
98 ( ! $opt{'params'}->{$targetnum}
99 && ! $opt{'params'}->{"$target_field$targetnum"}
102 qsearch( $link_table, \%hash )
104 my $error = $del_obj->delete;
106 $dbh->rollback if $oldAutoCommit;
111 foreach my $add_targetnum (
112 grep { ! qsearchs( $link_table, { %hash, $target_field => $_ } ) }
113 map { /^($target_field)?(\d+)$/; $2; }
114 grep { /^($target_field)?(\d+)$/ }
115 grep { $opt{'params'}->{$_} }
116 keys %{ $opt{'params'} }
119 my $add_obj = "FS::$link_table"->new( {
121 $target_field => $add_targetnum,
123 my $error = $add_obj->insert;
125 $dbh->rollback if $oldAutoCommit;
130 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
135 my( $self, $table ) = @_;
136 eval "use FS::$table";
147 # my $target_table = $self->_target_table;
148 # eval "use FS::$target_table";