如何选择至少有一个bid cakephp 3.0的所有订单?

时间:2017-08-09 18:31:18

标签: cakephp select cakephp-3.0 query-builder

有两个表格表通过一对多关系中的外键链接

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查询构建器

1 个答案:

答案 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();