Hi all
I am relatively new to CakePHP 4.40 but have done a lot of reading in the cook book, done the tutorials and read on other helpful staring guides. It’s all working so far, however am I stuck with below topic. I have done research on my own but so far could not find a solution.
I have below table and would like to select data in a way that the template can access data via one array.
Work log table:
id date hours
1 2022-07-08 2.00
2 2022-06-21 5.00
3 2022-06-06 4.00
4 2022-05-03 1.00
I can get data grouped by month and on an id level and apply paging on one of both.
$worklog = $this->Worklog
->find()
->select(['yearmonth' => 'concat(YEAR(date), MONTH(date))', 'month' => 'MONTH(date)', 'year' => 'YEAR(date)'])
->group('MONTH(date)')
->order(['date' => 'DESC']);
$this->set('worklog', $this->paginate($worklog, ['limit'=> '2']));
$worklog_detail = $this->Worklog->find()
->select(['id', 'date', 'hours'])
->order(['date' => 'DESC']);
$this->set('worklog_detail', $worklog_detail);
However, I can’t get the code to create below JSON. My desired array for template:
Page 1 of 2:
{
"2022_7": {
"month": "7",
"year": "2022",
"data": [
{
"id": "1",
"date": "2022-07-08",
"hours": "2.0"
}
]
},
"2022_6": {
"month": "6",
"year": "2022",
"data": [
{
"id": "2",
"date": "2022-06-21",
"hours": "5.0"
},
{
"id": "3",
"date": "2022-06-06",
"hours": "4.0"
}
]
}
}
Page 2 of 2:
{
"2022_5": {
"month": "5",
"year": "2022",
"data": [
{
"id": "4",
"date": "2022-05-03",
"hours": "1.0"
}
]
}
}
The frontend will present data grouped by month & year with accordions.
Page 1 of 2:
Accordion 1: July 2022
1 2022-07-08 2.00
Accordion 2: June 2022
2 2022-06-21 5.00
3 2022-06-06 4.00
Page 2 of 2:
Accordion 3: Mai 2022
4 2022-05-03 1.00
I’d very much appreciate if you could give me some guidance on how this is most efficient implemented.
Many thanks in advance!