Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Class::DBI find_or_create method inserts record but $obj->id is undef by jerrygarciuh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-20 16:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found