[Catalyst] DBIx::Class many_to_many relationship

Kutbuddin Doctor ksdoctor at sanfordburnham.org
Tue Nov 30 17:57:15 GMT 2010


In the classes you have written, you are using the same name for the =

many_to_many method from ("group") in User.pm as you have used in method =

between UserGroup to Group.

__PACKAGE__->many_to_many(group =3D> 'usergroups', 'group');

I have seen this cause problems in my hands when using it for User Role =

relationships (Authorization::Role and ACL).. I would recommend avoiding =

reusing the relationship accessor names.


On 11/30/10 8:02 AM, linuxsupport wrote:
> 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 =

> <http://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 <http://self.id>" },);
>
> Can anyone tell me how I can retrieve all the users who are member of =

> a group called 'manager'?
>
> Thanks


-- =

Kutbuddin Doctor, PhD
Bioinformatics Shared Resource,
Sanford-Burnham Medical Research Institute
http://www.sanfordburnham.org/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101130/41d74=
296/attachment.htm


More information about the Catalyst mailing list