在列表中返回CakePHP GROUP和COUNT项 [英] CakePHP GROUP and COUNT items returned in list
问题描述
我知道这里有一些类似的问题,但是他们都是在使用
的时候。 Model-> find ('所有');
但这不是我在做什么,我在做:
Model-> find('list');
这是什么使这个工作和不工作之间的区别。
对于一组产品,我想找到该组中的所有品牌以及每个品牌的数量。
听起来很简单,这里是我做的:
$ fields = array('Product.brand' 'COUNT('Product`.`brand`)AS brand_count')
$ brand_data = $ this-> Product-> find('list',array(
'fields'=> $字段,
'conditions'=> $ conditions,
'recursive'=> 0,
'group'=>'Product.brand'
)
debug($ brand_data);
在这里我告诉它给我一个数组,其中键是 Product.brand
,值为 COUNT(Product.brand)
获取:
数组
(
[品牌A] =>
[ B] =>
[品牌C] =>
)
当我预期这个:
数组
(
[品牌A] => 534
[Brand B] => 243
[Brand C] => 172
)
b $ b
它可以工作,如果我做所有而不是列表虽然,它只是给我一个更复杂的数组钻取。我很高兴使用全部,我只是想先看看是否有一个原因,它不工作列表?
COUNT() / code>等),所以你应该使用CakePHP的1.3。对于CakePHP 1.2使用,有一个contraption < a>。
详细:
最可能的问题在于您的
COUNT(`Product.brand`)AS brand_count
c $ c> find('list')
$ lst ['keyPath'],$ lst ['valuePath'],$ lst ['groupPath'])
对查询结果,其中 $ lst ['valuePath']
将是
code>{n} .COUNT(`Product`.`brand`)AS brand_count
<在结果中,它实际上是{n} .Product.brand_count
- 不排队。
尝试将您的COUNT()设为虚拟字段。
Ie:
// model
var $ virtualFields = array(
'brand_count'=>'COUNT(Product.brand)'
);
// controller
$ fields = array('Product.brand','Product.brand_count');
I know there are some similar quesiton like this on here but they are all about when using
Model->find('all');
But thats not what I am doing, I am doing:
Model->find('list');
Which is whats making the difference between this working and not working.
Given a group of Products I want to find all the brands in that group and how many of each brand.
Sounds simple enough, here is what I did:
$fields = array('Product.brand','COUNT(`Product`.`brand`) AS brand_count')
$brand_data = $this->Product->find('list',array(
'fields'=>$fields,
'conditions'=>$conditions,
'recursive'=>0,
'group' => 'Product.brand'
));
debug($brand_data);
In this I am telling it to give me an array where the Keys are Product.brand
and the values are COUNT(Product.brand)
I am getting this:
Array
(
[Brand A] =>
[Brand B] =>
[Brand C] =>
)
When I am expected this:
Array
(
[Brand A] => 534
[Brand B] => 243
[Brand C] => 172
)
It works if I do all instead of list though, it just gives me a much more complicated array to drill through. I am fine using all, I just first wanted to see if there was a reason why its not working in list?
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');
这篇关于在列表中返回CakePHP GROUP和COUNT项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!