I am fetching data in pagination $dogs variable and then passing it into ctp and displaying that data. So when we come to that page, $dogs variable has 10 records from database.
When I pass search data and add condition on that same $dogs variable, it updated with searched result in controller. for e.g if I pass "tomy" in search and add condition and update $dogs variable, it display only 3 records with who have name tomy.
If I print $dogs variable in controller it give me correct result of 3 record.
But on ctp file, it display data without adding condition i.e default result which I fetched first time when we load page which are 10 records.
I have clear cache as well but not sure why this happens.
Below is my code
if ($this->request->is('post')) {
$data = $this->request->getData();
$dogname = $data['search_dog'];
$id = $data['client_id'];
$this->request->session()->write('search_dog', $dogname);
//$conditions['name LIKE'] = "%" . $dogname . "%";
$this->ClientDogs->belongsTo('ClientUsers', [
'className' => 'ClientUsers',
'foreignKey' => 'client_users_id',
]);
$dogs = $this->paginate($this->ClientDogs->find('all', ['contain' => ['ClientUsers'], 'conditions' => array('name LIKE' => '%' . $dogname . '%')]), ['limit' => 9]);
} else {
$this->ClientDogs->belongsTo('ClientUsers', [
'className' => 'ClientUsers',
'foreignKey' => 'client_users_id',
]);
$dogs = $this->paginate($this->ClientDogs->find('all', ['contain' => ['ClientUsers']]), ['limit' => 9]);
}
Here $dogs variable gives correct result in controller, but when I passed that $dogs variable in ctp file, it gives same result which is fetched all $dogs no matter what condition I passed.
// keep this inside ClientDogs Model
$this->belongsTo('ClientUsers', [
'className' => 'ClientUsers',
'foreignKey' => 'client_users_id',
]);
// Inside controller
// why to use POST while search ?
// if the filters params sent via post it wont be on the URL, and it cant be retrive while GET
// if the $dogname is stored on the session, it has to retrive while GET
$params = $this->request->getQuery();
// $id = $params['client_id']; // this is unused
$conditions = [];
if(!empty($params['search_dog'])){
$conditions[] = ['ClientDogs.name LIKE' => '%' . $params['search_dog'] . '%'];
}
$dogQuery = $this->ClientDogs->find('all', ['contain' => ['ClientUsers'], 'conditions' => $conditions ]);
$dogs = $this->paginate($dogQuery, ['limit' => 9]);