[CDBI] Re: uniqueness of objects in memory??
perrin at elem.com
Tue Oct 25 05:18:54 BST 2005
On Mon, 2005-10-24 at 16:34 -0700, Phil Mitchell wrote:
> For sanity's sake, here's the sequence:
> User logs in and User object is saved to session
> User has a prefs object
> User wants to edit prefs
> Prefs object is retrieved from db
> Prefs object is updated
> User->prefs is not refreshed
Okay, this is the sort of thing I meant by the sequence of actions.
This makes it clear what the issue is.
The object index works by keeping a hash of weak references to all
objects in the current process as they are loaded from the database. It
does this in the constructor. Things you put in the session are never
loaded from the database again, so they don't get registered in the
object index. The session just runs the object through a
Storable::thaw, without calling the constructor.
I would advise you to never put CDBI objects in a session, or otherwise
serialize them with Storable. Besides the fact that they don't call the
CDBI constructor, you are also probably going to overwrite things in
your database and lose changes. When you load the User object from the
session, it has the same values it had when you put it in there, even if
the database has been updated since then. If you have some code that
manipulates a User object and saves it to the db but not to the session,
you'll lose that change when you get the User from the session next
time. It gets even more complex when you have has_a relationships,
since the parent object keeps a reference internally to the other one,
and so both will get serialized and stored in the session, compounding
the chances for lost updates.
If you want to keep things in your session, stick to simple values, like
the ID of your User object.
More information about the ClassDBI