Thursday, January 29, 2009

Re: Get count of related model

Very nice, thanks!

Webweave wrote:
> You could just add a count to the fields that you are fetching like:
>
> $this->Foo->find('all', array('fields' => array('Foo.*', '(select count
> (1) from bars Bar where Bar.foo_id = Foo.id) as bar_count')));
>
> I use this approach both with fields and it can also be used with
> conditions when you need to limit the results to rows that have (or
> don't have) related items.
>
> On Jan 28, 7:18 am, Günther Theilen <thei...@eqi.de> wrote:
>> Hi,
>>
>> I've got two models "Foo hasMany Bar".
>> What would be the best way to get all entries of Foo and the number of
>> according entries in Bar.
>> I thought about a find->('all') and a count() in the view but that
>> doesn't seem to be very elegant.
>> Any hints?
>>
>> Regards
>> Guenther
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: