Please help me to correct my code above, what I want is the total_score can SUM the Rubrics score where Instrument tabel that Inside CategoryInstruments…
it’s work but SUM function is calculate all rubrics score not in the specific in each data in table CategoryInstruments.
so you want sum group by category ? … and what fields, objects do you expect and need at the end ? … going inside out may be a better way … so doing subquery for sum and left join it with category. … but lets see … you have more tables in your query than in your question … can you try to rephrase your question please ?
I’m sorry that I’m not good in english, you were correct actually
the field what I want to get is SUM of score in Rubrics table which group by Instruments table that contain by Category
No worries at all, well it still depends on your use case and what data you are fetching from $categoryInstruments your view_internal.ctp (and also which cake version you use - 3.x ?).
But I try to give you some idea on alternatives ( cake 3.8+) what I (me also not an expert) would see:
Usecase 1: get the sum of Rubrics.score for each category and subcategory assuming you have good model and associations
use case 2: your query (probably may not work)
$categoryInstruments = $this->CategoryInstruments->find(‘all’)->contain(
[
‘SubCategories’ => function($q) use($supervision_id){
return $q->contain([‘Instruments’ => function($qq) use($supervision_id){
return $qq->contain([‘Rubrics’ => function($s){
return $s->select([
‘Rubrics_Totalscore’ => ‘sum(Rubriks.score)’,
])
}])->where([‘Instruments.supervision_id’ => $supervision_id]);
}]);
}
]
);
can you give an example, what your output (fields, values) should look like ? and probably the associations and relations between your four tables would be interesting.
The grouping on instrument_id looks for me a bit to detailed, so the statement itself will work may be, but you group on the most granular level and also without any sum function now.
so,
at which level you want to sum and which table is delivering the grouping field ?
which fields do you need / use in your view_internal.ctp ?