Easiest way I've found is just to include a select field in the query
like:
$data = $this->find("all", array(
"fields"=>array(
"Tag.id",
"Tag.name",
"(select COUNT(Article.id) from
articles Article where Article.tag_id = Tag.id and Article.published =
1) AS tag_count"
)));
Here's an example of using the finderQuery:
http://groups.google.com/group/cake-php/msg/c10840be78a34df0
On Jan 17, 12:55 am, RichardAtHome <[email protected]> wrote:
> Given the following Model relations:
>
> article <-- HABTM (articles_tags) --> tag
>
> How do I fetch back a list of Tags with the count of associated
> Articles?
>
> I've tried (in the Tag Model):
>
> $data = $this->find("all", array(
> "fields"=>array(
> "Tag.id",
> "Tag.name",
> "COUNT(Article.id) AS tag_count"
> ),
> "conditions"=>array(
> "Article.published" => 1
> ),
> "group" => "Tag.id"
> ));
>
> But that doesn't generate the HABTM join in the SQL:
>
> SQL Error: 1054: Unknown column 'Article.id' in 'field list'
> SELECT `Tag`.`id`, `Tag`.`name`, COUNT(`Article`.`id`) AS tag_count
> FROM `tags` AS `Tag` WHERE `Article`.`published` = 1 GROUP BY
> `Tag`.`id`
>
> Thanks in advance :-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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
-~----------~----~----~----~------~----~------~--~---