CakePHP3でページング時に検索条件を保持
Controller
public function list()
{
if ($this->request->is('POST')) {
$this->Session->write(['requestSearchConditions' => $this->request->data]);
$this->redirect(['action' => 'list']);
}
//LIKE検索リスト
$searchLike = [
'login_name', 'user_name'
];
$searchConditions = [];
$requestSearchConditions = $this->Session->read('requestSearchConditions');
//検索条件配列作成
foreach ($requestSearchConditions as $column => $value) {
if (empty($value)) {
$this->set($column, '');
continue;
}
$this->set($column, $value);
if (array_search($column, $searchLike) !== FALSE) {
array_push($searchConditions, [$column . ' LIKE' => '%'. $value . '%']);
} else {
array_push ($searchConditions, [$column => $value]);
}
}
$query = $this->Recruits->find()->where($searchConditions);
try {
$this->set('recruits', $this->paginate($query));
} catch (NotFoundException $e) {
$this->redirect(['action' => 'list']);
}
}
POST送信時に検索条件をSessionにセット、1ページ目に飛ばすよう処理
これで検索時の条件を保持
ページングはGETで行われるのでページめくりの際は検索条件が更新されない
ページめくりをするとView側で入力していた検索条件がクリアされてしまうのでSessionから検索条件の配列を作成している時にViewに検索条件をセット
こんな感じでよいのか謎