【问题标题】:Cakephp How to use SQL month function in cakephp find methodCakephp如何在cakephp查找方法中使用SQL月份函数
【发布时间】:2016-08-09 09:56:38
【问题描述】:

我有一个类似的查询

SELECT count(id),DATE_FORMAT(created,'%M') from searches group by month(created)

我得到的输出像

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

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

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

第一次我收到错误 date_format() 需要参数 1。如何在 cakephp 查找方法中应用我的顶级 sql 查询? 我使用的是 cakephp 3 版本。

【问题讨论】:

    标签: cakephp cakephp-3.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 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多