Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: One to many, many to one relationships

by lima1 (Curate)
on Mar 12, 2007 at 20:43 UTC ( [id://604427]=note: print w/replies, xml ) Need Help??


in reply to Re^2: One to many, many to one relationships
in thread One to many, many to one relationships

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://604427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found