0

I am porting an application from Class::DBI to DBIx::Class and need help. I have a table T with a primary key tid and another table ChildT that relates a row of T to multiple (child) rows of T itself. How can I setup the relations between T and ChildT so that I can find all children of an instance of T. Here are the stripped down versions of these two tables:

T: (id, name);
ChildT: (rowid, tid, childid)

tid and childid both reference the id column of T.

Thanks!

Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43
  • Did you read the DBIx::Class::Manual - especially the Cookbook, which has various examples of doing many-to-many relations? Did what is explained in there not work for you? What was the error you got? – rafl Oct 31 '10 at 10:50
  • I did. The Cookbook and the Relationship docs explain how to do it when the link table contains foreign keys of two distinct tables. So I know how to do it for Actor -> ActorRole <- Role. What I want is to setup something for (say) Actor -> CoStar <- Actor. Should I set up two belongs_to relationships in the Costar Class - one for each foreign key and two has_many relations from Actor to Costar class? Confused! – Gurunandan Bhat Oct 31 '10 at 11:57

1 Answers1

0

I am answering my own question.

The DBIx::Class::Relationship docs explain this clearly enough. In the ChildT Class define a belongs_to relationship to T using the foreign key childid:

__PACKAGE__->belongs_to(parent => 'App::Schema::Result::T', 'childid');

In the T Class define a has_many and a many_to_many relation with the ChildT class:

__PACKAGE__->has_many(childrecords => 'App::Schema::Result::ChildT', 'tid');
__PACKAGE__->many_to_many(children => 'childrecords', 'parent');

With this $t->children gives all child records of any instance of T.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gurunandan Bhat
  • 3,544
  • 3
  • 31
  • 43