2

CakeDC の Search pluginで検索を実装しようとしています。この検索で​​は、'multiple' => 'checkbox'設定されているフィールドがあります。このフィールドを使用すると、ユーザーは複数の都市を選択できるため、都市/都市に従って結果をフィルタリングできます。私がこれに賛成したことは'type' => 'IN'、そのフィールドの を でSearchable Model指定しただけ$filterArgsです。しかし、検索/フィルタリングが行われず、すべての結果で応答しただけであることに注意してください。ここで実装した内容を明確に理解するために、コード スニペットを示します。

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());
}

また、beforeFilter()inを使用してwithを内ModelsController破させようとしたことがありますが、すべての結果は同じです。これを行うための他のプラグインや、cakeDC の検索プラグインでこれを行うためのハックがあるかどうかを尋ねたいと思います。助けてください。city array()(,)IN

4

1 に答える 1

0

This should work fine, assuming you are passing in the arg to parseCriteria() as a simple array of values.

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

And you should be able to pass city:houston|dallas|austin in the URL

It should parse that into an array of args => [city => [houston, dallas, austin]]

And parseCriteria() will translate that into the following conditions fragment: [Model.city => [houston, dallas, austin]]

And CakePHP natively translates that into a SQL where fragment: IN(houston, dallas, austin)

Use DebugKit and monitor your SQL... you can debug() at any step of the process, you should get these values.

Here's the full docs on the Search plugin: https://github.com/CakeDC/search/tree/master/Docs/Documentation

(I use it heavily, and I love how it lets me organize all search filters)

于 2015-02-25T18:41:22.083 に答える