The recipe, Searching for all items that match search terms, gives us a great starting point to create a custom find type. Custom find types allow us to extend the basic find types any model has, allowing our code to become more readable and extensible.
This recipe shows how to create a custom find type to allow the Post
model to be searched against a set of terms, thus extending the functionality shown in the previous recipe.
We need some sample models and data to work with. Follow the Getting ready section of the recipe, Performing GROUP and COUNT queries.
1. Open the
post.php
file and add thesearch
find type to the list of find methods using the_findMethods
property, together with the actual implementation of the_findSearch()
method.<?php class Post extends AppModel { public $belongsTo = array('Blog'); public $_findMethods = array('search' => true); protected function _findSearch($state, $query, $results = array()) ...