Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Session::set adds to and doesn't overwrite


Reply


2 Posts   876 Views

Avatar
okotoker

Community Member, 41 Posts

12 November 2011 at 6:36am

I have a form action that takes all of its values puts them in an array and stores this in a session. I have a update button so if they change any info and then click update it should reflect those changes. This all works fine if every time the array stays keeps the same keys and just the values change. When the form updates, some fields may be removed so If I try to Session::set to overwrite whats currently there with a new array with less elements, it will change the values that are there but will keep the old elements that no longer exist.

So it seems like its merging and not overwriting when I call Session::set with the new array. I have tried clearing the session right before I call set again and this doesn't work either.

So something like this:

function myFormUpdateAction($data,$form){
$newArr = array();
foreach($data as $key=>$value){
$arr = array($key=>$value);
$newArr=array_merge($newArr,$arr);
}
Session::set('MySession',$newArr);
Director::redirect('/my-page/'); //goes back to same page and builds the form again with the values from the session               
}

Avatar
jdi

Community Member, 34 Posts

31 January 2012 at 11:36am

http://api.silverstripe.org/2.4/sapphire/control/Session.html

Session::save ( )    Save all the values in our session to $_SESSION