有两个表格表通过一对多关系中的外键链接
表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查询构建器
答案 0 :(得分:0)
例如:
$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();