Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用Symfony插件DbFinderPlugin来计算按列分组的查询?_Mysql_Plugins_Symfony1_Count - Fatal编程技术网

Mysql 如何使用Symfony插件DbFinderPlugin来计算按列分组的查询?

Mysql 如何使用Symfony插件DbFinderPlugin来计算按列分组的查询?,mysql,plugins,symfony1,count,Mysql,Plugins,Symfony1,Count,我不知道如何使用带有Symfony和spreep的DbFinderPlugin 1.2.2编写以下查询: SELECT species, COUNT(*) FROM Bird GROUP BY species; 我对这个插件还比较陌生,到目前为止我确实很喜欢它,但是这个问题一直困扰着我。我不是DBFinder的专家,但看起来下面的方法应该可以奏效 $result = DbFinder::from('Bird')-> groupBy('species')-> select

我不知道如何使用带有Symfony和spreep的DbFinderPlugin 1.2.2编写以下查询:

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


我对这个插件还比较陌生,到目前为止我确实很喜欢它,但是这个问题一直困扰着我。

我不是DBFinder的专家,但看起来下面的方法应该可以奏效

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

编辑以更改代码

结果表明,您必须使用withColumn才能获得正确的结果:

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

这返回给我:“未知模型类物种”。with()似乎想要一个模型类,而不仅仅是一个列名。如何只指定列名?我的错误。with用于连接。如果未显式完成联接,则with将以任何方式执行联接。上面编辑的代码。我真的无法在文档中找到一种更简单的方法来完成它,但它看起来应该可以工作。感谢到目前为止的帮助,现在它给了我:“添加了sfPropelFinder::withColumn()的计算列需要一个别名作为第二个参数”。我尝试过在任何地方添加别名,但我不确定在这种情况下它指的是什么。我应该意识到它需要一个别名来进行计数。编辑