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 @ISA = qw( FS::Record );
14 FS::m2m_Common - Base class for classes in a many-to-many relationship
20 @ISA = qw( FS::m2m_Common );
24 FS::m2m_Common is intended as a base class for classes which have a
25 many-to-many relationship with another table (via a linking table).
27 Note: It is currently assumed that the link table contains two fields
28 named the same as the primary keys of ths base and target tables.
39 my( $self, %opt ) = @_;
41 my $self_pkey = $self->dbdef_table->primary_key;
43 my $link_table = $self->_load_table($opt{'link_table'});
45 my $target_table = $self->_load_table($opt{'target_table'});
46 my $target_pkey = dbdef->table($target_table)->primary_key;
48 foreach my $target_obj ( qsearch($target_table, {} ) ) {
50 my $targetnum = $target_obj->$target_pkey();
52 my $link_obj = qsearchs( $link_table, {
53 $self_pkey => $self->$self_pkey(),
54 $target_pkey => $targetnum,
57 if ( $link_obj && ! $opt{'params'}->{"$target_pkey$targetnum"} ) {
59 my $d_link_obj = $link_obj; #need to save $link_obj for below.
60 my $error = $d_link_obj->delete;
63 } elsif ( $opt{'params'}->{"$target_pkey$targetnum"} && ! $link_obj ) {
65 #ok to clobber it now (but bad form nonetheless?)
66 #$link_obj = new "FS::$link_table" ( {
67 $link_obj = "FS::$link_table"->new( {
68 $self_pkey => $self->$self_pkey(),
69 $target_pkey => $targetnum,
71 my $error = $link_obj->insert;
81 my( $self, $table ) = @_;
82 eval "use FS::$table";
93 # my $target_table = $self->_target_table;
94 # eval "use FS::$target_table";