views:

150

answers:

2

I cannot figure out how to write the following query using the DbFinderPlugin 1.2.2 with Symfony and Propel:

SELECT species, COUNT(*) FROM Bird GROUP BY species;

Here is the DbFinderPlugin page

I am rather new to the plugin, and I do love it so far, but this query has so far stumped me.

+1  A: 

I'm not an expert at DBFinder but it looks like the following should work

$result = DbFinder::from('Bird')->
  groupBy('species')-> 
  select(array('species', 'count(*) cnt'))->
  find();

Edited to change code

Jonathan Fingland
This returns me: "Unknown model class species". It seems that with() seems to want a model class and not just a column name. How do I specify just a column name?
Failpunk
my bad. with is for use with joins. if a join is not explicitly done, with will do one anyways. edited code above. I really couldn't find an easier way to do it in the docs, but it looks like it should work
Jonathan Fingland
Thanks for the help so far, now it gives me: "Calculated colums added with sfPropelFinder::withColumn() need an alias as second parameter". I've tried adding aliases everywhere, but I'm just not sure what it's referring to in this situation.
Failpunk
I should have realized it would need an alias for the count. edited
Jonathan Fingland
A: 

It turns out that you have to use withColumn to get the proper result:

$result = DbFinder::from('Bird')
    ->withColumn('count(Bird.Id)', 'total_birds')
    ->groupBy(species')
    ->find();
Failpunk