views:

267

answers:

2

I am trying to solve a hopefully simple problem here is the query I am trying produce:

SELECT `categories`.*, COUNT(`entities`.id)
FROM `categories` 
LEFT JOIN `entities` ON (`categories`.`id` = `entities`.`category_id`)
GROUP BY `categories`.`id`

I am really struggling to do this is in cakePHP 1.2

How would/should I go about doing this... (I am using 'Containable' if that helps)

Thanks in advance

+1  A: 

Model->find() has a group param.

webbiedave
+2  A: 

This is what I eneded up with:

 $options = array(
                    'conditions' => $conditions,
                    'fields'=>array('Category.*','COUNT(`Entity`.`id`) as `entity_count`'),
                    'joins' => array('LEFT JOIN `entities` AS Entity ON `Entity`.`category_id` = `Category`.`id`'),
                    'group' => '`Category`.`id`',
                    'contain' => array('Domain' => array('fields' => array('title')))
                );

                return $this->find('all', $options);
Lizard
I didn't know the 'group' param, cheers!
Nicolas