Re: [CDBI] Normalization

[prev] [thread] [next] [Date index for 2005/11/15]

From: Matt S Trout
Subject: Re: [CDBI] Normalization
Date: 01:16 on 15 Nov 2005
On Mon, Nov 14, 2005 at 05:38:19PM -0600, Eamon Daly wrote:
> Okay, so now I'm a touch confused. Suppose I have a table
> "foo" with columns "foo_id" (the PK) and "bar_id". Table
> "bar" contains columns "bar_id" (the PK) and "bar". How do I
> define the relationship between the tables so that these
> both work:
> 
> my $bar_id = My::Foo->bar_id   # id
> my $bar_value = My::Foo->bar   # value
> 
> This gets me halfway there:
> 
> package My::Bar;
> __PACKAGE__->set_up_table('bar');
> 
> package My::Foo;
> __PACKAGE__->set_up_table('foo');
> __PACKAGE__->has_a(bar_id, 'My::Bar');
> 
> and elsewhere:
> 
> my $bar_id = My::Foo->bar_id           # id
> my $bar_value = My::Foo->bar_id->bar   # value
> 
> but like I said, I'd really prefer to just say My::Foo->bar.
> I'm sure it's trivial, but I just can't find the right
> technique.

sub bar { shift->bar_id->bar; }

Class::DBI has method proxy stuff, but only in the might_have relationship.

Under DBIx::Class you could just do

__PACKAGE__->has_a('bar_id', 'My::Bar', undef, { proxy => 'bar' });

You could probably make your own subclass of ::HasA that does the same for
Class::DBI ...

        -- 
             Matt S Trout       Specialists in Perl consulting, web development, and
  Technical Director    UNIX/Linux systems architecture and automation. Mail
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] Normalization
Eamon Daly 23:38 on 14 Nov 2005

Re: [CDBI] Normalization
William Ross 00:50 on 15 Nov 2005

Re: [CDBI] Normalization
Matt S Trout 01:16 on 15 Nov 2005

Re: [CDBI] Normalization
Eamon Daly 18:32 on 15 Nov 2005

Generated at 21:49 on 21 Nov 2005 by mariachi v0.52