[Catalyst] DBIx::Class many_to_many relationship

linuxsupport lin.support at gmail.com
Tue Nov 30 17:22:35 GMT 2010


Thanks Emmanuel, worked as I wanted to, much appreciated.

On Tue, Nov 30, 2010 at 10:25 PM, Emmanuel OTTON <otton at mines-albi.fr>wrote:

> Le 30 nov. 2010 =E0 17:02, linuxsupport a =E9crit :
>
> > Hi,
> > I am new to Catalyst and DBIx::Class, trying to use many_to_many
> > relationship.
> > I have 3 tables, users, user_groups, and group, table structure and
> > relationship are setup as follows.
> >
> > User.pm
> > __PACKAGE__->add_columns(
> >  "id",
> >  { data_type =3D> "integer", is_nullable =3D> 0 },
> >  "username",
> >  { data_type =3D> "text", is_nullable =3D> 1 },
> >  "password",
> >  { data_type =3D> "text", is_nullable =3D> 1 },
> >  "email_address",
> >  { data_type =3D> "text", is_nullable =3D> 1 },
> >  "first_name",
> >  { data_type =3D> "text", is_nullable =3D> 1 },
> >  "last_name",
> >  { data_type =3D> "text", is_nullable =3D> 1 },
> >  "active",
> >  { data_type =3D> "integer", is_nullable =3D> 1 },
> > );
> > __PACKAGE__->set_primary_key("id");
> >
> > __PACKAGE__->has_many("usergroups",
> "Example::Schema::Result::UserGroup",{
> > "foreign.user_id" =3D> "self.id" },);
> > __PACKAGE__->many_to_many(group =3D> 'usergroups', 'group');
> >
> > UserGroup.pm
> >
> > __PACKAGE__->add_columns(
> >  "user_id",
> >  { data_type =3D> "integer", is_nullable =3D> 0 },
> >  "group_id",
> >  { data_type =3D> "integer", default_value =3D> 0, is_nullable =3D> 0 },
> > );
> > __PACKAGE__->set_primary_key("user_id", "group_id");
> >
> > __PACKAGE__->belongs_to("user", "Example::Schema::Result::User", { id =
=3D>
> > "user_id" },{ join_type =3D> "LEFT" },);
> > __PACKAGE__->belongs_to("group", "Example::Schema::Result::Group", { id
> =3D>
> > "group_id" },{ join_type =3D> "LEFT" },);
> >
> > Group.pm
> >
> > __PACKAGE__->add_columns(
> >  "id",
> >  { data_type =3D> "integer", is_nullable =3D> 0 },
> >  "group",
> >  { data_type =3D> "text", is_nullable =3D> 0 },
> > );
> > __PACKAGE__->set_primary_key("id");
> >
> > __PACKAGE__->has_many("usergroup","Example::Schema::Result::UserGroup",{
> > "foreign.group_id" =3D> "self.id" },);
> >
> > Can anyone tell me how I can retrieve all the users who are member of a
> > group called 'manager'?
>
> 1 - DECLARATION
> ---------------
> First, declare your N:M link from group to user, using the method
> many_to_many, i.e. in your Group.pm, manually add this (at the end of the
> file, AFTER the line that says "DO NOT MODIFY THIS OR ANYTHING ABOVE" if =
you
> generated your classes using the DBIx::Class::Schema::Loader method
> "make_schema_at", which I highly recommend over the tedious manual method=
 ):
>
> __PACKAGE__->many_to_many('users','usergroup','user');
>
> This many_to_many method adds to any Group object an accessor called
> "users", giving directly access to the linked users.
> The 3 arguments are a name and two accessors constituting the path to be
> used:
>  - the accessor name, chosed by you (I tend to recommand the plural, which
> by the way you did not use for your "has_many" accessors)
>  - the has_many accessor leading from Group to UserGroup,
>  - the belongs_to accessor leading from UserGroup to User.
>
> 2 - USE
> -------
> Then, when you need your users (supposing there is only one group called
> 'manager'), you just have to get your Group object, and apply the just
> created accessor on it:
>
> my $manager_group =3D $schema->resultset('Group')->search( { group =3D>
> 'manager' } )->next;
> my @users_belonging_to_manager_group =3D $manager_group->users;
>
> And voila.
>
> By the way, the many_to_many method has also created a "add_to_users"
> method:
> Suppose Joe was just promoted as manager:
> my $joe =3D $schema->resultset('User')->find('Joe');
> my $manager_group =3D $schema->resultset('Group')->search( { group =3D>
> 'manager' } )->next;
> $manager_group->add_to_users($joe);
>
> The last line does the right thing(s) to link joe to the manager group.
>
> I've been using it since a long time, it works like a charm.
> --
> Emmanuel OTTON - Responsable informatique - Ecole des mines d'Albi-Carmaux
> - T=E9l: 05 63 49 30 86
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101130/e1fae=
9da/attachment.htm


More information about the Catalyst mailing list