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.


In reply to Re: Class::DBI and - possibly - complex data structures by Ovid
in thread Class::DBI and - possibly - complex data structures by Evil Attraction

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.