【问题标题】:Using CakeDC's search plugin to search data with multi-select field使用 CakeDC 的搜索插件搜索具有多选字段的数据
【发布时间】:2014-11-20 14:39:18
【问题描述】:

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

Model.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');   

ModelsController.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 的搜索插件来做到这一点。请帮忙。

【问题讨论】:

  • 我认为搜索插件不支持此功能。您最好只构建自己的过滤器。
  • @DavidYell 是的,我认为它不存在。可能是我们需要调整它以按照我们的意愿使用它,或者您可以建议任何方法以便我可以实现它..?
  • 就我个人而言,我可能只是遍历请求数据并构建一个数组,我可以将它传递给我的查询中的“IN”条件。
  • @DavidYell 是的,我做了类似的事情,我最后也提到过,问题可能是我的实现错误..

标签: php cakephp cakephp-2.0 cakephp-2.5 cakephp-search-plugin


【解决方案1】:

这应该可以正常工作,假设您将 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 where 片段:IN(houston, dallas, austin)

使用 DebugKit 并监控您的 SQL...您可以在过程的任何步骤debug(),您应该得到这些值。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多