cakephp 3中按月按月选择记录组



我正在使用 CakePHP 3.x+

我必须在页面上显示图形,因此要为此构建脚本。

我必须按照本年度按月选择记录组的计数。

这是我尝试的。

$graph = $this->GenerateVideos->find()
        ->select('COUNT(id)', 'MONTH(created)')
        ->where(['YEAR(created)' => date('Y')])
        ->group(['MONTH(created)']);

生成SQL喜欢

'sql' => 'SELECT GenerateVideos.COUNT(id) AS GenerateVideos__COUNT(`id`) FROM generate_videos GenerateVideos WHERE YEAR(created) = :c0 GROUP BY MONTH(created) ',
'params' => [
    ':c0' => [
        'value' => '2018',
        'type' => null,
        'placeholder' => 'c0'
    ]
],

,但这给出了错误,例如

Error: SQLSTATE[42000]: Syntax error or access violation: 
1064 You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near '(`id`) 
FROM generate_videos GenerateVideos WHERE YEAR(created) = '2018' GROUP BY' at line 1 

尝试在您的->select()值中使用数组:

->select(['COUNT(id)', 'MONTH(created)'])

在书中,它总是显示一个数组,并且似乎没有使用您的第二个选择值。

或,根据这本书,您可以尝试以下方式:

$query = $this->GenerateVideos->find();
$query->select(['count' => $query->func()->count('id'), 'month' => 'MONTH(created)']);
$query->where(['YEAR(created)' => date('Y')])
$query->group(['month' => 'MONTH(created)']);

最新更新