The solution I've found for this type of queries is to select using a MySQL query alias with AS
or refer to the model FQCN, PHQL has a weird behavior sometimes... here is how I do it:
$reports = $di['modelsManager']->createBuilder()
->from(['r' => Reports::class])
->groupBy('MONTH(r.created_at)')
->getQuery()
->execute()
;
Or if you want to use Model::find()
:
$reports = Reports::find([
'group' => 'MONTH('.Reports::class.'.created_at)'
]);
If you are not using namespaced model classes is not needed to use Reports::class
, simply Reports