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

O wise and omnipotent monks,
I'm using Class::DBI to retrieve a reply to a thread on a message board I am writing. (The code)
#!perl -w use strict; use PL; my $obj = PL->new; my $q = $obj->CGI; my $id = $q->param('id'); unless ( $q->param('go') eq 'yup' ) { my $thread = $obj->DBI->Entries->retrieve($id); my @comments = $thread->replies; my @d_body = (); my @d_comment = (); for (@comments) { my %data; $data{author} = $_->author; $data{content} = $_->content; $data{date} = $_->date; push @d_comment, \%data; } # eww....can't figure out a better way for now though for ( $thread ) { my %data2; $data2{author} = $thread->author; $data2{date} = $thread->date; $data2{content} = $thread->content; push @d_body, \%data2; } $obj->Template->file ("tmpl/comments.tmpl"); print $q->header, $obj->Template->format ( { title=>'Viewing thread: ' . $thread->title, body=> \@d_body, comments => \@d_comment } ); } else { if ( $q->param('go') eq 'yup' ) { my $now = $obj->DBI->now; my %form = ( id => '0', author => $q->param('author'), date => $obj->DBI->now, content => $q->param('content'), thread_id => $id ); $obj->DBI->Replies->create (\%form); $obj->DBI->Replies->update; $obj->DBI->Replies->discard_changes; print $q->header, $q->p('Thank you for your reply', $q->a( {-h +ref=>'reply.cgi?id=' . $q->param('id')}, '&laquo Back')); } }

However, it's only returning 0's, so instead of:



Any ideas?
UPDATE: I'd also like to get a better way for retrieving both the original thread and replies for that thread, I had a good thing going with  @comments = $thread->replies, but then the train of thought just kinda died. Any suggestions?
meh.

Replies are listed 'Best First'.
Re: Class::DBI error?
by cbrandtbuffalo (Deacon) on Jun 10, 2005 at 11:48 UTC
    Don't know if this will solve your problem, but why are you doing:
    for ( $thread ) {
    $thread is one CDBI object with methods for each column. I don't think you're getting anything from looping on it. The code inside the loop should work without the 'for'.

    Other than that it is hard to tell because it seems you are sending some data structures to your template and printing them there. I would try using Data::Dumper with warn to see if d_body and d_comment have what you think they have. Then run it through the template.

      I couldn't think of any other way to do it with what I had...I'm not really sure how to access my data through $thread like I am intending, how should I load up @d_body properly?
      meh.
        Given that $thread is a blessed object your code
        for ( $thread ) { my %data2; $data2{author} = $thread->author; $data2{date} = $thread->date; $data2{content} = $thread->content; push @d_body, \%data2; }
        is equivalent to
        push @d_body, {}; $d_body[-1]->{$_} = $thread->$_ for ("author", "date", "content");
        Still I don't see why you need @d_body. I would simply use
        my $d_body; $d_body->{$_} = $thread->$_ for ("author", "date", "content");
        and later in the code
        body=> [$d_body],


        holli, /regexed monk/
        If the body is just a single record, you should be able to load that with the same code, just without the loop.

        I'm guessing the 0's you are seeing are empty arrays in scalar context, which would happen if you got no records back. You might want to put in a quick check after your retrieve to make sure you are getting a record back. If no records are retrieved, $thread will be undef. Actually, you'll want code to catch this anyway and handle it nicely.