CakePHP GROUP and COUNT items returned in list

五迷三道 提交于 2019-12-05 08:02:38

Brief recap: find('list') has problems with aliased fields (and therefore aggregate functions like COUNT() etc.), so you should use CakePHP's 1.3 instead. For CakePHP 1.2 uses there's a contraption.

Detailed: Most likely problem lies in your

COUNT(`Product.brand`) AS brand_count

Because find('list') does

Set::combine($results, $lst['keyPath'], $lst['valuePath'], $lst['groupPath']) 

on results of query, where $lst['valuePath'] would be

"{n}.COUNT(`Product`.`brand`) AS brand_count"

while in results it would be actually "{n}.Product.brand_count" - doesn't line up.

Try making your COUNT() a virtual field.

I.e.:

//model
var $virtualFields = array(
    'brand_count' => 'COUNT(Product.brand)'
);

//controller
$fields = array('Product.brand','Product.brand_count');
Pooja Baghel

In controller in Paginator you can use like this:

 $itemads = $this->paginate($itemads, ['contain' => ['Departments', 'Stores'],'group'=>'upc','fields'=>array("upc_count"=>"COUNT(`upc`)")]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!