Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

DBIx::Class - shortcutting relationships [resolved]

by cLive ;-) (Parson)
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:

$league->team->create()

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

Comment on DBIx::Class - shortcutting relationships [resolved]
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997284]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2015-07-05 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls