Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

DBIx::Class - shortcutting relationships [resolved]

by cLive ;-) (Prior)
on Oct 04, 2012 at 18:21 UTC ( #997284=perlquestion: print w/replies, xml ) Need Help??
cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: I was being brain dead, but leaving this question up because I see no way to delete it...

If I have 3 tables (league, team and league_team), with the following relationships:

league has one to many league_team
team has one to many league_team

with league,team being the PK on the league_team table.

What I want to build is so the following will work (say) for a league):

while (my $team = $league->teams->next ) { ... }

With my current relationship across the three tables though, I have to use this:

while (my $team_league = $league->team_league->next ) { my $team = $team_league->team; ... }

I'm guessing also, that there's going to need to be an implicit create on the league_team entry wrapped in the term:


I can easily hack the interface I want, but I'm assuming that there is a correct way to do this in DBIx::Class. What is it? Anyone got a working example I can look at for inspiration?

Oh, I found this module, but I'm not quite getting it, if that is indeed the solution.

Note, refactoring the tables is not something I have access to right now.

Edit: I think I've found the term I was looking for - relationship_bridge. Now I just need to find an example, heh. The hardest part of any quest is working out what you're looking for, heh

Replies are listed 'Best First'.
Re: DBIx::Class - shortcutting relationships
by Your Mother (Chancellor) on Oct 04, 2012 at 18:42 UTC

    many_to_many detailed in DBIx::Class::Relationship is what you want, I think. FWIW, it sounds like your data is set-up well. Refactoring the tables for this would be a mistake. Fake, untested code for your case–

    { package MyApp::Schema::Result::League; # ... __PACKAGE__->has_many( league_teams => "MyApp::Schema::Result::Lea +gueTeam", "league" ); __PACKAGE__->many_to_many( teams => "league_teams", "team" ); # ... } { package MyApp::Schema::Result::Team; __PACKAGE__->has_many( league_teams => "MyApp::Schema::Result::Lea +gueTeam", "team" ); __PACKAGE__->many_to_many( leagues => "league_teams", "league" ); # ... } { package MyApp::Schema::Result::LeagueTeam; # ... __PACKAGE__->belongs_to( league => "MyApp::Schema::Result::League" + ); __PACKAGE__->belongs_to( team => "MyApp::Schema::Result::Team" ); # ... }
      Yeah, thanks. About 10 minutes after I posted it I worked it out. I was so wrapped up in a detail that I didn't think to look up many to many. Heh.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997284]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2017-07-25 15:31 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (374 votes). Check out past polls.