Hello,
Can someone help me with this? I'm trying to make the use of Class::DBI and i
need to do a
many-to-many relationship. (an user has many groups and a group has many users,
and an user with a
group has many history.)
I think what i make was correct, i can access the group_name value of the
relationship
(users->groups), but i can access the history of that USER with that GROUP.
below is my tables and the classes that i make.
Thank you very much, any way.
Rodrigo Zadra.
<table>
+---------+ +-------------+
| users | | groups |
+---------+ +-------------+
|*id_user |-+ +--| *id_group |
| login | | | | group_name |
+---------+ | | +-------------+
| |
+--------+ |
| +------------+
| +---------------+ |
| | relationship | |
| +---------------+ |
+---<| *id_user | |
| *id_group |>---+
+---------------+
| |
| |
/|\ /|\
+-----------------+ +--------------+
| history | | action |
+-----------------+ +--------------+
| *id_user | | *id_action |
| *id_group | +--| action |
| id_action |--+ +--------------+
| date |
+-----------------+
</table>
and this classes defined:
<code>
package dbBase;
use base qw/Class::DBI/;
use base qw(Class::DBI);
__PACKAGE__->set_db( "Main", "dbi:mysql:Test", "TEST_USER", "TESTPASS"
);
1;
package dbUser;
use lib '/home/rodza/perl_tests';
use base 'dbBase';
dbUser->table('users');
dbUser->columns( All => qw/id_user login/ );
dbUser->has_many( groups => [dbRelationship => 'id_group'] => 'id_user' );
1;
package dbGroup;
use lib '/home/rodza/perl_tests';
use base 'dbBase';
dbGroup->table('groups');
dbGroup->columns( All => qw/id_group group_name/ );
dbUser->has_many( users => [dbRelationship => 'id_user'] => 'id_group' );
1;
package dbRelationship;
use lib '/home/rodza/perl_tests';
use base 'dbBase';
dbRelationship->table('relationship');
dbRelationship->columns( All => qw/id_user id_group/ );
dbRelationship->has_a( id_group => dbGroup );
dbRelationship->has_a( id_user => dbUser );
dbRelationship->has_many( history => dbHistory );
1;
package dbHistory;
use lib '/home/rodza/perl_tests';
use base 'dbBase';
dbHistory->table('history');
dbHistory->columns( All => qw/id_user id_group action date/ );
dbHistory->has_a( id_action => dbAction );
1;
package dbAction;
use lib '/home/rodza/perl_tests';
use base 'dbBase';
dbAction->table('action');
dbAction->columns( All => qw/id_action action/ );
1;
package main;
use dbUser;
my ($info) = dbUser->retrieve(1);
foreach($info){
printf "UID: %d LOGIN: %S\nGID: %D GROUP_NAME: %S",
$info->id_user, $info->login, $info->id_group->group_name;
}
</code>
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>