如何用CakePHP格式编写查询?

时间:2014-08-14 12:50:34

标签: php cakephp

请检查我的查询。我需要将其转换为cakephp格式。

SELECT category,user_id 
FROM `products` 
WHERE status=1 
GROUP BY category  
ORDER BY COUNT(category) DESC

CakePHP的

$conditions=array('Product.status'=>1)
$fields = array('Product.category','Product.user_id');
$groups = array('Product.category');

$order = array('COUNT'=>array('Product.category DESC'))

$this->Product->find("all",array('conditions'=>$conditions,'group'=>$groups, 'order'=>$order, 'limit'=>10));

在CakePHP上面查询我得到null结果。请帮帮我。

1 个答案:

答案 0 :(得分:0)

$conditions=array('Product.status'=>1)
$fields = array('Product.category','Product.user_id','COUNT(Product.category) as categoryCount');
$groups = array('Product.category');

$this->Product->find("all",array('fields'=>$fields,'conditions'=>$conditions,'group'=>$groups,'order'=>array('categoryCount DESC'), 'limit'=>10));