I'm writing a plug-in module for a system that uses CakePHP 1.2 and I am new to the framework. How does one do the following query the Cake way?
SELECT a.id, a.name, COUNT(a.id) AS comments
FROM articles a LEFT JOIN comments c ON a.id = c.item_id
GROUP BY a.id
ORDER BY comments DESC;
I can't edit the Article or Comment models, but I'm trying this in my plug-in's model and it doesn't give the same result:
$this->loadModel('Article');
$options['fields'] = array('Article.id', 'Article.name',
'COUNT(Article.id) AS comments');
$options['joins'] = array(
array('table' => 'comments',
'alias' => 'c',
'type' => 'INNER',
'conditions' => array(
'Article.id = c.item_id')
));
$options['group'] = array('Article.id');
$options['order'] = 'comments DESC';
$options['limit'] = 5;
$rows = $this->Article->find('all', $options);
Also, I'm not sure but I think the Article class might already have:
public $actsAs = array('Containable');
public $hasMany = array('Comment');