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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Session::set adds to and doesn't overwrite


Go to End


2 Posts   2084 Views

Avatar
okotoker

Community Member, 50 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
Jakxnz

Community Member, 36 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