Paginating MySQL Fulltext Searches in CakePHP

1 Jul

# query comes from GET request parameter 'q'
$input = $this->params['url']['q'];

# sanitize the query
uses('sanitize');
$sanitizer = new Sanitize();
$q = $sanitizer->clean($input);

# now the pagination options hack to perform a fulltext search
# we are searching a table called 'listings' with a fulltext index on
# `title` and `description`
$options['conditions'] = array(
   '1' => "1 AND MATCH(Listing.title,Listing.description)
          AGAINST('$q' IN BOOLEAN MODE)"
);

$this->set('results' => $this->paginate('Listing', $options);
blog comments powered by Disqus