X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Foption_Common.pm;h=968dcdf798ea68901db5906b37fa4f01d6d10518;hp=441e798d2356796edb078d2860d40c86113dc451;hb=5b73387992a96f7b80e40b5ecb2fedabd8a78d6b;hpb=9509e5bfb7f9331303153cac24d7bfecbe2ea9f1 diff --git a/FS/FS/option_Common.pm b/FS/FS/option_Common.pm index 441e798d2..968dcdf79 100644 --- a/FS/FS/option_Common.pm +++ b/FS/FS/option_Common.pm @@ -1,12 +1,11 @@ package FS::option_Common; use strict; -use vars qw( @ISA $DEBUG ); +use base qw( FS::Record ); +use vars qw( $DEBUG ); use Scalar::Util qw( blessed ); use FS::Record qw( qsearch qsearchs dbh ); -@ISA = qw( FS::Record ); - $DEBUG = 0; =head1 NAME @@ -161,8 +160,8 @@ sub delete { Replaces the OLD_RECORD with this one in the database. If there is an error, returns the error, otherwise returns false. -If a list hash reference of options is supplied, option records are created or -modified. +If a list or hash reference of options is supplied, option records are created +or modified. =cut @@ -173,10 +172,15 @@ sub replace { ? shift : $self->replace_old; - my $options = - ( ref($_[0]) eq 'HASH' ) - ? shift - : { @_ }; + my $options; + my $options_supplied = 0; + if ( ref($_[0]) eq 'HASH' ) { + $options = shift; + $options_supplied = 1; + } else { + $options = { @_ }; + $options_supplied = scalar(@_) ? 1 : 0; + } warn "FS::option_Common::replace called on $self with options ". join(', ', map "$_ => ". $options->{$_}, keys %$options) @@ -252,13 +256,15 @@ sub replace { } #remove extraneous old options - foreach my $opt ( - grep { !exists $options->{$_->$namecol()} } $old->option_objects - ) { - my $error = $opt->delete; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return $error; + if ( $options_supplied ) { + foreach my $opt ( + grep { !exists $options->{$_->$namecol()} } $old->option_objects + ) { + my $error = $opt->delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } } } @@ -317,6 +323,19 @@ sub option { $obj ? $obj->$valuecol() : ''; } +=item option_cacheable OPTIONNAME + +Same as the option method, but may cache and return a cached value. +Good for use within loops; otherwise, probably avoid. + +=cut + +sub option_cacheable { + my( $self, $name ) = @_; + return $self->{option_cache}{$name} if exists $self->{option_cache}{$name}; + $self->{option_cache}{$name} = $self->option($name,1); +} + sub option_table { my $self = shift;