I am preparing to post a more detailed intro to Class::DBI, but your post came just as I was doing the initial walk through of the very same thing. Here is how I resolved the problem, but this will likely be refined further. I was unable to get the Class::DBI::Loader module to work correctly so this code had a two fold value for me. This first part is in your initial Class file, the one that defines the DBI connection parameters.
package My::ClassDBI;
use strict;
use warnings;
use base 'Class::DBI';
my $dsn = 'dbi:mysql:database';
my $user = 'user';
my $password = 'password';
My::ClassDBI->set_db('Main', $dsn, $user, $password);
# auto load tables
my $base = __PACKAGE__; # "My::ClassDBI";
# create connection to database
my $dbh = DBI->connect($dsn,$user,$password)
or die $DBI::errstr;
# not the Right Way(tm) but works for now
my $get_tables = $dbh->prepare(qq!SHOW TABLES!);
$get_tables->execute;
my @tables;
while ( my $table = $get_tables->fetchrow ) {
my @columns;
my $ucftable = ucfirst($table);
my $get_column = $dbh->prepare(qq!DESC $table!);
$get_column->execute();
while ( my @cols = $get_column->fetchrow() ) {
# force the primary key to be first
# this insures Class::DBI correctly relates table
# without having to manually define the Primary Key
$cols[3] =~ /pri/i ? unshift @columns , $cols[0]
: push @columns , $cols[0]
}
eval qq!package $ucftable;
use base '$base';
$ucftable->table('$table');
$ucftable->columns(All => qw/! .
join(" ",@columns) .
"/);";
}
# Table1 and Table 2 class dynamically generated above
Table1->has_many('method_name_you_want' ,
Table2 => 'related_column' );
1;
Then inside your application you simply use My::ClassDBI and all your classes (tables) are sub classed under it.
use My::ClassDBI;
use strict;
# you can place the has_many here or in the
# package as shown above
# Table1->has_many('method_name_you_want' ,
# Table2 => 'related_column' );
my $row_from_table1 = Table1->retrieve(1);
my @table2_objects = $row_from_table1->method_name_you_want;
foreach (@table2_objects) {
print $_->column_name , "\n";
}
1;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|