Jump to:

23377 Posts in 18296 Topics by 2867 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 757 Views
  • okotoker
    Avatar
    Community Member
    41 Posts

    Session::set adds to and doesn't overwrite Link to this post

    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               
    }

  • jdi
    Avatar
    Community Member
    34 Posts

    Re: Session::set adds to and doesn't overwrite Link to this post

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

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

    757 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.