在我的資料庫中,我有tasks與user表相關的表。我想獲取特定狀態的任務串列,按用戶和狀態分組。這是我的查詢:
$this->createQueryBuilder('t')
->select('t.assignee, COUNT(t.id) as count, t.state')
->join('t.assignee', 'user')
->andWhere('t.state IN (:states)')
->setParameters([
'states' => array($states)
])
->addGroupBy('t.assignee')
->addGroupBy('t.state')
->getQuery()
->getResult()
不幸的是,該查詢沒有回傳正確的記錄。結果是每個用戶只有一個記錄,盡管它應該回傳一個用戶的一些記錄,按任務型別排序。你能幫我糾正我的疑問嗎?
uj5u.com熱心網友回復:
您必須先設定 groupBy 方法,然后再使用 addGroupBy 方法。
您的代碼將如下所示:
$this->createQueryBuilder('t')
->select('t.assignee, COUNT(t.id) as count, t.state')
->join('t.assignee', 'user')
->andWhere('t.state IN (:states)')
->setParameters([
'states' => array($states)
])
->groupBy('t.assignee')
->addGroupBy('t.state')
->getQuery()
->getResult()
uj5u.com熱心網友回復:
更改andWhere到where太:
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository('App:Entity')->createQueryBuilder('t');
$query
->select('t.assignee, COUNT(t.id) as count, t.state')
->join('t.assignee', 'user')
->where($query->expr()->in('t.state', array($states)))
->groupBy('t.assignee')
->addGroupBy('t.state')
->getQuery()
->getResult();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332960.html
上一篇:從查詢Doctrine回傳字串
下一篇:注釋處理程式多模塊專案Java
