Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Class::DBI find_or_create method inserts record but $obj->id is undef

by jerrygarciuh (Curate)
on Jan 23, 2004 at 23:46 UTC ( #323738=perlquestion: print w/ replies, xml ) Need Help??
jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

Honored monks,
I am making the administrative methods for a largish educational project using Class::DBI, Template-Toolkit, and CGI::Application. I have had three occasions now when attempting to use the sequence
my $id = shift; $id ||= $q->param('id'); my $obj = myDBI::SomeTable->find_or_create({ id => $id }); foreach my $field ($q->param) { $obj->$field(scalar $q->param($field)) if $obj->can($field); } $obj->update;

If I set $id explicitly to undef then the Class::DBI record object $obj has the new record's row in $obj->id after find_or_create(). Thing is I need to check @_ and $q->param() for $id. Now, if either @_ or $q->param() has a value for $id, the the record gets appropriately put into $obj, altered in my foreach and then updated by $obj->update;

The bug shows up when $q->param and @_ do not have a value for $id. Class::DBI's find_or_create() inserts the new row in the db but $obj->id is undef, and consequently the $obj->update does no good.

So my question to you is how best to do this. Should I make a test for the contents of $id and set it explicitly to undef if it fails? What is a Good way to do this?
TIA
jg
sub save_to_db { my $self = shift; my $class_id = shift; my $educator_id = shift; # get CGI query object my $q = $self->query; # find or create an object for this class $class_id ||= $q->param('class_id'); $educator_id ||= $q->param('educator_id'); my $class = AIOC::Class->find_or_create({ id => $class_id }); # set the values of each field in the object foreach my $field ($q->param) { $class->$field(scalar $q->param($field)) if $class->can($field +); } $class->educator($educator_id); # save the values to the database $class->update; # class_id may have been undef before create() $class_id ||= $class->id; # print data_form return $self->render_page('cp/cp_index.html', { educator_id => +$class->educator }); } # end save_to_db()
_____________________________________________________
"The man who grasps principles can successfully select his own methods.
The man who tries methods, ignoring principles, is sure to have trouble.
~ Ralph Waldo Emerson

Comment on Class::DBI find_or_create method inserts record but $obj->id is undef
Select or Download Code
Re: Class::DBI find_or_create method inserts record but $obj->id is undef
by perrin (Chancellor) on Jan 24, 2004 at 00:09 UTC
    You should check out Class::DBI::FromCGI, which does this for you. However, if you just delete the id field from your CGI object after the call to find_or_create, your current code should work.
      Perrin,
      Thank you for the reply. Perhaps I am failing to grok how to delete the field from the CGI object, for instance if I do this:

      $q->param('class_id') = undef;

      I get "Can't modify non-lvalue subroutine call".
      Is there some other way to alter the object that will work here?
      TIA
      jg
      _____________________________________________________
      "The man who grasps principles can successfully select his own methods.
      The man who tries methods, ignoring principles, is sure to have trouble.
      ~ Ralph Waldo Emerson

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://323738]
Approved by jdtoronto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-12-26 07:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (167 votes), past polls