jump to navigation

Saving a model object on session vars February 13, 2006

Posted by rossoft in CakePHP.
trackback

I am coding the typical cart application. One of the model classes are the cart itself. This class is a normal class, it doesn’t extends AppModel. That object is saved in the session.

When you save a instance of the class in the session there isn’t any problem; but for reading it from the session, you need to have defined the class of the object BEFORE of the call session_start(), if you don’t then you will get a incomplete php object (this is a simple array of it’s properties, you can’t call any of it’s methods).

CakePHP does the call session_start() before loading the classes, so you must do a little hack. A simple hack that seems to work (I don’t know if it has side effects) is to close and reopen the session every time you read a variable (I don’t know if this is too slow, but works…):

I have changed that to the component /app/controllers/components/session.php

function read($name = null)
{
session_write_close();
session_start();
return $this->CakeSession->readSessionVar($name);
}
Advertisements

Comments»

1. rossoft - June 22, 2006

There’s another method much better.
Use serialize + unserialize with the standard SessionComponent.

$this->Session->write(‘somename’,serialize($object));

$object=unserialize($this->Session->read(‘somename’));

2. Onur - September 3, 2006

hi, sorry for my english. i have a problem about sessions on internet explorer. When i use $this->Session->write method or standart $_SESSION assignment in controller, internet explorer cannot save my session variables ( cannot set cookie ). it works fine on firefox. Otherwise when i try this thing in standart php procedures ( not using cake framework system, exp. session_start(); $_SESSION[‘test’] = 1; ) , it works fine both browsers.
Thanks for help.

3. rossoft - September 3, 2006

IE doesn’t allow cookies in domain names with the character underscore like my_dev.wordpress.com but firefox supports it. Maybe it’s your case.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: