[Dbix-class] many to many self join?

Richard Jolly Richard.Jolly at bbc.co.uk
Tue Oct 18 19:27:39 CEST 2005


Hello DBIx::Class,

I'm having difficulty with a many-to-many self join:

TABLE A
- id

TABLE AA
- id
- parent_id
- child_id

I'm trying:

package A;

__PACKAGE__->has_many(
  'a_a', # relationship name
  'AA', # class of join table
  { 'foreign.parent_id' => 'self.id' }
);

sub children {
  my $self = shift;
  A->search(
      { 'a_a.parent_id' => $self->id },
      { join => { 'a_a' => 'child_id' }
  );

package AA;

__PACKAGE__->belongs_to(
  'parent_id' => 'A',
        );

__PACKAGE__->belongs_to(
  'child_id' => 'A',
        );

It looks like the join loops back on itself. Am I on the right lines?

By inspecting 'a_a' I seem to be able to get into the join table ok,
but I have problems getting from the join table to the child objects.

I guess I don't understand what the join clause in the 'children' method
does :-(



http://www.bbc.co.uk/

This e-mail (and any attachments) is confidential and may contain
personal views which are not the views of the BBC unless specifically
stated.
If you have received it in error, please delete it from your system. 
Do not use, copy or disclose the information in any way nor act in
reliance on it and notify the sender immediately. Please note that the
BBC monitors e-mails sent or received. 
Further communication will signify your consent to this.



More information about the Dbix-class mailing list