Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

silent11 (2860)

  (email not shown publicly)

Journal of silent11 (2860)

Friday July 01, 2005
03:43 PM

Fed up with myself ( Class::DBI hurts my brain )

[ #25473 ]
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';

packag e Repository::Users;
        use base 'Repository::DBI';
        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->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->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`)

# 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`)

# 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`)

My script (below) only works with the last 2 lines in my .pm file commented out.

1 #!/usr/bin/perl
3 use strict;
4 use lib '/home/will/lib/';
5 use Repository::DBI;
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 }

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • If you are defining all of these classes in a single file then you should make sure that you define your Repository::Entry class before defining the Repository::Users and Repository::Category classes.
    The CDBI docs states "When setting up the relationship we examine the foreign class's has_a() declarations to discover which of its columns reference our class. (Note that because this happens at compile time, if the foreign class is defined in the same file, the class with the has_a() must be defined earlier t