by so many now, that I fell down on my kneed and tried it out properly. I've been fiddling with it previously too, but didn't like the outcome performance-wise. Anyway - I'm ready to give a new try.
My little test project deserves some help, though, as I'm having problems dealing with "complex" data structures. They aren't that complex, really, but putting them into a
I have these tables:
person
person_id mediumint unsigned auto_increment primary key,
sex char(1) default '?' not null,
modified date not null
name
name_id mediumint unsigned auto_increment primary key,
name varchar(255) not null
person_name
person mediumint unsigned not null, /* references 'person' */
firstname mediumint unsigned not null, /* references 'name' */
lastname mediumint unsigned not null /* references 'name' */
As you can see, one Person can have more than one name. One extra question; it seems like
Class::DBI forces you to name references without any ending '_id' if you want some nifty named methods in your classes?
Anyways - here are my classes;
Person.pm
package Person;
use strict;
use warnings;
use base 'My::DBI';
Person->table( 'person' );
Person->columns( All => qw(person_id sex modified) );
Person->has_many( names => 'PersonName' );
1;
Name.pm
package Name;
use strict;
use warnings;
use base 'My::DBI';
Name->table( 'name' );
Name->columns( All => qw(name_id name) );
1;
PersonName
package PersonName;
use strict;
use warnings;
use base 'My::DBI';
PersonName->table( 'person_name' );
PersonName->columns( All => qw(person firstname name) );
PersonName->has_a( firstname => 'Name' );
PersonName->has_a( lastname => 'Name' );
My script might look something like this:
#!/usr/bin/perl
#
use strict;
use warnings;
use Person;
my $Person = Person->retrieve( 1 );
my $names = $Person->names();
while ( my $Name = $names->next() ) {
print $Name->lastname()->name() . ', ' . $Name->firstname()->name(
+) . "\n";
}
It works
almost, except that even though the person has 2 different names, only one of them gets listed (twice).
Any ideas?