[CDBI] Normalization

Matt S Trout dbix-class at trout.me.uk
Tue Nov 15 01:16:07 GMT 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/ +

More information about the ClassDBI mailing list