/**
* @param $filter_attr | String 需要进行统计的字段名称
* @propertuy $where | Array 搜索条件。这个需要些mongodb的搜索条件。
* 得到的是个属性,以及对应的个数。
* 这个功能是用于前端分类侧栏进行属性过滤。
* @return
[
['_id' => 'white', 'count' => 3],
['_id' => 'multicolor', 'count' => 6],
['_id' => 'black', 'count' => 13],
]
*/
public function getFrontCategoryFilter($filter_attr, $where)
{
if (empty($where)) {
return [];
}
if (!isset($where['status'])) {
$where['status'] = $this->getEnableStatus();
}
if($filter_attr != 'color'){
if (!$this->_productModel->hasAttribute($filter_attr)) {
return [];
}
}
// where条件处理
if ($categoryId = $where['category']) {
$productIds = $this->getProductIdsByCategoryId($categoryId);
unset($where['category']);
$arr = [];
$whereArr = [
'and',
['in', 'id', $productIds]
];
foreach ($where as $k=>$v) {
if($k == 'color'){
$whereArr[] = ['like','attr_group_info',$v];
}else{
$whereArr[] = [$k =>$v];
}
}
$where = $whereArr;
}
$product_attrs = $this->_productModel->find()
->select('attr_group_info')
->where($where)->asArray()
->all();
$filter_attr_array = [];
foreach ($product_attrs as $key => $value) {
$product_attr = unserialize($value['attr_group_info']);
if(isset($product_attr['color'])){
if(isset($filter_attr_array[$product_attr['color']])){
$filter_attr_array[$product_attr['color']] += 1;
}else{
$filter_attr_array[$product_attr['color']] = 1;
}
}
}
$filter_data = [];
$filter_data_one = [];
foreach ($filter_attr_array as $key => $value) {
$filter_data_one['_id'] = $key;
$filter_data_one['count'] = $value;
$filter_data[] = $filter_data_one;
}
//echo "<pre>";
//print_r($filter_attr_array);exit;
// 总数
//$filter_data = $this->_productModel->find()
// ->select($filter_attr.' as _id , COUNT(*) as count')
// ->where($where)
// ->groupBy($filter_attr)
// ->all();
return $filter_data;
}
我改了核心services/product/ProductMysqldb.php里面这一块(1095行),在牺牲性能的情况下处理了这样的查询
,保持了不加数据表不加字段,当然,目前只针对于COLOR 这一个字段做的,根据需求或者会增加其他字段,暂时先这样处理了,大佬看如何?