Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2014-10-02 06:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (49 votes), past polls