我正在使用CakeDC搜索插件。我有一个简单的购物车,产品都属于子类别。例如,食物的子类别可以是水果,蔬菜,肉类等,因此梨仅属于水果类别。
所以我有一个包含所有类别的下拉菜单,如果我选择父类别食物,没有项目出现,如果我选择水果,那么水果将显示。
我想要的行为最初只是显示父类别,所以食物,体育用品等等,然后当他们选择食物时,我想要发生一些不同的事情:
1)显示与此类别相关的所有产品以及所有子类别
2)显示水果,蔬菜,肉等的另一个下拉菜单以进一步过滤
3)如果这个子类别有子类别,也要显示他们的孩子,直到过滤得更多。
我的代码现在只允许我找到与类别的直接关联
以下是我的型号Product.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,
));
}
我已经搜索了一个解决方案的高低,我认为这很简单,但没有找到任何东西。任何帮助都非常感谢!
答案 0 :(得分:2)
我可以通过修改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版