doctrine dbal 获取每个组的最新聊天消息

doctrine dbal get latest chat message per group

尝试制作多个时 select

$result = $this->qb->select('c.id','c.message','acc.name as chat_from', 'c.chat_to as count')
    ->addSelect("SELECT * FROM chat ORDER BY date_deliver DESC")
        ->from($this->table,'c')
        ->join('c','account','acc', 'c.chat_from = acc.id')
        ->orderBy('date_sent','DESC')
        ->groupby('chat_from')
        ->where('chat_to ='.$id)
        ->execute();
        return $result->fetchAll();

我也试过

$result = $this->qb->select('c.id','c.message','acc.name as chat_from', 'c.chat_to as count')
        ->from("SELECT * FROM chat ORDER BY date_deliver DESC",'c')
        ->join('c','account','acc', 'c.chat_from = acc.id')
        ->orderBy('date_sent','DESC')
        ->groupby('chat_from')
        ->where('chat_to ='.$id)
        ->execute();
        return $result->fetchAll();

我想按组显示数据,然后显示最后一个条目中的数据。

我'使用了 DOCTRINE DBAL

请帮帮我

由于您的问题不清楚,所以我假设您需要获取每组最近的 chat/message,等效的 SQL 将是

 SELECT c.id, c.message, a.name as chat_from, c.chat_to as count
 FROM account a
 JOIN chat c ON(c.chat_from = a.id )
 LEFT JOIN chat cc ON(c.chat_from = cc.chat_from AND c.date_sent < cc.date_sent)
 WHERE cc.date_sent IS NULL AND c.chat_to = @id
 ORDER BY c.date_sent DESC

所以使用 doctrine dbal 你可以将上面的查询写成

$this->qb->select( 'c.id', 'c.message', 'a.name as chat_from', 'c.chat_to as count' )
         ->from( 'account', 'a' )
         ->join( 'a', 'chat', 'c', 'c.chat_from = a.id' )
         ->leftJoin( 'c', 'chat', 'cc', 'c.chat_from = cc.chat_from AND c.date_sent < cc.date_sent' )
         ->where( 'cc.date_sent IS NULL' )
         ->andWhere( 'c.chat_to =' . $id )
         ->orderBy( 'c.date_sent', 'DESC' )
         ->execute();

同样,如果不查看样本数据和 DDL,它不是一个完整的解决方案。