将SQL查询转换为Doctrine 1.2 dql

时间:2015-02-03 09:48:58

标签: mysql symfony1 doctrine-1.2

我在Mysql表中有一个select查询,用于获取特定列(日期)中重复值的相关表。如果列中有重复值,这将成功显示列及其外键。示例两行相同的值(2014-11-10)在日期栏中

mysql>select man_id,date_created,count(date_created) as count
from collections
group by man_id,date_created
having count(date_created) > 1;

我希望此查询转换为Doctrine查询,因​​为我使用symfony 1.4作为框架

 public function getDuplicateDatePayment() {
    $q = $this->createQuery()
              ->select('man_id','date_created','count(date_created) as count')
              ->from('Collections')
              ->groupBy('man_id','date_created')
              ->having('COUNT(c.date_created) > 1');
        return $q->execute();
}

SELECT c.id AS c__id, c.man_id AS c__man_id FROM collections c GROUP BY c.man_id HAVING count(c.date_created) > 1 //result 1 row

为什么doctrine查询不能按预期显示结果?如何转换所述doctrine查询以使其显示类似于SQL的结果?

mysql command line

//结果1141行

更新

集合表与一对多关系中的Man表有关。我必须使用innerJoin吗?

1 个答案:

答案 0 :(得分:0)

经过两天的尝试,寻找答案,我终于找到了解决问题的方法。

SELECT c.id AS c__id, c.man_id AS c__man_id FROM collections c GROUP BY c.man_id HAVING count(c.date_created) > 1 

上述sql结果来自doctrine的问题是,它在groupBy子句中不包含'c.loan_id',即使我将它包含在groupBy子句中。解决方法是,只需添加一个单独的addGroupBy子句..现在它的working.cheers ..

public function getDuplicateDatePayment() {
$q = $this->createQuery()
          ->select('c.man_id','c.date_created','count(c.date_created) as count')
          ->from('Collections')
          ->groupBy('c.man_id')
          ->addGroupBy('c.date_created')
          ->having('count(c.date_created) > 1');
    return $q->execute();

}