[Vienna-pm] Class::DBI pod/doc issues and question for m2m +might_have

matthias tarasiewicz parasew at sonance.net
Mon Sep 29 21:17:23 CDT 2003

for having a good usage of Class::DBI, as far as i understood it you 
have to reference the classes the way you want to use them.
this means that the has_a and has_many does not need to be the same as 
your references in the database. they are much more a tool to get the 
data in a way you want (Class::DBI is creating SQL queries, joins out of 
the has_a and has_many methods you define).

Class::DBI is very poor documented, at least the main parts are not 
clear to me since the underlying SQL-structure for the examples is not 
visible to the user. issues like correct naming of Classes and 
database-tables become a problem (if you don't know how Class::DBI deals 
with that).

i still didn't get the clue about how to use the Mapping described in 
the Class::DBI pod, and also still no clue about might_have. if anyone 
could describe me the both, i would be very happy :)

at least i know how to use has_a and has_many correctly (at least i 
think so, any hints and better ways very welcome ;)
(see the perlmonks-thread-link below)

On Fri, Sep 05, 2003 at 03:45:58PM -0400, Perrin Harkins wrote:
 > Since has_many already works as a join when there is no linking table,
 > the only issue here is handling many-to-many. I think this should be a
 > separate method, because I find the current syntax for it (with the
 > array ref in the middle) confusing.
 > That would mean creating a new
 > method with no backwards compatibility baggage, and it could have a
 > clean syntax for specifying the join table, and possibly even accept
 > an> option to either use a join or do what the current has_many does
 > for> linking tables.

this seems to be a good idea to me since the array ref for many-to-many 
relations is indeed confusing (i didn't get this running still)

for has_a compared to has_many i found it strange that one is depending 
on correct naming (has_a), the other one completely not.

(see my posting on the perlmonks thread for more around that
http://perlmonks.org/index.pl?node_id=295028 )

matthias tarasiewicz


-------------- next part --------------
# this is just a suggestion about how the examples on the Class::DBI pod-manpage
# how would you do m:m mapping?
# see the examples here 
# http://search.cpan.org/~tmtm/Class-DBI-0.94/lib/Class/DBI.pm#Mapping

# and the correct calling would be like this?
my $t=Music::CD->retrieve(1);
my @test=$t->styles;

# if i have this SQL (mysql)
# id		mediumint(7)  NOT NULL auto_increment,
# firstname 	varchar(25) NOT NULL,
# ) Type=MyISAM;
# CREATE TABLE user_role (
# entry_id	mediumint(7) NOT NULL,
# user_id		mediumint(7) NOT NULL,
# role_id		smallint(6) NOT NULL,
# ) Type=MyISAM;
# id		smallint(6) NOT NULL auto_increment, 
# title		varchar(50) NOT NULL,
# diz		varchar(50) NOT NULL,
# ) Type=MyISAM;
# CREATE TABLE entry (
# id		mediumint(7) NOT NULL auto_increment,
# txt		text,
# ) TYPE=MyISAM;default NULL, 

# so i am wondering how to make a m:m mapping at least to get 
# the roles a user would have.
# i didn't suceed yet.

 require DBD::mysql;
  use strict;
  use warnings;
  #require Class::DBI::mysql;

  use vars qw($VERSION);
  $VERSION = '0.1b';

package FsDBI;
  use base ('Class::DBI');
  FsDBI->set_db('Main', 'DBI:mysql:dbname', 'user', 'pass');

package FsDBI::User;
  use base ('FsDBI');
  FsDBI::User ->table  ('user');
  FsDBI::User ->columns (Essential => qw(id firstname) );

# ???
#  FsDBI::User ->has_many (Roles  => ['FsDBI::User::user_role' => 'role_id']);
#  FsDBI::User ->has_many(Entries   => 'FsDBI::Entry', 'owner');

package FsDBI::User::user_role;
  use base ('FsDBI');
  FsDBI::User::user_role ->table  ('user_role');
  FsDBI::User::user_role ->columns(Essential => qw(user_id role_id));

#  FsDBI::User::user_role ->has_many(Users    =>'FsDBI::User','id');
#  FsDBI::User::user_role ->has_many(Roles    =>'FsDBI::User::Role', 'id');

package FsDBI::User::Role;
  use base ('FsDBI');
  FsDBI::User::Role ->table  ('role');
  FsDBI::User::Role ->columns(Essential  => qw( id title diz));

More information about the Vienna-pm mailing list