So I need to write SQL statement using CakePhp ORM, but I have problem how to write in Cakephp GROUP BY IF NULL(condition).
Here is SQL Statement:
SELECT COUNT(*) FROM
(
SELECT i.id
FROM items i
INNER JOIN orders o ON i.order_id = o.id
WHERE (
(i.type = 1 AND i.status = 50) OR ((i.type = 2 OR i.type = 4) AND i.status = 60))
AND i.date_of_completion IS NOT NULL
GROUP BY IFNULL(i.vessel_change_identifier, i.id)
) AS temptbl;
This is my CakePhp Query
$query = TableRegistry::get('Items')
->find('all')
->innerJoinWith('Orders', function ($q) {
return $q->where(['Orders.id' => 'Items.order_id']);
})
->Where([
'Items.type' => 1,
'Items.status' => 50,
])
->orWhere([
'Items.type IN ' => [2, 4],
'Items.status' => 60,
])
->andWhere([
'Items.date_of_completion IS NOT' => NULL
]);
$query->select([
'count' => $query->func()->count('*')
]);
Thank you!