CakePHP- cakeDC search plugin implementation

2019-03-06 02:44发布

I am currently trying to figure out a way to implement cakeDC's search plugin within my application, but I am finding it quite difficult to understand the plumbing that needs to be done before I can get it to work(nicely) with my app.

Things to consider: the search needs to be a 'live search' Records retrieved need to be paginated The search will be done using a selected criteria (id,name,etc the actual key not value) and will require a user entry which we will call 'query' for now..

here is my code so far.

Model Code :

public $filterArgs = array(
        'query' => array('type' => 'query', 'method' => 'filterQuery'),

    );

    public function filterQuery($data = array()) {
        $filter = $data['query'];
        $criteria = $data['criteria'];
        if(empty($filter)){
            return array();
        }
        $cond = array(
            'OR' => array(
                $this->alias . $criteria. 'LIKE' => '%' . $filter . '%',
                //ie. criteria represents a field $ filter is the data to search/match
            ));
        return $cond;
    }

So what I am having trouble with is, how will my filterQuery method receive the $data argument.. Is it a normal request data ? I want to access both values submitted.

Here is the relevant code for the view:

<div id="search-container">
                <?php
                //echo $this->Form->create(false,array('type'=>'post','default'=>false));
                echo $this->Form->input('criteria',array(
                        'label'=>'Search Criteria',
                        'options' => array(
                            'id'=> 'By ID',
                            'name' => 'By Name',
                            'blood_group_id' => 'By Blood Type',
                            'type' => 'By Donor Type',
                            'age' => 'By Age',
                            'gender' => 'By Gender' 
                        )
                    ));
                 ?>

                <?php  echo $this->Form->input('query', array('type' => 'text', 'id' => 'query', 'name' => 'query', 'label' => false, 'placeholder' => 'Search')); ?>

[EDIT]

ofcourse in my controller I also have this setup

Search.Prg Component is loaded

 public $presetVars = array(
        'query' => array('type' => 'value'),
        'criteria' => array('type' => 'value'),

    );

Any help is appreciated, even if its just a link to a tutorial. Thanks

1条回答
Emotional °昔
2楼-- · 2019-03-06 03:09

When I wrote the plugin a lot of useful examples I put directly into test cases of the plugin. So take a look into behavior test file to see how to use query type method.

查看更多
登录 后发表回答