[Dbix-class] Multiple joins, noob questions

Александер Пономарёв shootnix at gmail.com
Sun May 15 15:26:13 GMT 2011


An additional question:
how can I use such oerations like order by in joined table?
For example, almost the same tables
user {
    id_user,
    user_name
}
roles {
    id_role,
    role_name,
    role_rank
}
user_roles {
    id_user,
    id_role
}

First, I make
my $user = $schema->resultset('User')->find(1);
Now I can get all user roles trought user object, but what can I do to
get this roles, ordered by role_rank?

2011/5/15 Александер Пономарёв <shootnix at gmail.com>:
> Thanks to all, I was all clear!
>
> 2011/5/14 Denny <2011 at denny.me>:
>> On Sat, 2011-05-14 at 13:40 +0600, Александер Пономарёв wrote:
>>> My question is: how can I organize my schema-library to get user and
>>> his role with one step? To get $user->user_name & $user->role_name?
>>
>> Assuming (from your table structure) that a user can have multiple
>> roles, then you can't just get $user->role_name - it won't know which
>> one you want.  You could get $user->roles though, by having a
>> many_to_many relationship, and then you can search or loop through those
>> roles.  You might also want a $user->has_role() method for convenience.
>>
>> Take a look at the user / role / user_role bits of ShinyCMS for an
>> example that I think is pretty close to what you're trying to do:
>>
>> https://github.com/denny/ShinyCMS/blob/master/lib/ShinyCMS/Schema/Result/User.pm
>>
>> https://github.com/denny/ShinyCMS/blob/master/lib/ShinyCMS/Schema/Result/Role.pm
>>
>> https://github.com/denny/ShinyCMS/blob/master/lib/ShinyCMS/Schema/Result/UserRole.pm
>>
>> Hope that helps!
>>
>> Regards,
>> Denny
>>
>>
>> _______________________________________________
>> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
>> IRC: irc.perl.org#dbix-class
>> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
>> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
>>
>



More information about the DBIx-Class mailing list