I have a pretty complex (fancy?) query, which is working in pure sql but I need to translate it to be used with cakephp 3.0's orm.
This is the query:
select user_id, count(id)/period_diff(date_format(now(), '%Y%m'),
date_format(sent, '%Y%m')) AS average from invoices
WHERE sent != '0000-00-00' GROUP BY user_id
And this is what I was trying:
$monthly = $this->find()
->select(function ($query) {
return [
'user_id',
'average' => $query->func()->extract(
'COUNT(Invoices.id)/PERIOD_DIFF(date_format(now(), "%Y%m"),
date_format(Invoices.sent, "%Y%m"))'
)
];
})
->where([
"Invoices.sent !=" => "0000-00-00",
])
->group('user_id');
The problem is, the extract method (which I'm not sure it's what I actually need) is available since CakePHP 3.1, but I'm using 3.0.
Is it the correct method? Can I make it work with 3.0 somehow, or is there something else I can use?