I'm new to CakePHP and have the following problem:
I have the tables "Images", "Keywords" and "KeywordCategories". Every image can have many keywords (many to many) and every keyword has a category (many to one). Retrieving a list of images with
$images = $this->Images->find()->contain(['Keywords', 'Keywords.KeywordCategories']);
returns a result structure like this:
[
{
"id":1,
"keywords":[
{
"keyword":"Dog",
"keyword_category":{
"title":"Animal"
}
},
{
"keyword":"Cat",
"keyword_category":{
"title":"Animal"
}
},
{
"keyword":"Black",
"keyword_category":{
"title":"Color"
}
}
]
}
]
That's fine but I want the keywords to be grouped by its keyword-category in a structure like this:
[
{
"id":1,
"keyword_categories":[
{
"title":"Animal",
"keywords":[
{
"keyword":"Dog"
},
{
"keyword":"Cat"
}
]
},
{
"title":"Color",
"keywords":[
{
"keyword":"Black"
}
]
}
]
}
]
Any idea how I can achieve that with a CakePHP query?