[Dbix-class] Using joins and aggregate functions with DBIC
Matt S Trout
dbix-class at trout.me.uk
Sat May 27 00:41:52 CEST 2006
Nilson Santos Figueiredo Junior wrote:
> 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.
Sadly, you quit IRC 10 seconds before I hit enter on the answer to this :)
> 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) = @_;
my $alias = $self->{attrs}{alias}
> $self->next::method($cond, $attrs)->next::method(
> {},
> {
> join => [qw/logs/],
select => [(map {"${alias}.${_}"}
> $self->result_source->columns), {sum => 'logs.hours_spent'} ],
> as => [$self->result_source->columns, qw/hours_spent/],
group_by => [ "${alias}.id" ]
> }
> );
> }
>
> 1;
Presto.
More information about the Dbix-class
mailing list