Cakephp如何在cakephp中使用SQL月份函数查找方法

时间:2016-08-09 09:56:38

标签: cakephp cakephp-3.0

我有一个像

这样的查询
SELECT count(id),DATE_FORMAT(created,'%M') from searches group by month(created)

我得到的输出

count(id)   DATE_FORMAT(created,'%M')   
16             August
2              September

我在cakephp find方法中应用了这个查询,如下所示

$total = $this->Searches->find('count',[
             'fields'=>['Searches.id',DATE_FORMAT('created','%M')],
             'group' =>[month('Searches.created')]
]);

第一个我收到错误date_format()期望参数1.如何在cakephp find方法中应用我的顶级sql查询? 我正在使用cakephp 3版本。

1 个答案:

答案 0 :(得分:1)

查询构建器有一个func()方法,允许您使用SQL函数。

http://book.cakephp.org/3.0/en/orm/query-builder.html#using-sql-functions

$time = $query->func()->date_format([
    'created' => 'identifier',
    "'%M'" => 'literal'
]);

然后,您可以在查询中使用该$time变量。