Napoleon: Gosh!
I can't wrap my brain around Class::DBI. I don't think I understand what exactly a has_a is or something. Or maybe it's Friday and it's been a loooooong week.
I'm writing this little code repository script ( that is quite weak) just to get some practice with cdbi.
Here is my module
package Repository::DBI;
use base 'Class::DBI';
Repository::DBI->set_db('Main','dbi:mysql:repository','root','mysql');
package Repository::Users;
use base 'Repository::DBI';
Repository::Users->table('repository_users');
Repository::Users->columns(All => qw`users_id users_name users_email users_since users_password`);
Repository::Users->has_many('entries', "Repository::Entry" => 'entry_user_id');
package Repository::Category;
use base 'Repository::DBI';
Repository::Category->table('repository_cat');
Repository::Category->columns(All => qw`cat_id cat_name cat_description`);
Repository::Category->has_many('entries', 'Repository::Entry' => 'entry_cat_id');
package Repository::Entry;
use base 'Repository::DBI';
Repository::Entry->table('repository_entry');
Repository::Entry->columns(All => qw`entry_id entry_user_id entry_cat_id entry_code entry_description entry_mod_date entry_create_date`);
#Repository::Entry->has_a(user => 'Repository::Users');
#Repository::Entry->has_a(category => 'Repository::Category');
The commented lines are the ones giving me trouble.
And my database:
# category table CREATE TABLE `repository_cat` ( `cat_id` int(11) NOT NULL auto_increment, `cat_name` varchar(50) default NULL, `cat_description` text, PRIMARY KEY (`cat_id`) ) ENGINE=MyISAM; # entry table CREATE TABLE `repository_entry` ( `entry_id` int(11) NOT NULL auto_increment, `entry_user_id` int(11) NOT NULL default '0', `entry_cat_id` int(11) default NULL, `entry_code` longtext, `entry_description` text, `entry_mod_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `entry_create_date` date default NULL, PRIMARY KEY (`entry_id`) ) ENGINE=MyISAM; # users table CREATE TABLE `repository_users` ( `users_id` int(11) NOT NULL auto_increment, `users_name` varchar(50) default NULL, `users_email` varchar(225) default NULL, `users_since` date default NULL, `users_password` varchar(25) default NULL, PRIMARY KEY (`users_id`) ) ENGINE=MyISAM;
My script (below) only works with the last 2 lines in my .pm file commented out.
1 #!/usr/bin/perl
2
3 use strict;
4 use lib '/home/will/lib/';
5 use Repository::DBI;
6
7
8 my $iterator = Repository::Users->retrieve_all;
9 print join("\t",qw/id name email/) , "$/" ;
10 while (my $user = $iterator->next){
11 print join("\t",($user->users_id, $user->users_name, $user->users_email, $/));
12 }
~
~
~
Re:Compilation order of CDBI classes
itub on 2005-07-02T03:24:20
Yep, what I ususally do is I leave all the relationships (or at least the has_many) for the end of the file, after defining all the classes.