Patrick Guinness
Patrick Guinness

Reputation: 397

cakephp - building a query with a multi-word search string, searching multiple fields

I have a model 'Mix' which has the fields (which I'd like to be searchable) 'name' 'description' and 'tag_words', as well as a boolean field 'published'. Users should be able to enter a multi-word search term and get results back for any of their words appearing in any of the searchable fields providing the 'published' field is set to 1

So far with the help of google and reading around on here I've got:

if ($this->request->is('post')) {
    $conditions = array();
    $search_terms = explode(' ', $this->request->data['Mix']['search']);
    foreach($search_terms as $search_term){
        $conditions[] = array('Mix.name Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.description Like' =>'%'.$search_term.'%');
        $conditions[] = array('Mix.tag_words Like' =>'%'.$search_term.'%');
    }
    $searchResults = $this->paginate('Mix', array('conditions' => array('Mix.published'=>1, 'AND'=>array('OR' => $conditions))));
}

But since it returns nothing but loads of errors I can guess it's totally wrong. I've very confused, what is the correct syntax?

Upvotes: 1

Views: 1507

Answers (1)

drmonkeyninja
drmonkeyninja

Reputation: 8540

You don't use the AND index, it is already implied:-

$searchResults = $this->paginate('Mix', array('conditions' => array('Mix.published'=>1, 'OR' => $conditions)));

Upvotes: 1

Related Questions