Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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;

In reply to Re: Class::dbi::loader and OO structure by trs80
in thread Class::dbi::loader and OO structure by rkg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-26 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found