If you need to get how many articles are in a specific Category ID (or a
group of category IDs, or all categories) then set up a model for your HABTM
join table. If your models are named Category and Content, and you've set up
the HABTM on Category (so your join table is named contents_categories) then
create a model file contents_category.php:
class ContentsCategory extends AppModel
{
var $name = 'ContentsCategory';
var $primaryKey = 'category_id';
function getArticleCount($categoryIds)
{
$result = false;
if (!is_array($categoryIds))
{
$categoryIds = array($categoryIds);
}
$conditions = 'ContentsCategory.category_id IN (' .
implode(',', $categoryIds) . ') GROUP BY ContentsCategory.category_id';
$records = $this->findAll($conditions,
'ContentsCategory.category_id, COUNT(*) AS count', null, null, null, 0);
if ($records !== false)
{
$result = array();
foreach($categoryIds as $categoryId)
{
$result[$categoryId] = 0;
}
foreach($records as $record)
{
$result[$record['ContentsCategory']['category_id']] = $record[0]['count'];
}
}
return $result;
}
}
Then add this model to your controller:
var $uses = array('ContentsCategory');
And do:
$category_id = 4; // Category ID we want to get number of articles
$result = $this->ContentsCategory->getArticleCount($category_id);
If you want to get the number for a SET of categories:
$result = $this->ContentsCategory->getArticleCount(array(1, 4, 3));
If you want to get the number for all categories:
$all = $this->Category->findAll(null, 'Category.id', null, null, null, 0);
$categoryIds = array();
if ($all !== false)
{
foreach($all as $record)
{
$categoryIds[] = $record['Category']['id'];
}
}
$result = $this->ContentsCategory->getArticleCount($categoryIds);
(PS: With CakePHP 1.2's Set::extract() method the above would be just one
line)
For example the following:
$result = $this->ContentsCategory->getArticleCount(array(1, 4, 3));
Will generate this query:
SELECT `ContentsCategory`.`category_id`, COUNT(*) AS count FROM
`contents_categories` AS `ContentsCategory` WHERE
`ContentsCategory`.`category_id` IN (1,4,3) GROUP BY
`ContentsCategory`.`category_id`
And will get you this as a result:
Array
(
[1] => 3
[4] => 0
[3] => 1
)
-MI
---------------------------------------------------------------------------
Remember, smart coders answer ten questions for every question they ask.
So be smart, be cool, and share your knowledge.
BAKE ON!
blog: http://www.MarianoIglesias.com.ar
-----Mensaje original-----
De: [email protected] [mailto:[EMAIL PROTECTED] En nombre
de Nookie
Enviado el: Sábado, 24 de Febrero de 2007 10:16 p.m.
Para: Cake PHP
Asunto: Re: how to count articles in category ?
now i've put $hasAndBelongsToMany into my Category model so i get
categories but with all articles.I need ONLY number of articles in
category and i dont want to use count($data['Article']) in PHP !!
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---