Wednesday, January 28, 2009

Re: Get count of related model

I think the quickest way would be to create a database view.

CREATE VIEW foobar AS SELECT f.id, COUNT(b.id) FROM foo AS f INNER
JOIN bar AS b ON b.foo_id = f.id GROUP BY f.id';

$this->Foo->query('SELECT * FROM foobar');

Oooh! Wouldn't Cake DBviews be tasty? Cake 2.0, maybe?

On Wed, Jan 28, 2009 at 10:18 AM, Günther Theilen <theilen@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: