I've been recommended Class::DBI 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 Class::DBI context seems more than trivial.
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?

In reply to 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.