I am trying to group by Year in cakephp3. I am able to get data using following way, but it's not still grouped by year(The way I want.)
$query = $this->Alerts->query();
$year = $query->func()->year([
'added_on' => 'literal'
]);
$month = $query->func()->monthname(['added_on' => 'literal']);
$monthAlertsCount = $query->func()->count($month);
$data = $query
->select([
'year' => $year,
'month' => $month,
'count' => $monthAlertsCount
])
->group($year)
->group($month);
$status = "success";
$this->set('response', $status);
$this->set('year', $data);
$this->set('_serialize', ['response','year']);
Current output is as follows:
{
"response":"success",
"year":[
{
"year":"2013",
"month":"November",
"count":"1"
},
{
"year":"2014",
"month":"February",
"count":"2"
},
{
"year":"2014",
"month":"January",
"count":"3"
},
{
"year":"2015",
"month":"December",
"count":"6"
},
{
"year":"2015",
"month":"February",
"count":"3"
},
{
"year":"2015",
"month":"January",
"count":"4"
},
{
"year":"2016",
"month":"January",
"count":"83"
}
]
}
Expected Output:
{
"response":"success",
"year":[
{
"2013":[
{
"month":"November",
"count":"1"
}
],
"2014":[
{
"month":"February",
"count":"2"
},
{
"month":"January",
"count":"3"
}
],
"2015":[
{
"month":"December",
"count":"6"
},
{
"month":"February",
"count":"3"
},
{
"month":"January",
"count":"4"
}
],
"2016":[
{
"month":"January",
"count":"83"
}
]
}
]
}
Can anybody help me get the expected output in cakephp3?