[Dbix-class] Chaining multiple many-to-many relations

Emmanuel OTTON otton at mines-albi.fr
Wed Dec 15 17:16:15 GMT 2010


Le 15 déc. 2010 à 16:18, Jean-François Stenuit a écrit :

> Hi DBIC experts,
> 
> Maybe a dumb question, maybe I'm doing it wrong, but I don't seem to
> be able to chain two many-to-many relations.
> 
> I'm re-writing (OO style) a RBAC system, where a user is part of a
> group, and each group has access to a set of applications :
> 
> user ----many-to-many---- group ----many-to-many----- application
> 
> I'd like to use a shortcut of the form :
> my $rsApplications=$rowUser->groups->applications
> 
> .../...
> Another option would be to create a many-to-many relationship that
> spawns all five tables ?

Yes, this is a good option; in the user-serviceable part of your User.pm class:

# You already have this one:
__PACKAGE__->many_to_many("groups", "user_groups", "gid");
 
# Just add this accessor, the map function does the expansion:
sub applications {
     my $user = shift;
     return map ( $_->applications , $user->groupes ) ;
}

Then, in your app, just call your accessor:

my @applications_for_this_user = $user->applications;

--
Emmanuel OTTON - Responsable informatique - Ecole des mines d'Albi-Carmaux - Tél: 05 63 49 30 86




More information about the DBIx-Class mailing list