Count in contain Cakephp 3
<p>I have a table <strong>Post</strong> and this has a <strong>has-many</strong> association with a table <strong>Stars</strong>. </p>

<p>I can get all the associated data using:</p>

<pre><code>$this-&gt;Posts-&gt;find()-&gt;contain(['Stars']);
</code></pre>

<p>That works well.</p>

<p>But I want to count the Stars. I have tried this but its not working:</p>

<pre><code>$this-&gt;Posts-&gt;find-&gt;contain([
    'Stars' =&gt; function($q) {
        return $q-&gt;select(['total' =&gt; $q-&gt;func()-&gt;count('Stars.post_id')]);
    }
]);

//I've also tried this
...
...$q-&gt;select(['total' =&gt; "COUNT(Stars.post_id)"]);
...
//Also fail
</code></pre>

<p>This does not return the number of associated Stars.</p>

<p>Is there something wrong or should do it some other way?</p>

<p>Thanks</p>