使用 CakeDC 的搜索插件使用多选字段搜索数据


Using CakeDC's search plugin to search data with multi-select field

我正在尝试使用 CakeDC 的搜索插件实现搜索。在此搜索中,我有一个设置了'multiple' => 'checkbox'字段。此字段允许用户选择多个城市,以便他们可以根据城市/城市过滤结果。我对此所做的,我只是在Searchable Model $filterArgs中指定了该领域的'type' => 'IN'。但是注意到发生了它只是响应所有结果,没有发生搜索/过滤。为了清楚地了解我在这里实现的内容,以下是代码片段:

型号.php

   public $actsAs = array(
            'Search.Searchable'
        );
   public $filterArgs = array(
                    'city' => array(
                        'type' => 'in',
                        'field' => 'Model.city'
                    ));

search_form.ctp

echo $this->Form->create('Model', array('url' => array('controller' => 'models', 'action' => 'search')));
echo $this->Form->input('city', array(
    'multiple' => 'checkbox',
    'options' => array(
        'city1' => 'city1',
        'city2' => 'city2',
        'cityn' => 'cityn')
));
echo $this->Form->end('search');   

模型控制器.php

public function search() {
        $this->layout = 'front_common';
        $this->Prg->commonProcess();
        $this->Paginator->settings = array(
            'conditions' => $this->Model->parseCriteria($this->Prg->parsedParams()),
            'limit' => 10
        );
        $this->set('Data', $this->Paginator->paginate());
}

还有一次我试图在 ModelsController 中使用beforeFilter()来内爆city array()(,)用于IN但所有结果都相同。我想问一下是否有任何其他插件可以执行此操作,或者使用 cakeDC 的搜索插件执行此操作的任何黑客。请帮忙。

假设您将 arg 作为简单的值数组传递给parseCriteria(),这应该可以正常工作。

public $filterArgs = array(
    array(
        'name' => 'city',
        'type' => 'value',
        'field' => 'Model.city'
    )
);

您应该能够在URL中传递city:houston|dallas|austin

它应该将其解析为一个args => [city => [houston, dallas, austin]]数组

parseCriteria()将其转换为以下条件片段: [Model.city => [houston, dallas, austin]]

CakePHP原生地将其转换为SQL片段:IN(houston, dallas, austin)

使用调试工具包并监视您的 SQL...您可以在该过程的任何步骤debug(),您应该获得这些值。

以下是搜索插件的完整文档:https://github.com/CakeDC/search/tree/master/Docs/Documentation

(我经常使用它,我喜欢它让我组织所有搜索过滤器的方式)