我有一个非常复杂(花哨的?)查询,它在纯sql中工作,但我需要翻译它以与cakephp 3.0' s orm一起使用。
这是查询:
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
这就是我的尝试:
$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');
问题是,提取方法(我不确定它是我真正需要的)可用since CakePHP 3.1,但我使用的是3.0。
这是正确的方法吗?我可以以某种方式使用它,或者还有其他我可以使用的东西吗?