# Select Count on contain table

**URL:** <https://discourse.cakephp.org/t/select-count-on-contain-table/8747>\
**Category:** Need Help\
**Created:** [December 16, 2020, 3:38pm UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747 "2020-12-16T15:38:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![werkudara](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/werkudara/32/1772_2.png) [@werkudara](https://discourse.cakephp.org/u/werkudara)\
**Post date:** [December 16, 2020, 3:38pm UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/1 "2020-12-16T15:38:02Z")

</div>

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-\>?\>

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [December 16, 2020, 8:24pm UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/2 "2020-12-16T20:24:27Z")

</div>

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'));`

---

<div class="post-metadata">

**Author:** ![werkudara](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/werkudara/32/1772_2.png) [@werkudara](https://discourse.cakephp.org/u/werkudara)\
**Post date:** [December 17, 2020, 1:03am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/3 "2020-12-17T01:03:20Z")

</div>

> [@Jawfin](#):
>
> $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

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [December 17, 2020, 1:15am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/4 "2020-12-17T01:15:12Z")

</div>

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

```auto
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: -

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

```

so it only debugs for you!

---

<div class="post-metadata">

**Author:** ![werkudara](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/werkudara/32/1772_2.png) [@werkudara](https://discourse.cakephp.org/u/werkudara)\
**Post date:** [December 17, 2020, 1:51am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/5 "2020-12-17T01:51:57Z")

</div>

> [@werkudara](#):
>
> ing this error  
> count(): Parameter must be an array or an object that implements Countable

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](mailto:email%22:%22iqnaaaa@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

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [December 17, 2020, 2:02am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/6 "2020-12-17T02:02:36Z")

</div>

_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)

---

<div class="post-metadata">

**Author:** ![werkudara](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/werkudara/32/1772_2.png) [@werkudara](https://discourse.cakephp.org/u/werkudara)\
**Post date:** [December 17, 2020, 2:04am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/7 "2020-12-17T02:04:48Z")

</div>

there is no personal data there, only a dummy data

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [December 17, 2020, 2:27am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/8 "2020-12-17T02:27:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [December 17, 2020, 4:39am UTC](https://discourse.cakephp.org/t/select-count-on-contain-table/8747/9 "2020-12-17T04:39:10Z")

</div>

> [@werkudara](#):
>
> if I count the the groups, it works and showing 2 data. but for students it show error

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`.
