blob: fd8700a2d93a100c8bbdd6a5c45f9a890b05c09a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package FS::m2m_Common;
use strict;
use vars qw( @ISA $DEBUG );
use FS::Schema qw( dbdef );
use FS::Record qw( qsearch qsearchs ); #dbh );
@ISA = qw( FS::Record );
$DEBUG = 0;
=head1 NAME
FS::m2m_Common - Base class for classes in a many-to-many relationship
=head1 SYNOPSIS
use FS::m2m_Common;
@ISA = qw( FS::m2m_Common );
=head1 DESCRIPTION
FS::m2m_Common is intended as a base class for classes which have a
many-to-many relationship with another table (via a linking table).
Note: It is currently assumed that the link table contains two fields
named the same as the primary keys of ths base and target tables.
=head1 METHODS
=over 4
=item process_m2m
=cut
sub process_m2m {
my( $self, %opt ) = @_;
my $self_pkey = $self->dbdef_table->primary_key;
my $link_table = $self->_load_table($opt{'link_table'});
my $target_table = $self->_load_table($opt{'target_table'});
my $target_pkey = dbdef->table($target_table)->primary_key;
foreach my $target_obj ( qsearch($target_table, {} ) ) {
my $targetnum = $target_obj->$target_pkey();
my $link_obj = qsearchs( $link_table, {
$self_pkey => $self->$self_pkey(),
$target_pkey => $targetnum,
});
if ( $link_obj && ! $opt{'params'}->{"$target_pkey$targetnum"} ) {
my $d_link_obj = $link_obj; #need to save $link_obj for below.
my $error = $d_link_obj->delete;
die $error if $error;
} elsif ( $opt{'params'}->{"$target_pkey$targetnum"} && ! $link_obj ) {
#ok to clobber it now (but bad form nonetheless?)
#$link_obj = new "FS::$link_table" ( {
$link_obj = "FS::$link_table"->new( {
$self_pkey => $self->$self_pkey(),
$target_pkey => $targetnum,
});
my $error = $link_obj->insert;
die $error if $error;
}
}
'';
}
sub _load_table {
my( $self, $table ) = @_;
eval "use FS::$table";
die $@ if $@;
$table;
}
#=item target_table
#
#=cut
#
#sub target_table {
# my $self = shift;
# my $target_table = $self->_target_table;
# eval "use FS::$target_table";
# die $@ if $@;
# $target_table;
#}
=back
=head1 BUGS
=head1 SEE ALSO
L<FS::Record>
=cut
1;
|