0

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?

Ahmed Elmorsy
  • 564
  • 3
  • 8
  • 18
  • 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 Jul 26 '11 at 10:15

1 Answers1

4

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,
        )
    );
}
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • 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 without adding other methods – Ahmed Elmorsy Jul 26 '11 at 10:15
  • I'd say It's not complicated and works well without any other additional method. The additional method will be the perfect way with more complex query but I just wanted to add OR between conditions instead of And between them. – Ahmed Elmorsy Jul 28 '11 at 07:08