[Dbix-class] Using joins and aggregate functions with DBIC

Nilson Santos Figueiredo Junior acid06 at gmail.com
Sat May 27 00:36:32 CEST 2006


On 5/26/06, Nilson Santos Figueiredo Junior <acid06 at gmail.com> wrote:
> I'm trying to get two things:
> 1 - the sum of a given field in all "task_logs" given a "task"
> 2 - the sum of a given field in all "task_logs" given a "task" and an "user"

I've managed to solve number 1.
However, number 2 is still not working. I thought that this would work
automagically when using user relationship accessor ($user->tasks),
but it didn't. The problems seems to be with the "me.*" magic I've
tried to use.

Here's the code I've got so far.
Since it solves the first issue, it might be useful for someone else
(and it might also help you to help me with the second issue ;) ).

package MyTasks::Schema::ResultSet::Task;
use strict;
use warnings;
use base 'DBIx::Class::ResultSet';

sub search {
    my ($self, $cond, $attrs) = @_;
    $self->next::method($cond, $attrs)->next::method(
        {},
        {
            join     => [qw/logs/],
            select   => [(map {"me.$_"}
$self->result_source->columns), {sum => 'logs.hours_spent'} ],
            as       => [$self->result_source->columns, qw/hours_spent/],
            group_by => [qw/me.id/]
        }
    );
}

1;

-Nilson Santos F. Jr.



More information about the Dbix-class mailing list