Esteemed monks,

I cannot understand the 'mapping' method described in the documentation. Here is what I have - starting with three tables:

CREATE TABLE `user` ( `user_id` int(11) NOT NULL auto_increment, `firstname` varchar(50) NOT NULL default '', `lastname` varchar(50) NOT NULL default '', PRIMARY KEY (`user_id`) ) TYPE=MyISAM; insert into `user` values (1, 'John', 'Day'), (2, 'Jacqui', 'Burke'), (3, 'Brenda', 'Day'), (4, 'Cassie', 'Cat'), (5, 'Steven', 'Burke'), (6, '', ''); CREATE TABLE `role` ( `role_id` int(11) NOT NULL auto_increment, `rolename` varchar(50) NOT NULL default '', PRIMARY KEY (`role_id`) ) TYPE=MyISAM; insert into role values (1, 'Typist'), (2, 'Programmer'), (3, 'Cleaner'); CREATE TABLE `user_role` ( `user_id` int(11) NOT NULL default '0', `role_id` int(11) NOT NULL default '0' ) TYPE=MyISAM; insert into user_role values (1, 1), (1, 2), (1, 3), (2, 2), (3, 1), (3, 2), (0, 0);
My Class::DBI subclass:
package Test::DBI; use base 'Class::DBI'; Test::DBI->set_db('Main', 'DBI:mysql:appsys2', '****', '****'); package TestDBI::userrole; use base 'Test::DBI'; __PACKAGE__->table('user_role'); __PACKAGE__->columns(All => qw(user_id role_id)); __PACKAGE__->has_a( role_id => TestDBI::role ); __PACKAGE__->has_a( user_id => TestDBI::user ); package TestDBI::user; use base 'Test::DBI'; __PACKAGE__->table('user'); __PACKAGE__->columns(All => qw(user_id firstname lastname)); __PACKAGE__->has_many(haveroles => ['TestDBI::userrole' => 'role_id'] +); package TestDBI::role; use base 'Test::DBI'; __PACKAGE__->table('role'); __PACKAGE__->columns(All => qw(role_id rolename)); 1;
and my test code:
#!/usr/local/bin/perl; use warnings; use strict; use Data::Dumper; use Test::DBI; my $testuser = TestDBI::user->retrieve( 1 ); print "user with id 1 is: ". $testuser->firstname ."\n"; print "and has roles:\n"; my @roles = $testuser->haveroles; print Dumper( \@roles ); #foreach (@roles) { # print "role: " . $_->id . " and " . $_->rolename . "\n"; #} exit;
I want to see the list of roles for the user, but, all I get is this:
user with id 1 is: John and has roles: is not a column of TestDBI::userrole at test.pl line 12 [root@posiedon3 www]#
I am sure I have done something stupid here. But the problem is that I just cannot figure out what the 50 words in the POD is actually saying! Maybe someone can point me to some more documentation.

jdtoronto


In reply to many to many with Class::DBI by jdtoronto

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



  • 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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.