[Kc] DBIx::Class::ResultSet / Template Toolkit / Aggregate Group By

Stephen Clouse stephenclouse at gmail.com
Wed Jun 10 11:39:35 PDT 2009


On Wed, Jun 10, 2009 at 12:15 PM, Jonathan Otsuka <djgoku at gmail.com> wrote:

>  $c->stash->{stuff} = [
>         $c->model('DB::Blah')->search(
>             {},
>             { select => ['data', { count => 'data' }], group_by => ['data
> '] }
>         )
>     ];
>

You want the "as"
attribute<http://search.cpan.org/%7Eribasushi/DBIx-Class-0.08104/lib/DBIx/Class/ResultSet.pm#as>on
your query:

$c->model('DB::Blah')->search(
    {},
    { select   => [ 'data', { count => 'data' } ],
      as       => [ 'data', 'cnt' ],
      group_by => [ 'data' ],
    },
);

And then:

$rec->data;
$rec->get_column('cnt');

Note the necessity of using get_column here, since "cnt" is not an actual
table column and thus won't have an auto-generated accessor method.

-- 
Stephen Clouse <stephenclouse at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/kc/attachments/20090610/cfc53300/attachment.html>


More information about the kc mailing list