http://www.perlmonks.org?node_id=446896

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

I've been using Class::DBI and decided to try Class::DBI::Loader.

Unfortunately I get an error even when I've reduced my code to a minimum, which is almost a duplicate of example the documentation:

package MyCompany::MyProduct::Database; use Class::DBI::Loader; my $loader = Class::DBI::Loader->new( dsn => "dbi:mysql:databasename", user => "username", password => "password", namespace => 'MyCompany::MyProduct::Database', relationships => 1); 1;
I am then using this module from a simple script which only tries to retrieve something from one of the tables.

This gives me the error 'Can't locate object method "set_db"' for the first table (alphabetically) in my database. It's obviously not inheriting correctly from Class::DBI somewhere along the line, but adding Class::DBI as a base does not solve the problem.

By adding Class::DBI to @ISA in Class::DBI::Loader::Generic's _load_classes routine I can force my way past the problem just long enough to get hung up on a call to set_up_table(). Clearly there are also other things the author wanted.

I don't see this problem listed in open bugs for Class::DBI::Loader and I find it difficult to imagine that I'd be the first one to find it. Therefore I hope perhaps someone can point me to what I'm doing wrong. I've read related nodes on the subject, such as this one, but that didn't seem to provide the answer.

I am using Perl 5.8.5, Class::DBI::Loader version 0.18, and Class::DBI version 0.96.

Thank you.

Replies are listed 'Best First'.
Re: Class::DBI::Loader error: Can't locate object method "set_db"
by Corion (Patriarch) on Apr 12, 2005 at 10:29 UTC

    My guess is that you want namespace => 'MyCompany::MyProduct' instead of namespace => 'MyCompany::MyProduct::Database', at least if you plan on accessing the classes/tables later as MyCompany::MyProduct::MyTable->new(...). Other than that, I can't find anything that directly strikes me as odd, so maybe some more surrounding code would be helpfull.

      Tables will be accessed as MyCompany::MyProduct::Database::Tablename.

      What I have shown above is the entire module. I removed all other code to give as clear of an example as possible.

      Here is the code that tries to use it:

      #!/usr/local/bin/perl use strict; use warnings; use lib '/myproject/lib'; use MyCompany::MyProduct::Database; use Data::Dumper; my $person = MyCompany::MyProduct::Database::Person->retrieve(7); print Dumper $person;

        The following code works for me. It has the following differences:

        • It uses DBD::SQLite, where you use DBD::mysql
        • I didn't test out putting the whole stuff into two separate files.

        One thing that comes to mind: Did you install the proper Class::DBI subclass (Class::DBI::mysql) ?

        Table setup script

        #!/usr/bin/perl -w use strict; use DBI; my $dbh = DBI->connect('dbi:SQLite:dbfile=kudra.sqlite',"","", {AutoCo +mmit => 0}); my @statements; { local $/; @statements = split /;/, <DATA>; }; for my $statement (@statements) { print "$statement\n"; eval { $dbh->do($statement); $dbh->commit; }; $@ ||= $dbh->errstr; if ($@) { if ($statement =~ /^drop/i) { warn $@; } else { die $@; }; }; }; $dbh->commit; $dbh->disconnect; __DATA__ create table person ( id INTEGER PRIMARY KEY, name VARCHAR(256) ); insert into person (name) values ("Corion"); insert into person (name) values ("Kudra"); insert into person (name) values ("vroom"); insert into person (name) values ("jcwren");

        Class::DBI script

        #!/usr/local/bin/perl -w package MyCompany::MyProduct::Database; use Class::DBI::Loader; my $loader = Class::DBI::Loader->new( dsn => "dbi:SQLite:dbfile=kudra.sqlite", user => "username", password => "password", namespace => 'MyCompany::MyProduct::Database', relationships => 1); package main; use strict; use warnings; #use MyCompany::MyProduct::Database; use Data::Dumper; my $person = MyCompany::MyProduct::Database::Person->retrieve(3); print Dumper $person;