Recently I get stuck with seemingly simple task as to output the related count number of referenced items in an query builder statement.
Here the simplified code:
$data = TableRegistry::getTableLocator()->get('tableA')->find()
->select(
[
'tableA.term',
'tableA.termkey',
'count(tableA.termkey)' //my first though was this...but it does not work
'count' => TableRegistry::getTableLocator()->get('tableA')->find()->func()->count('*') //ok
],
)
->join([
....
]
])
->where(
....
)->group(
....
)->order(
....
);
Right now, after some documentation lookup I had finally find an solution with rather exotic syntax.
TableRegistry::getTableLocator()->get('tableA')->find()->func()->count('*') //ok, works so far..but do I need this all stuff for an simple count?
It is really the desired approach to use a count function within an query builder?
Are there any better solution than this?