I am currently working on a website with a lot a of table. Almost all of the table have filters, and since the user don't want to lose all of his filters when he reloads, I am saving the filter in the session.
So far, all of this is working, I run an ajax call everytime the filter changes to save the new filter in the session. My issue is when I try to clear the filters.
When the user clicks on the clear button or closes the last filter box, the session key is emptied, and it prints null, but when I reload the page, that last filters are loaded, and the session prints them as if I never removed them.
The function that I use to fill/empty the session:
public function addPersistenceAction(Request $request)
{
$parameters = $request->request->all();
$session = $this->get('session');
$session->start();//I tried removing/adding this line but it didn't change anything
//if the key wasn't send, nothing happends
if (!isset($parameters['storage'])){
return new JsonResponse(0);
}
//if no data was send, it removes the key.
if(!isset($parameters['data'])) {
$session->remove($parameters['storage']);
// I also tried setting an empty array but it didn't work either
//$session->set($parameters['storage'], []);
return new JsonResponse($session->all()); // this prints the correct data
}
//if the data was sent, it is set. This works perfectly
$session->set(
$parameters['storage'],
$parameters['data']
);
return new JsonResponse($session->get($parameters['storage']))
}
The function always returns what I expect it to, but everytime I reload the page, the session still have the last filter. I have tried with different filters and it's always the last one so it can't have been harcoded in my controller.
Funny thing, I have noticed that if I manually run the function that gets the filter data before reloading the page, it shows me the right value, and if I reload after that, the filter are correctly emptied.
So my question: Is there a reason why my session seems to not be saved and is there a way to programatically "force" symfony to save the session data?
some usefull code:
The function that gets the session data:
/**
* @param Request $request
* @return JsonResponse
*/
public function getSessionDataAction(Request $request){
$parameters = $request->request->all();
$session = $this->get('session');
$session->start();
if (!empty($parameters['storage'])) {
$data = $session->get($parameters['storage'])?:[];
return new JsonResponse($data);
}else{
return new JsonResponse(array('status' => 'failed', 'message' => 'storage is mandatory'));
}
}
My controller function:
/**
* @param Request $request
* @return Response
*/
public function indexAction(Request $request)
{
$this->denyAccessUnlessGranted(RoleVoterHelper::SECTION_USER_VIEW);
$session = $request->getSession();
//I checked and the session key I use is not touched in this function
$filterWidgets = $this->get('dmt.filter.manager')->getWidgetData('user_filter');
if (!$this->get('dmt.role_voter')->getExtGranted(RoleVoterHelper::SECTION_USER_VIEW)) {
$filterWidgets['company'] = $this->getDoctrine()->getRepository('BugTrackerModelBundle:Company')->findByAuthorities($this->getUser()->getAuthorities());
}
$authorities = $this->getDoctrine()->getRepository('BugTrackerModelBundle:Authority')->findAll();
return $this->render('DMTBundle:User:index.html.twig', [
'filter_widgets' => $filterWidgets,
'usersColumns' => $session->get('usersColumns'),
'usersSort' => $session->get('usersSort'),
'isSuperAdmin' => $this->isGranted(UserHelper::ROLE_SUPER_ADMIN),
'authorities' => $authorities,
]);
}
The javascript called when the page opens:
$.post(dependencies.bridgeGetUrl, {
// something like "project_page_filter", the key for the session.
storage : dependencies.storage
}).success(function(response){
dependencies.filterManager.hook(function(app){
app.scheme = response;
app.readScheme();
});
run(dependencies);
if(typeof dependencies.callback == "function"){
dependencies.callback(dependencies.params);
}
});
the javascript that sends any modification to the session:
App.scheme is always the current value of the filters
dependencies.filterManager.setLifecycle(["add", "change-value"], function(app) {
$.post(dependencies.bridgeUrl, {
storage : dependencies.storage,
data : app.scheme
});
}).setLifecycle(["remove-all", "remove"], function(app) {
$.post(dependencies.bridgeUrl, {
storage : dependencies.storage,
data : app.scheme
});
$("#filterSubmit").click();
});