I have on my CakePHP project some Categories that hasMany Subcategories that hasMany Texts that hasMany and belongToMany tags. To rephrase it, the Texts can be tagged and belong to a subcategory. A subcategory belong to a category.
I'm looking for a clean and efficient way to get these informations out of the table. I want for all Categories, all of these Subcategories, the Texts that depends on it and their tags.
I think in the first time that my controller should return an array like :
['categories'=>[[
'name'=>'cat1' 'subcategories'=>[[
'name'='sub1', texts=>[[
'name'=>'text1', 'tags'=>['tag1', 'tag2']
]]
]]
]]]
So i tried to build this structure by hand and I made a loop in a loop in a loop... The code was so horrible I knew my way was wrong.
I tried to join all the tables together with the 'find' method but it doesn't seem to work. here is the beginning of the code :
$query = $categories->find('all', [
'where'=>['Categorie.enabled ='=>true],
'join'=>[
[
'table' => 'Subcategories',
'type' => 'LEFT',
'conditions' => 'Subcategories.categorie_id = Categories.id'
],[
'table' => 'Texts',
'type' => 'LEFT',
'conditions' => 'Texts.subcategorie_id = Subcategories.id'
]
],
'fields' => ['Subcategories.name', 'Categories.name']
]);
I got two row, one for each subcategory but I can't see the texts.
Am I doing right using joins ? Why the texts I created are not visible on my test page ? Is my orthographic mistake on the singular of categories the reason of this malfunction ?
I hope I'm understandable.