Doctrine_Pager使用多个groupBy列返回错误的结果

时间:2010-11-28 02:51:21

标签: php mysql orm doctrine

我使用mySQL作为数据库进行Doctrine查询设置:

  $q = Doctrine_Query::create();
    $q->select('make.id, make.make, 
    model.id, model.year, model.model')
      ->from('Make make')
      ->innerJoin('make.Models model');     
    $q->groupBy('make.id, model.id');    // <-- this is where the problem
    $q->orderBy('make ASC');
    $q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);
    $pager = new Doctrine_Pager($q, 1, 1);  // just want one result to prove the concept
    $items = $pager->execute();

执行这三个查询(来自mysql日志):

  

SELECT COUNT(*)AS num_results FROM(SELECT mid FROM make m INNER JOIN model {{1} } ON m2m = idm2 GROUP BY make_idmidm2id

     

SELECT DISTINCT dctrn_count_querym3 FROM id make内部加入m3 model ON m4。{{1} } = m3id GROUP BY m4make_idm3id ORDER BY m4。{{1} } ASC LIMIT 1

     

选择idm3 AS makemid AS m__idm。{{1} } AS makem__makem2 AS idm2__idm2 AS year FROM m2__year { {1}} INNER JOIN m2 model ON m2__modelmake = mmodel WHERE m2。{{1 } IN('33')GROUP BY midm2make_id ORDER BY mid ASC

结果集是

m

问题在于第二个查询返回make.id,在第三个查询中用于选择1 make(Alfa-Romeo有3个模型)。我想要的是返回NUMBER OF品牌/型号组合。

如果我将Doctrine_Pager中返回的项目数量更改为2,我会得到33行(因为两个选定的产品使得Alfa-Romeo有3个型号而奥迪(下一个)有30个型号。

我在哪里弄错了?

2 个答案:

答案 0 :(得分:1)

您不能将多个列组合在一个组中。试试这个......

$q->groupBy('make.id');
$q->addGroupBy('model.id');

或稍微压缩......

$q->groupBy('make.id')->addGroupBy('model.id');

答案 1 :(得分:0)

$table->setAttribute(Doctrine_Core::ATTR_QUERY_LIMIT, Doctrine_Core::LIMIT_ROWS);

http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en