0
votes

I'm using CakePHP search plugin http://cakedc.com/downloads/view/cakephp_search_plugin and want to make an arg to search multiple fields I have the array filterArgs as follows

var $filterArgs = array(
      array('name' => 'search', 'type' => 'like', 'field' => 'Interview.title_en'),
    );

I want the search arg to search not only the field Interview.title_en but to search also another field I tried something like

var $filterArgs = array(
      array('name' => 'search', 'type' => 'like', 'field' => array('Interview.title_en',  'Interview.Desc'));

but It doesn't work!!

Any suggestions?

1
I managed to solve it by sending the 2 params in the url /interviews/title:{searchedvalue}/desc:{searchedvalue} and in the controller $this->paginate['conditions'] = array( "or" => $this->Post->parseCriteria($this->passedArgs) ); It worked well - Ahmed Elmorsy

1 Answers

4
votes

In order to achieve this, you have to create a simple method in your model which builds the 'OR' conditions for searching the fields.

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

public function filterQuery($data = array()) {
    if(empty($data['q'])) { // q is the name of my search field
        return array();
    }

    $query = '%'.$data['q'].'%';
    return array(
        'OR' => array(
            'Model.title LIKE' => $query,
            'Model.description LIKE' => $query,
            'Model.resources LIKE' => $query,
        )
    );
}