Using cakephp 4. I have a complex search form with check boxes, date ranges, and text input controls. The user may enter none, some, or all of the inputs available on the form. I build the query with a series of ‘if’ statements to check whether the user entered a value for the search and create the query parameters. For example assume the table is Products with a long list of attributes for each record like color( red, green, blue, yellow, orange, etc), size (s,m,lg), manufacture date, price, description.
The form is created with checkboxes for each color and size, low and high manufacture date range, low and high price range, text input for description keywords.
To create the controller builds the query like this:
$query = $this->Products->find();
if($serch_terms['red']){
$query = $query->where(['color =' => '"red"']);
}
if($search_terms['blue']{
$query = $query->where(['color =' => '"blue"']);
}
if($search_terms['yellow']{
$query = $query->where(['color =' => '"yellow"']);
}
........
if($serach_terms['small']{
$query = $query->where(['size =' => '"small"']);
}
..........
if{$search_term['keywords']){
$query = $query->where(['description like' => "% keyword %"]);
}
$products = $this->paginate($query);
$this->set(compact('products'));
The search can be repeated, so the form posts back to the original template to display results. In the controller I test if the incoming request was a first request or a post back request:
if (!$this->request->is('get') ) {
//This is a postback request from a 'form submit' action
...build query code
$products = $this->paginate($query);
$this->set(compact('products'));
} else {
//This is an initial page access, build a blank form
$product = "";
$this->set(compact('product'));
}
This approach seems to break the paginator.
When I click on a page number or ‘next’, none of the query parameters are preserved and I get a blank results. I’m thinking is has something to do with page and limit parameters of the query, but I’m stuck.
Thanks for any assistance. Much appreciated!
KBG