Multiple keyword search using like in CakePHP
function searchCustomers($query=NULL){
$this->Customer->recursive = 0;
//check if there is form input
//if there is form input write those values in session for paging
if (isset($this->data[‘Customers’][‘search’])){
$queryString = $this->data[‘Customers’][‘search’];
CakeSession::write(‘Config.query’, $queryString);
}
//if there is no form input as in case of paging then get those values from session
$queryString=CakeSession::read(‘Config.query’);
$conditions = array();
$queryStrings = explode(‘ ‘, $queryString);
foreach($queryStrings as $query){
$conditions[] = array(‘OR’ => array(
‘Customer.id LIKE’ => “%$query%”,
‘Customer.first_name LIKE’ => “%$query%”,
‘Customer.middle_name LIKE’ => “%$query%”,
‘Customer.last_name LIKE’ => “%$query%”,
‘Customer.organization LIKE’ => “%$query%”,
‘Customer.address LIKE’ => “%$query%”,
}
return $this->set(‘customers’, $this->paginate($conditions));
}
view
<?php
echo $this->Form->create(‘Customers’, array(‘action’=>’searchCustomers’));
echo $this->Form->input(‘search’);
echo $this->Form->end(‘Search’);
?>