in reply to Class::DBI and - possibly - complex data structures

Your PersonName class has a bug:

PersonName->columns( All => qw(person firstname name) );

Since you don't have a 'name' field defined, that should be 'lastname'.

Why does each person have more than one name, though? If it's the case that one person can be "John Smith" and "Jane Doe", it's appropriate to have names in a separate table. If, however, you mean that one person has a first and a last name, then you probably want to move those into your person table. If that's the case, you can collapse this down to one table.

package Person; use strict; use warnings; use base 'My::DBI'; __PACKAGE__->table( 'person' ); __PACKAGE__->columns( All => qw(person_id sex modified first_name last +_name) );

If, however, a person can really have more than one name (aliases), then two tables can suffice.

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' ); package PersonName; use strict; use warnings; use base 'My::DBI'; PersonName->table( 'person_name' ); PersonName->columns( All => qw(person_name_id person_id first_name las +t_name) );

And in your actual code:

my $person = Person->retrieve( $person_id ); while ( my $name = $person->names ) { printf "%s, %s\n", $name->last_name, $name->first_name; }

It also looks like you could use a brush-up on database design. Microsoft has a decent article on Database normalization basics. It's fairly concise and easy to understand -- as much as the topic is easy to understand, that is.

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: Re: Class::DBI and - possibly - complex data structures
by Evil Attraction (Novice) on Sep 06, 2003 at 00:06 UTC
    The reason I've modelled the database like this, is to save space. I've created a utility which converts genealogical data from the Gedcom format to a MySQL-driven database. When dealing with genealogy it's fairly common to have individuals with more than one name; either you're not sure about the person's name, and want to refer to more than one names, or the person have a marriage name etc.

    That name vs. lastname "bug" was a typo, and it doesn't affect the real problem here.

    For simplicity, I could have had a name table looking like this:

    person_id mediumint unsigned not null, /* references 'person' */ firstname varchar(255) not null, lastname varchar(255) not null

    ...but that's not really what I want. As I said, I want to save disk space, and by not duplicating data in the database I save a lot of space in this example.

    However, after some reading I discovered Class::DBI::Join which seems to do what I'm after. The only problem is: I still can't get it to work. Any examples on using that class would have been great, as the example doesn't do it for me.

    Thanks again!