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(compact('major'));
        $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

<?=count($femaleStudent->groups->students->?>

0 & 1, binary joke there somewhere.

Your count syntax looks wrong, close it with a round bracket and remove the ->
<?= count($femaleStudent->groups->students) ?>
(untested)
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
{
“id”:1,
“name”:“Desain Pemodelan dan Informasi Bangunan”,
“created”:“2020-12-08T06:55:35+00:00”,
“modified”:“2020-12-08T06:55:35+00:00”,
“groups”:[
{
“id”:1,
“name”:“X DPIB”,
“created”:“2020-12-08T07:08:26+00:00”,
“modified”:“2020-12-10T15:47:33+00:00”,
“major_id”:1,
“teacher_id”:1,
“students”:[
{
“id”:5,
“rfid_card”:“127.123.242”,
“nis”:“343/4233.045”,
“nisn”:“2147483647”,
“nik”:“2147483647”,
“name”:“Iqnaaa Aaullllia”,
“gender”:“1”,
“address”:"Perum BMI Eagle
“place_born”:“Brebes”,
“date_born”:“2005-06-09”,
“rt”:2,
“rw”:7,
“village”:“Jedong”,
“sub_district”:“Jedong”,
“district”:“Wagir”,
“post_code”:65158,
“stay_status”:5,
“transportation”:7,
“phone”:“098778342343”,
email":"iqnaaaa@gmail.com”,
“kip”:1,
“photo”:“94037abaf03a3f8967a32ae9cc13a6b252cd3a1e.jpg”,
“created”:“2020-12-10T13:13:25+00:00”,
“modified”:“2020-12-12T14:47:49+00:00”,
“user_id”:6,
“group_id”:1,
“parrent_id”:9
}
]
},
{
“id”:2,
“name”:“XI DPIB”,
“created”:“2020-12-08T07:22:26+00:00”,
“modified”:“2020-12-15T23:20:23+00:00”,
“major_id”:1,
“teacher_id”:4,
“students”:[

     ]
  }

]
}

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.