[Dbix-class] select as, count
Matt S Trout
dbix-class at trout.me.uk
Mon Jun 26 19:47:36 CEST 2006
Sarah Berry wrote:
> Hi,
>
> I'm having trouble retrieving the columns I think I should have in a
> resultset, specifically when using count(*). I've queried the list
> archives and the DBIx::Class cookbook, but I can't seem to get this
> right.
> Here's my code:
> my @conds = {
> date => { '>' => '2006-06-21 11:59:59', '<'
> => '2006-06-22 11:59:59' },
> sensorid => { '>' => '4', '<' => '9' },
> };
> my @cnt = {
> select => [ 'DATE_FORMAT(date,%b)',
> 'DAYOFMONTH(date)', { count => '*' } ],
> as => [ 'month', 'daymonth', 'qcount' ],
> };
> my @ords = {
> order_by => 'sensorid DESC' ,
> group_by => 'sensorid'
> };
> my $rs = $schema->resultset('Tblsensorlog')->search(@conds, at cnt, at ords);
> my $choice = $rs->first;
> $c->stash->{st_record} = $choice->qcount;
>
> The last line doesn't work. I get the following message:
> Can't locate object method "qcount" via package
> "vieDB::Model::SensorLogDB::Tblsensorlog"
>
> I can change "qcount" in that last line to the name of a field in the
> table and access that field just fine. But trying any of the fields I
> named myself (month, daymonth, qcount) gives me that error.
You never asked DBIC to generate an accessor method for that field name,
though - hence there's no method on the class. You'll find
$obj->get_column('qcount');
will work fine.
--
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