蛋糕PHP蛋糕搜索按类别和类别儿童查找所有产品


CakePHP cakedc search find all products by category and categories children

我正在使用CakeDC搜索插件。 我有一个简单的购物车,其中包含所有产品都属于子类别。 例如,食物的子类别可能是水果、蔬菜、肉类等。 所以梨只属于水果类别。

所以我有一个包含所有类别的下拉列表,如果我选择父类别食物,则没有项目显示,如果我选择水果,那么水果将显示。

我想要的行为是最初只显示父类别,所以食品、体育用品等。 然后当他们选择食物时,我希望发生一些不同的事情:

1) 显示与此分类关联的所有商品以及所有子分类

2)显示另一个包含水果,蔬菜,肉类等的下拉列表,以便进一步过滤

3)如果此子类别有子类别,则还要显示其子类别,直到过滤更多。

我现在的代码只允许我找到与类别的直接关联

这是我的模型产品中的代码.php

 public $actsAs = array('Search.Searchable');
    public $filterArgs = array(
        array('name' => 'cid', 'field' => 'category_id', 'type' => 'value', 'method' => 'query', 'method' => 'query', 'allowEmpty' => true);

    public function filterCat($data, $field = null) {
        if (empty($data['cid'])) {
            return array();
        }
        $cat = $data['cid'];
        return array(
            'OR' => array(
                $this->alias . '.category_id' => $cat,
                ));
    }

我已经上下搜索了一个解决方案,我认为这很简单,但什么也没找到。 任何帮助都非常感谢!

我能够通过修改 filterCat() 函数来实现我想要的功能,如下所示。

public function filterCat($data, $field = null) {
    if (empty($data['cid'])) {
        return array();
    }
    $cat[] = $data['cid'];
    // Get child categories
    $children = $this->Category->children($cat['0'],false,'id');
    // extract the ids of the child categories
    $children = Hash::extract($children, '{n}.Category.id');
    // if no child categories, return requested category id
    if(empty($children)){
        return array(                
           $this->alias . '.category_id' => $cat);
    }else{
    // else merge array of child category ids with parent category id
    // and create SQL string to match the list of category ids
        $children = array_merge($cat,$children);
        return array(                
            $this->alias . '.category_id IN ' => $children);    
    }
}

该函数查找子类别,如果没有子类别,则返回带有类别 ID 的搜索字符串。 如果存在子类别,它将返回列表中包含父类别和子类别 ID 的搜索字符串。

我知道这是一个老问题,但希望这可以帮助其他人。

CakePHP 版本 2.2.3 和 CakeDC 搜索插件版本 2.1