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

Esteemed Monks,

In using Class::DBI::mysql to get some data from a single table using the code fragment below:

sub en_display { # Display 'full signup or enroll' form my $self = shift; my $errs = shift; my $output =''; my $sess = $self->param('up_session'); if ( $sess->param('_IS_USER') && $sess->param('_IS_REGISTERED') ) +{ my $obj = AppSys::User->search( 'upusername' => $sess->param(' +_IS_USER')); while ( my $usr = $obj->next ) { #$output .= "Username: " . $usr->upusername . "<br><br>"; foreach ( $usr->columns ) { $output .= "<b>$_</b>; $usr->{$_} <br>"; } } } return $output;
I find I get no output printed. This is wierd! So I went back to the documentation and could find nothing. Then I recalled that when I first looked at Class::DBI I tried the code from Class::DBI Intro by trs80. Now in there I read:
# now we will make an object that relates to a single # record. We know that our entry is id 1 since it is # the only record in the database so we do the following our $user = Table::User->retrieve(1); # $user contains our object, but since we haven't called # any methods on the objects it has not made any calls to # the database. print ">> Class::DBI Results (single column)\n"; print " Users First Name: " , $user->first_name , "\n\n"; # Now $user contains all the information related to # the record in the database. You can verify this with # Data::Dumper if you want.
Which is just what I did and, yes, he is right! In my own code at the top of the page if I uncomment the line #$output .= "Username: " . $usr->upusername . "<br><br>"; I get the expected output.

Why do I need this apparently redundant method call to get the data into the object? Is there some way of ensuring that the data is loaded without the additonal call?

jdtoronto

Replies are listed 'Best First'.
Re: Class::DBI strangeness...
by duct_tape (Hermit) on Dec 19, 2003 at 20:47 UTC
    Hello,

    In the Class::DBI pod there is a section about LAZY POPULATION. Basically it loads as little as possible into the object in hopes to increase performance.

    Assuming you specified all the columns that you want in the All (or Essential) group in your columns() call, whenever you make a call to any column accessor it will load the rest of them into object as well. So you can change your code to the following without worrying that it is querying the database for every column:

    ... foreach ( $usr->columns ) { $output .= "<b>$_</b>; " . $usr->$_ . "<br>"; } ...

    To see this in action, call $usr->upusername and then dump the $usr object and in theory it should also have the rest of the columns in it as well.

    I have also noticed that when you call create() with all the columns, it only returns back an object with the primary key in it. I assume it does this type of stuff in case you have triggers set up in your database that did something with the columns after you inserted them. Just a guess though...

    Take my comments with a grain of salt... I've only been playing around with CDBI for less than a week, so I am not too experienced with it. But I hope I am of some help.


    Regards,
    Bradley C Bailey
Re: Class::DBI strangeness...
by tantarbobus (Hermit) on Dec 19, 2003 at 20:50 UTC
    Try changing
    $output .= "<b>$_</b>; $usr->{$_} <br>";
    to  $output .="<b>$_</b>;".$usr->$_."<br>".