use strict;
use vars qw( @ISA $DEBUG );
+use Scalar::Util qw( blessed );
use FS::Record qw( qsearch qsearchs dbh );
@ISA = qw( FS::Record );
my $valuecol = $self->_option_valuecol;
foreach my $optionname ( keys %{$options} ) {
+
+ my $optionvalue = $options->{$optionname};
+
my $href = {
$pkey => $self->get($pkey),
$namecol => $optionname,
- $valuecol => $options->{$optionname},
+ $valuecol => ( ref($optionvalue) || $optionvalue ),
};
#my $option_record = eval "new FS::$option_table \$href";
#}
my $option_record = "FS::$option_table"->new($href);
- $error = $option_record->insert;
+ my @args = ();
+ push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
+
+ $error = $option_record->insert(@args);
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;
}
+
}
$dbh->commit or die $dbh->errstr if $oldAutoCommit;
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, part_export_option records are
-created or modified (see L<FS::part_export_option>).
+If a list or hash reference of options is supplied, option records are created
+or modified.
=cut
sub replace {
my $self = shift;
- my $old = ( ref($_[0]) eq ref($self) )
+ my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
? shift
: $self->replace_old;
( ref($_[0]) eq 'HASH' )
? shift
: { @_ };
+
warn "FS::option_Common::replace called on $self with options ".
join(', ', map "$_ => ". $options->{$_}, keys %$options)
if $DEBUG;
$namecol => $optionname,
} );
+ my $optionvalue = $options->{$optionname};
+
+ my %oldhash = $oldopt ? $oldopt->hash : ();
+
my $href = {
+ %oldhash,
$pkey => $self->get($pkey),
$namecol => $optionname,
- $valuecol => $options->{$optionname},
+ $valuecol => ( ref($optionvalue) || $optionvalue ),
};
#my $newopt = eval "new FS::$option_table \$href";
my $opt_pkey = $newopt->primary_key;
$newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
- warn $oldopt;
+
+ my @args = ();
+ push @args, $optionvalue if ref($optionvalue); #only hashes supported so far
+
warn "FS::option_Common::replace: ".
( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
if $DEBUG > 2;
- my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert;
- warn $error;
+ my $error = $oldopt ? $newopt->replace($oldopt, @args)
+ : $newopt->insert( @args);
if ( $error ) {
$dbh->rollback if $oldAutoCommit;
return $error;