简体   繁体   English

原则dql数组和对象同时

[英]doctrine dql array and object at same time

I have two entities and I want to get a list of distinct elements and their count from the first entity, and the whole second entity as objects. 我有两个实体,我想从第一个实体以及整个第二个实体中获取不同元素的列表及其数量。 I'm not sure if I can explain it, because of my bad english. 由于英语不好,我不确定是否可以解释。

So, this is the function: 因此,这是函数:

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()
    ;
}

When I try to call it, I'm getting this error: 当我尝试调用它时,出现此错误:

[Semantical Error] line 0, col -1 near ' SELECT ': Error: Cannot select entity through identification variables without choosing at least one root entity alias. [语义错误]第0行,靠近'SELECT'的col -1:错误:如果没有选择至少一个根实体别名,则无法通过标识变量选择实体。

Is it even possible this way to get what I want? 这样甚至有可能得到我想要的东西吗?

Edit: Example data in the table: 编辑:表中的示例数据:

737117017
737117017
737117017
737561075
737561075
737561075
738821787
738821787
738821787
738848055
739040139

I want count of each distinct value. 我要计算每个不同的值。 These are the foreign keys to the table with events, so I want the corresponding object from events table. 这些是带有事件的表的外键,所以我想要事件表中的相应对象。

! 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  !
--------------------------------------------------------------

Edit 2 编辑2

I got it work by following way. 我通过以下方式使其工作。 I'm sure, it's not correct, but I don't know the right way. 我敢肯定,这是不正确的,但是我不知道正确的方法。

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

According to the referred problem you must change the order of your entities in the query, so it should be something like: 根据所提到的问题,您必须更改查询中实体的顺序,因此应类似于:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM