Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Re: Dynamic DSN w/ Class::DBI Example

by gryphon (Abbot)
on Apr 23, 2004 at 22:11 UTC ( #347765=note: print w/replies, xml ) Need Help??

in reply to Re: Dynamic DSN w/ Class::DBI Example
in thread Dynamic DSN w/ Class::DBI Example

Pardon my stupidity, but do you mean like this:

package Stuff::DBI; use base 'Class::DBI::BaseDSN'; package Stuff::Item; use base 'Stuff::DBI'; __PACKAGE__->set_up_table('item'); __PACKAGE__->has_a(categoryId => 'Stuff::Category'); package Stuff::Category; use base 'Stuff::DBI'; __PACKAGE__->set_up_table('category'); __PACKAGE__->has_a(departmentId => 'Stuff::Department'); package Stuff::Department; use base 'Stuff::DBI'; __PACKAGE__->set_up_table('department'); package main; Stuff::DBI->set_db('Main', 'dbi:mysql:dbname', 'gryphon', 'password'); my $item = Stuff::Item->retrieve(1); print $item->name, "\n";

When I run this, I get a "Can't locate object method 'set_up_table' via package 'Stuff::Item' blah blah" when I run this. What am I doing wrong?

code('Perl') || die;

Replies are listed 'Best First'.
Re: Re: Re: Dynamic DSN w/ Class::DBI Example
by perrin (Chancellor) on Apr 23, 2004 at 22:19 UTC
    Get rid of that BaseDSN stuff. You have to subclass Class::DBI::MySQL, like you were before, to get the set_up_table method.

      Switching to use base 'Class::DBI::mysql' results in an error of "Can't locate object method 'db_Main' via package 'Stuff::Item' at blah, blah". I think I'm not smart enough to use Class::DBI. Do you have any working example code you can share with me? Thanks.

      code('Perl') || die;

        Calm down, Class::DBI is much easier than lots of other things you've already learned.

        I'm guessing you are getting into trouble because you are calling set_up_table() before you call set_db(). That won't work because set_up_table() needs to talk to the database to find out about the table. So, load your other modules after you call set_db(). You could use require(), or you could put the call to set_db() in a BEGIN {} before you use() the other modules. And don't put them all in one file like that. It's cool, but not a good idea in the long run.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347765]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-03-02 07:49 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (40 votes). Check out past polls.