I use the finderQuery of the hasMany relation, I call bindModel before
paginate.
Suppose a Customer has many Contract, in paginate :
$finderContract = 'SELECT DISTINCT Contract.customer_id,
COUNT(Contract.id) as contractCount ';
$finderContract .= 'FROM contracts as Contract ';
$finderContract .= 'WHERE Contract.customer_id IN({$__cakeID__
$}) ';
$finderContract .= 'GROUP BY Contract.customer_id';
$this->Customer->bindModel(array('hasMany' => array(
'Contract'
=> array('className' => 'Contract',
'finderQuery' => $finderContract)
)
), false);
Hope this helps
On Feb 25, 8:33 am, Adam Royle <[EMAIL PROTECTED]> wrote:
> If you want to sort on the column as well, I suggest you create a
> "counter cache". Basically, you store the count of subitems in a field
> in the main table, which is updated on create/update/delete.
>
> There is even a behaviour that can do this automatically for you.
>
> http://bakery.cakephp.org/articles/view/countercache-or-counter_cache...
>
> Cheers,
> Adam
>
> On Feb 25, 3:02 pm, Andrew Allen <[EMAIL PROTECTED]> wrote:
>
> > This may be silly since there is a better to do this I'm sure, but is
> > there a way to count the number of items in a subselect on pagination?
> > Or is there a way to count the number of items that are linked in a
> > hasMany relationship. i.e. can you output the number of comments that
> > are associated with a post in a blog?
>
> > Thank you in advance.
>
> > Sincerely,
> > ~Andrew Allen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---