Cake 3x:- 如何在查找查询中使用计数和求和

Cake 3x:- how to use count and sum in find query

我想 select 计算 "id" 和 cakephp 中的价格总和-3.x 简单 mysql 查询:-

SELECT COUNT(id) as count, SUM(price) as total_price FROM bookings WHERE id=3;

我不知道如何在cakephp中写这个查询-3.x

请告诉我如何在 cakephp 中编写此查询-3.x

首先尝试阅读如何使用 ORM 的查询生成器:

Query Builder - Aggregates Group and Having

会是这样的

$query = $bookings->find();
$query->select([
    'count' => $query->func()->count('id'),
    'total_price' => $query->func()->sum('price')
])->where(['id' => 3]); 

计数将来自结果数或通过将计数添加到查询构建器中!