Issue
There are 2 table tables that are linked through a foreign key in a one-to-many relationship
table orders

and table bids

Need to select all orders to which there is at least one bid. On SQL this query is not difficult to do
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)
but need make using the cakephp 3.0 query builder
Solution
As example:
$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();
Answered By - KoBa4
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.