Finding records that match a set of search terms is almost a must-have on most web applications. Even when there is a good number of more in-depth, complex search solutions, sometimes a simple search is all we need.
This recipe shows how to implement a LIKE-based
search to find records that match some terms.
We need some sample models and data to work with. Follow the Getting ready section of the recipe, Performing GROUP and COUNT queries.
If we want to find all posts that have the word Post 1
or the word Post 2
, either in its title, or post, we do:
$posts = $this->Post->find('all', array( 'fields' => array('Post.id', 'Post.title'), 'conditions' => array('or' => array( array('Post.title LIKE ?' => '%Post 1%'), array('Post.body LIKE ?' => '%Post 1%'), array('Post.title LIKE ?' => '%Post 2%'), array('Post.body LIKE ?' => '%Post 2%'), )), 'recursive' => -1 ));
The preceding statement will...