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

Esteemed Monks,

After receiving the use ClassDBI mantra as a seemingly subliminal message from the monastery over some months I have now decided to try it. ne section of my major web-app is no complete and so I have some time to learn something new.

In fact object orientation is something new to this monk! I have written this subclass which I seem to be able to make work after a fashion.

package User; use Class::DBI::AsForm; use Class::DBI::Iterator; use base 'Class::DBI::mysql'; use strict; use warnings; __PACKAGE__->set_db('Main', 'DBI:mysql:appsys', 'root', ''); __PACKAGE__->set_up_table("ap_uprofile"); 1;
Now, in my run_mode I have this code:
my $self = shift; my $q = $self->query(); my $obj = User->search_like( 'upfirstname' => 'john%' ); print STDERR Dumper( $obj ); my @cols = $obj->columns; print STDERR Dumper ( @cols );
but I continually get told that the method columns cannot be found in Class::DBI::Iterator.

OK, so I am brain dead, but I just cannot seem to work out what is going on! Here is what I am trying to do:

  1. I search the table, and in this case I come up with 4 results. I want to print out a simple form that allows me to select one of the returned entries.
  2. I then want to retrieve the data for the selected record. Can I get this easily in a has (or hashref) that I can pass to HTML::Template to fill out the form fields?
  3. How do I get a count of how many records ( objects? ) are being returned?
I suspect that I am not use'ing the modules correctly. But the doc's are not aimed at neophytes :)

jdtoronto

Replies are listed 'Best First'.
Re: Object Oriented Confusion!
by jeffa (Bishop) on Dec 11, 2003 at 20:35 UTC
    That is because $obj is a Class::DBI::Iterator, not a User. If you want a User, you have to call search_like() in array context:
    my ($obj) = User->search_like( 'upfirstname' => 'john%' );
    Now, normally you would use an array on the left hand side, since you likely have multiple matches. This is just a trick i like to use when i want only one Foo object and not an iterator.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)