我正在使用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
答案 0 :(得分:2)
尝试在->select()
值中使用数组:
->select(['COUNT(id)', 'MONTH(created)'])
In the book,它始终显示一个数组,并且它似乎没有使用您的第二个选择值。
或者,根据the book here,您可以尝试这样做:
$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)']);