Subject: | CDBIP::Param should not die when called with non-existent column name |
First off - great module! Until now, I've been using my own homegrown
param method in my Class::DBI base class for the same purpose (HTML::
FillInForm compatibility). But it's better to have a standard CPAN
solution.
However the problem with the current implementation of C::DBI::P::Param
is that it dies when 'param' is called to get the value of a non-
existent parameter.
Most implementations of 'param' (e.g. the one in CGI.pm) return undef
when the parameter does not exist.
HTML::FillInForm will call 'param' on the object for every field in a
form. Since most forms contain extra fields (e.g. hidden fields) in
addition to the object fields, C::DBI::P::Param will fail as soon as one
of these fields is encountered.
Also, it is common to use HTML::FillInForm to fill a form with the
contents of several objects:
$output = $fif->fill(scalarref => \$html,
fobject => [$obj1, $obj2]);
Some fields in the form belong to $obj1 and other fields belong to
$obj2, but C::DBI::P::Param will fail as soon as it encounters a field
that does not exist in both objects.
This can be fixed by wrapping the call to $self->$column_name in an
eval:
return eval { $self->$column_name(@_) };
I've attached a patch (including a test) that works this way.
But you might consider (for completeness) adding the other behaviours of
CGI's 'param' as well:
my @keys = $self->param # get all keys
my @values = $self->param('foo','bar','baz'); # get multiple values
Also, C::DBI::P::Param works fine with Class::DBI 0.96. Maybe the
prerequisite could be dropped to that version?
Michael
Subject: | Class-DBI-Plugin-Param-0.03.patch |
diff -ur Class-DBI-Plugin-Param-0.03/lib/Class/DBI/Plugin/Param.pm Class-DBI-Plugin-Param-0.03.new/lib/Class/DBI/Plugin/Param.pm
--- Class-DBI-Plugin-Param-0.03/lib/Class/DBI/Plugin/Param.pm 2006-04-20 00:48:38.000000000 -0300
+++ Class-DBI-Plugin-Param-0.03.new/lib/Class/DBI/Plugin/Param.pm 2006-06-18 02:53:59.000000000 -0300
@@ -17,7 +17,7 @@
my $self = shift;
my $column_name = shift or
croak "You gave me no parameters to param()!";
- return $self->$column_name(@_);
+ return eval { $self->$column_name(@_) };
}
}
}
Only in Class-DBI-Plugin-Param-0.03.new/: pm_to_blib
diff -ur Class-DBI-Plugin-Param-0.03/t/01-methods.t Class-DBI-Plugin-Param-0.03.new/t/01-methods.t
--- Class-DBI-Plugin-Param-0.03/t/01-methods.t 2006-04-06 07:10:04.000000000 -0300
+++ Class-DBI-Plugin-Param-0.03.new/t/01-methods.t 2006-06-18 02:54:19.000000000 -0300
@@ -7,7 +7,7 @@
BEGIN {
eval "use DBD::SQLite";
- plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5);
+ plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 6);
}
Music::CD->CONSTRUCT;
@@ -20,7 +20,13 @@
ok $row->param('artist') eq 'baz';
ok $row->is_changed;
+# non existent params
+
+eval { $row->param('non-existent!') };
+ok !$@, "no error on non existent method";
+
$row->discard_changes; # stop warning
ok not $row->is_changed;