[Catalyst] DBIx::Class many_to_many relationship

Steve steve at matsch.com
Tue Nov 30 16:15:23 GMT 2010


I recommend you create a 'Example::Schema::ResultSet::Group' resultset 
class.  Presumably
you already have a 'Result' directory where your table classes reside, 
so create a ResultSet
directory (if you don't already have one).

Look here: 
http://search.cpan.org/~frew/DBIx-Class-0.08124/lib/DBIx/Class/ResultSet.pm 
<http://search.cpan.org/%7Efrew/DBIx-Class-0.08124/lib/DBIx/Class/ResultSet.pm>for 
direction,
but essentially this is where you would set up queries pertaining to 
your 'Groups'.

You'll do a search of all the related Users having a group name = 
'manager' or something
similar.

Steve
On 11/30/2010 11: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 => "integer", is_nullable => 0 },
>   "username",
>   { data_type => "text", is_nullable => 1 },
>   "password",
>   { data_type => "text", is_nullable => 1 },
>   "email_address",
>   { data_type => "text", is_nullable => 1 },
>   "first_name",
>   { data_type => "text", is_nullable => 1 },
>   "last_name",
>   { data_type => "text", is_nullable => 1 },
>   "active",
>   { data_type => "integer", is_nullable => 1 },
> );
> __PACKAGE__->set_primary_key("id");
>
> __PACKAGE__->has_many("usergroups", 
> "Example::Schema::Result::UserGroup",{ "foreign.user_id" => "self.id 
> <http://self.id>" },);
> __PACKAGE__->many_to_many(group => 'usergroups', 'group');
>
> UserGroup.pm
>
> __PACKAGE__->add_columns(
>   "user_id",
>   { data_type => "integer", is_nullable => 0 },
>   "group_id",
>   { data_type => "integer", default_value => 0, is_nullable => 0 },
> );
> __PACKAGE__->set_primary_key("user_id", "group_id");
>
> __PACKAGE__->belongs_to("user", "Example::Schema::Result::User", { id 
> => "user_id" },{ join_type => "LEFT" },);
> __PACKAGE__->belongs_to("group", "Example::Schema::Result::Group", { 
> id => "group_id" },{ join_type => "LEFT" },);
>
> Group.pm
>
> __PACKAGE__->add_columns(
>   "id",
>   { data_type => "integer", is_nullable => 0 },
>   "group",
>   { data_type => "text", is_nullable => 0 },
> );
> __PACKAGE__->set_primary_key("id");
>
> __PACKAGE__->has_many("usergroup","Example::Schema::Result::UserGroup",{ 
> "foreign.group_id" => "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
>
>
> _______________________________________________
> 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/



More information about the Catalyst mailing list