# Keep query alive during execution of script

**URL:** <https://discourse.cakephp.org/t/keep-query-alive-during-execution-of-script/10548>\
**Category:** Need Help\
**Created:** [July 30, 2022, 3:16pm UTC](https://discourse.cakephp.org/t/keep-query-alive-during-execution-of-script/10548 "2022-07-30T15:16:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeanquin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/mdeanquin/32/1802_2.png) [@mdeanquin](https://discourse.cakephp.org/u/mdeanquin)\
**Post date:** [July 30, 2022, 3:16pm UTC](https://discourse.cakephp.org/t/keep-query-alive-during-execution-of-script/10548/1 "2022-07-30T15:16:38Z")

</div>

I have a query where I show a grid paginated given a filter made by the user, when I click on page 2 it disappears because I ask if($this-\>request-\>is(‘post’)), how can I assign this query to a session variable so that it doesn’t disappear when I change of page?  
This is the code of the action in the controller:

```auto
    public function query($id = null)
    {
		$this->paginate = [
			'contain' => ['Enterprises', 'Affiliates'],
		];
		$contributions = [];
		if($this->request->is('post')) {
			$where = ['Contributions.enterprise_id' => $id];
			if(!empty($this->request->getData('cuil'))) $where += ['Affiliates.cuil' => $this->request->getData('cuil')];
			if(!empty($this->request->getData('period_since'))){
				if(empty($this->request->getData('period_until'))) $where += ['period' => $this->request->getData('period_since')];
				else $where += ['period >=' => $this->request->getData('period_since')];
			}
			if(!empty($this->request->getData('period_until'))) $where += ['period <=' => $this->request->getData('period_until')];
			if(!empty($this->request->getData('created_since'))) $where += ['created >=' => $this->request->getData('created_since')];
			if(!empty($this->request->getData('created_until'))) $where += ['created <=' => $this->request->getData('created_until')];
			$contributions = $this->paginate($this->Contributions->find('all')->where($where));
			$taxable_remuneration = $this->Contributions->find('all')->where($where)->func()->sum('taxable_remuneration');
			$this->set('taxable_remuneration', $taxable_remuneration);
			$taxable_remuneration = $this->Contributions->find('all')->where($where)->func()->sum('taxable_remuneration');
			$this->set('taxable_remuneration', $taxable_remuneration);
			$union_dues = $this->Contributions->find('all')->where($where)->func()->sum('union_dues');
			$this->set('union_dues', $union_dues);
			$worker_contribution = $this->Contributions->find('all')->where($where)->func()->sum('worker_contribution');
			$this->set('worker_contribution', $worker_contribution);
			$solidarity_aid_fund = $this->Contributions->find('all')->where($where)->func()->sum('solidarity_aid_fund');
			$this->set('solidarity_aid_fund', $solidarity_aid_fund);
		}
		$this->set(compact('contributions'));
		$this->set('id', $id);
    }
```

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [July 30, 2022, 7:40pm UTC](https://discourse.cakephp.org/t/keep-query-alive-during-execution-of-script/10548/2 "2022-07-30T19:40:11Z")

</div>

I would recommend you look into the Post/Redirect/Get Pattern to keep filters alive.  
Basically you shouldn’t put filter values inside the session or inside POST data but instead everything filter/pagination related should be inside the query params (aka GET params)

With that the paginator will also keep those filters active even when changing pages.  
This is also how the [GitHub - CakeDC/search: Search Plugin for CakePHP](https://github.com/CakeDC/search) handles it
