1 package FS::m2name_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::m2name_Common - Base class for tables with a related table listing names
18 use FS::m2name_Common;
20 @ISA = qw( FS::m2name_Common );
24 FS::m2name_Common is intended as a base class for classes which have a
25 related table that lists names.
36 my( $self, %opt ) = @_;
38 my $self_pkey = $self->dbdef_table->primary_key;
39 my $link_sourcekey = $opt{'num_col'} || $self_pkey;
41 my $link_table = $self->_load_table($opt{'link_table'});
43 my $link_static = $opt{'link_static'} || {};
45 foreach my $name ( @{ $opt{'names_list'} } ) {
47 my $obj = qsearchs( $link_table, {
48 $link_sourcekey => $self->$self_pkey(),
49 $opt{'name_col'} => $name,
53 if ( $obj && ! $opt{'params'}->{"$link_table.$name"} ) {
55 my $d_obj = $obj; #need to save $obj for below.
56 my $error = $d_obj->delete;
57 die "error deleting $d_obj for $link_table.$name: $error" if $error;
59 } elsif ( $opt{'params'}->{"$link_table.$name"} && ! $obj ) {
61 #ok to clobber it now (but bad form nonetheless?)
62 #$obj = new "FS::$link_table" ( {
63 $obj = "FS::$link_table"->new( {
64 $link_sourcekey => $self->$self_pkey(),
65 $opt{'name_col'} => $name,
68 my $error = $obj->insert;
69 die "error inserting $obj for $link_table.$name: $error" if $error;
78 my( $self, $table ) = @_;
79 eval "use FS::$table";