DQL:仅来自客户的最后一个条目

DQL: only last entry from a customer

我正在用 DQL 编写一个查询,我想在其中 select 来自客户的最新订单。目前我只能设法获取所有订单并根据日期降序获取它们。这意味着我需要过滤它以针对每个用户以及只选择他们的最新条目。但是,我对 DQL 和一般查询的了解并没有那么高,我被卡住了。任何有关如何继续我的查询的帮助将不胜感激。

public function getLatestOrder($customer){
    // get the latest order from a customer

    $em = $this->getEntityManager();

    $q1 = $em->getRepository('AppBundle:Order')->createQueryBuilder('o')
    ->leftjoin("AppBundle:User", "u")
    ->where("u.id = :customer_user_id")
    ->setParameter('customer_user_id', $customer)
    ->orderBy('o.date', 'DESC' );

    $q1 = $q1->getQuery();
    $res1 = $q1->getResult();



    return $res1;       
}

附加信息:订单实体有一个引用用户信息的客户列。

您可以使用子查询而不是更好地连接。

你可以试试这个:

$em->getRepository('AppBundle:Order')->createQueryBuilder('o')
->leftjoin("AppBundle:User", "u")
->leftjoin("AppBundle:Order", "laterOrder", 'WITH', 'laterOrder.date > o.date')
->where("u.id = :customer_user_id")
->andWhere("laterOrder is null")