Select Count on contain table

Hi, guys I need help
here is my MajorsController in a view function

        $major = $this->Majors->get($id, [
            'contain' => ['Groups', 'Groups.Teachers', 'Groups.Students'],

        $maleStudent = $this->Majors->get($id, [
            'contain' => ['Groups' => function($m){
                return $m->contain(['Students' => function($mm){
                    return $mm->where(['Students.gender' => 0]);

        $femaleStudent = $this->Majors->get($id, [
            'contain' => ['Groups' => function($f){
                return $f->contain(['Students' => function($ff){
                    return $ff->where(['Students.gender' => 1]);

        $this->set('maleStudent', $maleStudent);
        $this->set('femaleStudent', $femaleStudent);

My question is, How I can select count on my maleStudent and femaleStudent in my View?
I was doing this. but it doesn’t work


0 & 1, binary joke there somewhere.

Your count syntax looks wrong, close it with a round bracket and remove the ->
<?= count($femaleStudent->groups->students) ?>
If you’re not seeing what you expect debug that var and check its contents.

You can tidy up that compact too: -
$this->set(compact('major', 'maleStudent', 'femaleStudent'));

oh, I forgot to remove it
but it still showing this error
count(): Parameter must be an array or an object that implements Countable

Not a drama, but what you think that variable holds is different to what it has.
Turn your debugger on and do: -

debug($femaleStudent); //add this line
$this->set(compact('major', 'maleStudent', 'femaleStudent'));

And it’ll json dump it into your webpage.

If you’re doing this on your production server then I recommend turning the debugger in cfg/app_local.php on via: -

'debug' => filter_input(INPUT_SERVER, 'REMOTE_ADDR') === 'MY.IP.GOES.HERE',

so it only debugs for you!

I tried to active my debug function and it can show the correct value with only 1 student data
“name”:“Desain Pemodelan dan Informasi Bangunan”,
“name”:“X DPIB”,
“name”:“Iqnaaa Aaullllia”,
“address”:"Perum BMI Eagle
“name”:“XI DPIB”,



if I count the the groups, it works and showing 2 data. but for students it show error

Immediate reply, to be followed up in a sec

If any of that json dump is personal data can you please edit your post and remove it, or change it well beyond recognition? Thanks! (Ditto for the pastebin)

there is no personal data there, only a dummy data

I copy/pasted the text into json_decode() but it didn’t like it, so not sure at which point it’s not working.

So just work forwards with it slowly, like replace your debug($femaleStudent); with debug($femaleStudent->groups); and maybe you’ll hit a problem straight away. I’m not sure what Cake can do with its internal structure of its objects, but as the first level of depth is an array, and ‘groups’ is in that, you may need to do something like $femaleStudent[0]->groups but I’m really not sure.

You’ll need someone who knows the internals of the CakePHP variable structures and its properties to get the cleanest reference for your count.

Your $femaleStudent->groups is an array, with (as you say) 2 records. students is an element in each of those. That is, you don’t have $femaleStudent->groups->students, you have $femaleStudent->groups[0]->students and $femaleStudent->groups[1]->students.