[Dbix-class] Multiple joins, noob questions

Robert Kinyon rob.kinyon at gmail.com
Sun May 15 21:23:04 GMT 2011


On Sun, May 15, 2011 at 11:26, Александер Пономарёв <shootnix at gmail.com> wrote:
> 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?

my @ordered_roles = map { $_->role } $user->user_roles( {}, { order_by
=> 'role_rank' } );

The key is that the accessor for a has-many relationship returns a
resultset. In list context, that returns the rows in the resultset. In
scalar context, it returns the resultset. As a result, you can pass in
all the arguments to ->search() because that's what is being called
under the covers.

Rob



More information about the DBIx-Class mailing list