18591 Posts in 4875 Topics by 2285 members
General Questions
SilverStripe Forums » General Questions » Session::set adds to and doesn't overwrite
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba
|
Page:
1
|
Go to End | |
| Author | Topic: | 114 Views |
-
Session::set adds to and doesn't overwrite

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
} -
Re: Session::set adds to and doesn't overwrite

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
| 114 Views | ||
|
Page:
1
|
Go to Top |

