You can do this with Class::DBIx and Rose::DB::Object. For example in RDBO: Assume this simple schema:
DROP TABLE public.nodes; DROP TABLE public.sequences; DROP TABLE public.taxa; CREATE TABLE public.taxa ( id SERIAL NOT NULL PRIMARY KEY , common_name CHARACTER(60) , UNIQUE (common_name) ); CREATE TABLE public.nodes ( id SERIAL NOT NULL PRIMARY KEY , description CHARACTER(60) , taxon_id INT REFERENCES taxa (id) ); CREATE TABLE public.sequences ( id SERIAL NOT NULL PRIMARY KEY , description CHARACTER(60) , seq TEXT NOT NULL , taxon_id INT REFERENCES taxa (id) );
Then this perl code:
package MyDB; use base qw(Rose::DB); __PACKAGE__->use_private_registry; __PACKAGE__->register_db( driver => 'pg', database => 'mydb', host => 'localhost', username => $ENV{USER}, password => '', ); 1; package MyDB::Object; use MyDB; use base qw(Rose::DB::Object); sub init_db { MyDB->new }; 1; package MyDB::Node; use base qw(MyDB::Object); __PACKAGE__->meta->table('nodes'); __PACKAGE__->meta->auto_initialize; __PACKAGE__->meta->make_manager_class('nodes'); 1; package MyDB::Sequence; use base qw(MyDB::Object); __PACKAGE__->meta->table('sequences'); __PACKAGE__->meta->auto_initialize; __PACKAGE__->meta->make_manager_class('sequences'); 1; package MyDB::Taxon; use base qw(MyDB::Object); __PACKAGE__->meta->table('taxa'); __PACKAGE__->meta->auto_initialize; 1; my $t = MyDB::Taxon->new(common_name => 'Homo Sapiens'); $t->add_sequences({ description => '1', seq => 'AA'}, { description => '2', seq => 'GG'},); $t->add_nodes({ description => '1', }, ); $t->save; # a little bit more complicated than necessary just # to demonstrate queries my $nodes = MyDB::Node::Manager->get_nodes( query => [ 'taxon.id' => $t->id], require_objects => [ 'taxon'], ); foreach my $node (@$nodes) { print "NODE: ". $node->description . ' ' . $node->taxon->common_name . "\n"; }; my $seqs = MyDB::Sequence::Manager->get_sequences( query => [ 'taxon_id' => $t->id], ); foreach my $seq (@$seqs) { print "SEQ: ". $seq->description . ' ' . $seq->taxon->common_name . "\n"; }; # change taxon $nodes->[0]->taxon(MyDB::Taxon->new(common_name=>'Guppy')); $nodes->[0]->save; # old taxon $nodes->[0]->taxon($t); #$nodes->[0]->save;
...does want you want I think. Or not? Is there a reason why you want to write your own OO mapper (you said you will have an underlying database)? Update: Fixed Idention

In reply to Re^3: One to many, many to one relationships by lima1
in thread One to many, many to one relationships by rvosa

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.