【问题标题】:How to select all orders for which there is at least one bid cakephp 3.0?如何选择至少有一个出价 cakephp 3.0 的所有订单?
【发布时间】:2017-08-09 18:31:17
【问题描述】:
有 2 个表表通过一对多关系中的外键链接
表orders
和表bids
需要选择至少有一个出价的所有订单。
在 SQL 上,这个查询并不难做
SELECT * , count(bids.order_id) AS count_bid
FROM orders
LEFT JOIN bids ON bids.order_id = orders.id
GROUP BY orders.id
HAVING (count_bid > 0)
但需要使用 cakephp 3.0 查询生成器
【问题讨论】:
标签:
cakephp
select
cakephp-3.0
query-builder
【解决方案1】:
例如:
$query = $this->Articles->find();
$query
->select(['count_bid' => $query->func()->count('Comments.id')])
->leftJoinWith('Comments')
->group(['Articles.id'])
->having(['count_bid >' => 0])
->enableAutoFields(true);
$results = $query->toArray();