Welcome Guest, Not a member yet? Register   Sign In
storing objects in CI session
#1

[eluser]stormbytes[/eluser]
Trying to store an serialize($object) in $this->session->set_userdata('object', $object) and then retrieve it in a view using unserialize(). Is there an issue with this approach I should know about? It's not working.. getting errors.
#2

[eluser]Nick_MyShuitings[/eluser]
What errors?
#3

[eluser]stormbytes[/eluser]
Random stuff that didn't make much sense... "incomplete object" etc.

The real problem is that I'm not sure how to go about it, so I'm playing guessing games! ughh...

How about a tip?

I'd like to create an instance of my custom class in a controller method, then save that object in the session and retrieve/use it in another controller method at some later point Smile
#4

[eluser]Nick_MyShuitings[/eluser]
from another thread (http://ellislab.com/forums/viewthread/173310/):

[quote author="WanWizard" date="1289625664"]Are you using cookie-only sessions? And if so, are you storing a lot of data?

Cookies have a maximum size of 4Kb. If you encrypt the data, the data increases in size. If it exceeds 4Kb, the cookie gets truncated, which means it can't be decrypted anymore, and all data is lost.[/quote]

From the incomplete object error it seems like you might be truncated. I would recommend one of the advanced session libraries, or at the least setting the config to true to have the session stored in the db.
#5

[eluser]stormbytes[/eluser]
I'm storing the session data in db. I think my problem is syntactical. Could you provide some generic example I could follow that would accomplish what I'm trying to do?
#6

[eluser]Nick_MyShuitings[/eluser]
Code:
$this->load->library('session');
$object = json_encode($somegiantobjectyouhave); //I prefer json to serialize cuz of the possibilities for easy ajax
$this->session->set_userdata('some_name', $object);

// blah blah blah

$new object = json_decode($this->session->userdata('some_name'));
var_dump($new_object);
#7

[eluser]stormbytes[/eluser]
Okay so let me see if I got this...

(session loads automatically - I don't think you need to load it)

Code:
// Assuming a php class -

$object = new Object;

$object->some_property = 'foo';
$object->some_other_property = 'bar';


// Now we store it...

$object = serialize($object);
$this->session->set_userdata('object', $object);

// stuff happens....... views are loaded.. whatever...

// some more stuff happens....

// now we decide to go back & reconstitute our object

$new_object = unserialize($this->session->userdata('object');

// and we put it to work...

echo new_object->some_property // should return 'foo'


That look right?
#8

[eluser]Nick_MyShuitings[/eluser]
That should do it, I don't see any obvious errors there. Does that throw errors at you?

EDIT.

you did have a small error at the very end when you try to echo... forgot the $ in front of new_object
#9

[eluser]stormbytes[/eluser]
Well thought I'd run it by you first, but I'll give it a shot now...

Do you do freelance work?
#10

[eluser]Nick_MyShuitings[/eluser]
Yeap, Its what puts the bread on the table. And if its CI related its about 900x better then the nasty Drupal/Magento/Symfony work I sometimes have to take... lol Smile




Theme © iAndrew 2016 - Forum software by © MyBB