I've been using CakePHP for over a year. Without it, I probably would have stopped using PHP and switched to python or maybe squeak and seaside.
More and more, the strengths of the framework and my increasing knowledge of it make its forfeit an expensive proposition.
Now I'm up-ing the ante and releasing my own component. It doesn't do a whole lot, but I find it useful.
I've been using the code for most of my projects, although it was embedded in the controllers and customized.
As the month drew to a close and I had yet to write a blog post—again—I decided to distill the logic and give it to the sober masses.
But enough of the fascinating story behind the component, let me tell you how to use it.
soySearch creates criteria for a findAll query. All you have to do is pass it the search fields like this
$this->data['Search']['Modelname.fieldname']
So if you wanted your users to be able to search the body of your blog posts you could make a form like:
<form method="post" action="/posts/index/"> <input type="text" name="data[Search][Post.content]" /> <input type="Submit" value="Search" /> </form>
In your controller you include the component:
var $components = array('soySearch');
In the action you invoke the component:
function search() {
$this->Post->recursive = 1;
$criteria = array();
$criteria = $this->soySearch->getCriteria($criteria);
$this->set('posts', $this->Post->findAll($criteria));
}
Notice you can pass any pre-existing criteria to the component.
Try it out:
An additional feature is the ability to save the search to the session and recall it for later use. Just add the query string parameter newSearch=0 to the form action and the search will be stored in the session. I use it with the pagination component so I can order and page my resultset.