1 package FS::m2m_Common;
4 use vars qw( @ISA $DEBUG $me );
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 );
12 $me = '[FS::m2m_Common]';
16 FS::m2m_Common - Mixin class for classes in a many-to-many relationship
22 @ISA = qw( FS::m2m_Common FS::Record );
26 FS::m2m_Common is intended as a mixin class for classes which have a
27 many-to-many relationship with another table (via a linking table).
29 It is currently assumed that the link table contains two fields named the same
30 as the primary keys of the base and target tables, but you can ovverride this
31 assumption if your table is different.
37 =item process_m2m OPTION => VALUE, ...
43 =item link_table (required)
45 =item target_table (required)
47 =item params (required)
49 hashref; keys are primary key values in target_table (values are boolean). For convenience, keys may optionally be prefixed with the name
50 of the primary key, as in "agentnum54" instead of "54", or passed as an arrayref
53 =item base_field (optional)
55 base field, defaults to primary key of this base table
57 =item target_field (optional)
59 target field, defaults to the primary key of the target table
61 =item hashref (optional)
63 static hashref further qualifying the m2m fields
68 my( $self, %opt ) = @_;
71 #warn "$me process_m2m called on $self with options:\n". Dumper(%opt)
72 warn "$me process_m2m called on $self"
75 my $self_pkey = $self->dbdef_table->primary_key;
76 my $base_field = $opt{'base_field'} || $self_pkey;
77 my $hashref = $opt{'hashref'} || {};
78 $hashref->{$base_field} = $self->$self_pkey();
80 my $link_table = $self->_load_table($opt{'link_table'});
82 my $target_table = $self->_load_table($opt{'target_table'});
83 my $target_field = $opt{'target_field'}
84 || dbdef->table($target_table)->primary_key;
86 if ( ref($opt{'params'}) eq 'ARRAY' ) {
87 $opt{'params'} = { map { $_=>1 } @{$opt{'params'}} };
90 local $SIG{HUP} = 'IGNORE';
91 local $SIG{INT} = 'IGNORE';
92 local $SIG{QUIT} = 'IGNORE';
93 local $SIG{TERM} = 'IGNORE';
94 local $SIG{TSTP} = 'IGNORE';
95 local $SIG{PIPE} = 'IGNORE';
97 my $oldAutoCommit = $FS::UID::AutoCommit;
98 local $FS::UID::AutoCommit = 0;
101 foreach my $del_obj (
103 my $targetnum = $_->$target_field();
104 ( ! $opt{'params'}->{$targetnum}
105 && ! $opt{'params'}->{"$target_field$targetnum"}
108 qsearch( $link_table, $hashref )
110 my $error = $del_obj->delete;
112 $dbh->rollback if $oldAutoCommit;
117 foreach my $add_targetnum (
118 grep { ! qsearchs( $link_table, { %$hashref, $target_field => $_ } ) }
119 map { /^($target_field)?(\d+)$/; $2; }
120 grep { /^($target_field)?(\d+)$/ }
121 grep { $opt{'params'}->{$_} }
122 keys %{ $opt{'params'} }
125 my $add_obj = "FS::$link_table"->new( {
127 $target_field => $add_targetnum,
129 my $error = $add_obj->insert;
131 $dbh->rollback if $oldAutoCommit;
136 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141 my( $self, $table ) = @_;
142 eval "use FS::$table";
153 # my $target_table = $self->_target_table;
154 # eval "use FS::$target_table";