freddo411 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I'm using Class::DBI in my perl code to provide an OO interface to my simple DB table. The application is a classic CRUD pattern (create/retrieve/update/delete).

I've successfully set up my table, and have retrieved data, created data, and deleted data. I'm having trouble figuring how to update a record with the OO methods. The Class::DBI docs show only one example of an update, and in that one it simply updates a single field. I have a hash containing fieldnames/values and would like to pass it all at once.

Can anyone provide an example, or point me to method code to accomplish this?

# Various stuff above to use Class::DBI and set it up # verified to work by retrieving # Put the validated fields into the DB # New, unique ID number is automagically generated # if needed my $hashref = $results->valid; my %hash = %$hashref; my $foo = $hash{'id'}; carp "ID is: $foo "; my $entry = Contacts->find_or_create( id => $foo ); $entry->$hashref; # this doesn't work $entry->dbi_commit(); $entry->update;

-------------------------------------
Nothing is too wonderful to be true
-- Michael Faraday

Replies are listed 'Best First'.
Re: Class::DBI question
by perrin (Chancellor) on Oct 20, 2003 at 21:32 UTC
    I'm not sure what you imagined was going to happen when you called $entry->$hashref, but it doesn't work that way. $entry is an object, and you access it by calling method. If you want to update the fields foo, bar, and baz, you do this:
    $entry->foo(7); $entry->bar('some string'); $entry->baz(23.5); $entry->update(); $entry->dbi_commit();
      What I'd like is a method called "update" that works like "create":

      # as yet wishfull thinking... my $cd = Music::CD->update({ cdid => 1, artist => $artist, title => 'October', year => 1980, });

      I'm surprized that one hasn't been written like this already. I may have to write this myself, but it would be nice not have to.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

        I'm surprized that one hasn't been written like this already. I may have to write this myself, but it would be nice not have to.
        Well, it's called set
        my $cd = Music::CD->retrieve( cdid => 1 ) or die "no cd 1"; $cd->set( artist => $artist, title => 'October', year => 1980, );
        update: that's why they call me PodMaster ;D

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Class::DBI question
by set_uk (Pilgrim) on Oct 21, 2003 at 10:13 UTC
    Funnily enough I had this same problem yesterday. The update will change the values - but you want the commit to happen after you change them not before.
    I assumed that an implicit commit was part of the update but it isn't with Oracle