[英]doctrine dql array and object at same time
我有两个实体,我想从第一个实体以及整个第二个实体中获取不同元素的列表及其数量。 由于英语不好,我不确定是否可以解释。
因此,这是函数:
public function findMostPopular()
{
return $this->getEntityManager()
->createQuery("
SELECT
e,
COUNT(p.event) c,
IDENTITY(p.event) m
FROM AppBundle:Predictions p
LEFT JOIN p.event e
WHERE p.status = 'pending'
GROUP BY p.event
ORDER BY c DESC , m ASC
")
->setMaxResults(5)
->getResult()
;
}
当我尝试调用它时,出现此错误:
[语义错误]第0行,靠近'SELECT'的col -1:错误:如果没有选择至少一个根实体别名,则无法通过标识变量选择实体。
这样甚至有可能得到我想要的东西吗?
编辑:表中的示例数据:
737117017
737117017
737117017
737561075
737561075
737561075
738821787
738821787
738821787
738848055
739040139
我要计算每个不同的值。 这些是带有事件的表的外键,所以我想要事件表中的相应对象。
! c ! m ! event !
--------------------------------------------------------------
! 3 ! 737117017 ! the object from events with id 737117017 !
! 3 ! 737561075 ! the object from events with id 737561075 !
! 3 ! 738821787 ! the object from events with id 738821787 !
! 1 ! 738848055 ! the object from events with id 738848055 !
! 1 ! 739040139 ! the object from events with id 739040139 !
--------------------------------------------------------------
编辑2
我通过以下方式使其工作。 我敢肯定,这是不正确的,但是我不知道正确的方法。
SELECT
p,
e,
COUNT(p.event) c,
IDENTITY(p.event) m
FROM AppBundle:Predictions p
LEFT JOIN p.event e
WHERE p.status = 'pending'
GROUP BY p.event
ORDER BY c DESC , m ASC
根据所提到的问题,您必须更改查询中实体的顺序,因此应类似于:
public function findMostPopular()
{
return $this->getEntityManager()
->createQuery("
SELECT
e,
COUNT(p.event) c,
IDENTITY(p.event) m
FROM AppBundle:Event e
LEFT JOIN e.predictions p
WHERE p.status = 'pending'
GROUP BY p.event
ORDER BY c DESC , m ASC
")
->setMaxResults(5)
->getResult()
;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.