[Dbix-class] Problem with getting a grouped count

Matt S Trout dbix-class at trout.me.uk
Sun Mar 19 22:50:52 CET 2006


Matija Grabnar wrote:
> I've been trying to follow the example made in the cookbook, but for some reason, the "as"=> part doesn't seem
> to be working:
> 
> my $qs=$schema->resultset('Schema::Msgs')->search({},
>      {select   => [ 'quar_type',{ count => '*' } ],
>       join     => [ qw/quarantined/],
>       as       => [ qw/quar_type qcount/],
>       group_by => [ qw/quar_type/],
>      });
> 
> Here is the actualy query that gets executed:
> 
> SELECT quar_type, COUNT( * ) FROM msgs me LEFT JOIN quarantine quarantined ON ( quarantined.mail_id = me.mail_id ) GROUP BY quar_type
> 
> The problem is that since the as part doesn't execute, I can't do an order_by /qcount/, and what's even
> worse, I can't get to the count!

You *can* get to the count. DBIC will populate a 'qcount' column in the 
inflated object, so

$obj->get_column('qcount')

will work fine.

As noted in the ResultSet docs

        as

        Arguments: (arrayref)

        Indicates column names for object inflation. This is used
        in conjunction with "select", usually when "select" con­
        tains one or more function or stored procedure names:

the 'as' attribute does *not* translate to SQL. It's for the DBIC stuff only.

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list