Welcome Guest, Not a member yet? Register   Sign In
Storing standard object in session
#1

[eluser]kmanlove[/eluser]
For sticky forms that repopulate after an error, I've been storing arrays in the session userdata. Works fine. I want to convert that over to objects, but when I try to store objects in the session userdata, I only get "Object" returned to me. I've looked over the serialize code, and it seems like it should work. Instead of $array['keys'], I want $object->keys. No biggie, right?

I guess I'm asking before I put up any code, shouldn't this work fine? There shouldn't be anything preventing me from putting an object instead of an array (size, etc), should there? Any help is greatly appreciated.

Thanks,
Keith
#2

[eluser]Colin Williams[/eluser]
Should make no difference. Have you inspected the data structure at all, with print_r or var_dump? Sounds like you are simply accessing it incorrectly
#3

[eluser]kmanlove[/eluser]
[quote author="Colin Williams" date="1264415104"]Should make no difference. Have you inspected the data structure at all, with print_r or var_dump? Sounds like you are simply accessing it incorrectly[/quote]

I wish it were that simple.

I stick the form object, which I've verified is actually an object, in userdata with
Code:
$this->session->set_userdata('form', $this->item);

Obviously, I get it out with
Code:
$sticky_form = $this->session->userdata('form');

It returns "Object" with var_dump and print_r, and it's even stored like that in the database. If I change it to an array, it works. Really confused.
#4

[eluser]Colin Williams[/eluser]
Have you tried to serialize() the object outside of the session class?

Code:
print_r(unserialize(serialize($this->item)));
#5

[eluser]kmanlove[/eluser]
[quote author="Colin Williams" date="1264417217"]Have you tried to serialize() the object outside of the session class?[/quote]
Yes, and that works fine. Just returns the object as it should.

If I serialize and unserialize outside of the session class, like
Code:
$this->session->set_userdata('form', serialize($this->item));
$sticky_form = unserialize($this->session->userdata('form'));

it all works fine. I didn't think I needed to do this. I thought that the session class handled this. Am I mistaken?
#6

[eluser]Colin Williams[/eluser]
Yeah, it sounds like the session class is messing something up. Might want to dig into it and possibly overload the methods that are causing the problem. And report a bug if you think you find one (make sure you are using the latest version first)




Theme © iAndrew 2016 - Forum software by © MyBB