chb has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I think I have a rather common problem, but SuperSearch and Google didn't reveal anything useful :-( I want to store a Class::DBI object in a CGI::Application::Session (which is just a wrapper around the well-known CGI::Session module). When I put the object via the param() method in the session object and retrieve it on the next request, Data::Dumper shows me a rather empty object with the correct class and the correct id (my primary key):
$xxx = bless( { 'id' => 14 }, 'Application::Foo::Bar' );
Any method call on the object fails with an error message like null: Can't locate object method "id" via package "Application::Foo::Bar" at /home/chb/work/xyz/site/application/XYZApp.pm line 576.\n Am I missing something fundamental here?

Replies are listed 'Best First'.
Re: Storing Class::DBI objects in a Session
by Tuppence (Pilgrim) on Nov 03, 2004 at 00:02 UTC

    If at all possible you should try to not store objects in the session. It is messy, and tracking down related bugs is not fun.

    Consider storing the key for your item in the session, and the retrieving it from the database again next page load.

    You may also wish to check if adding an explicit use Application::Foo::Bar at the top of your script helps, it is entirely possible that the Apache process that is failing has never compiled Application::Foo::Bar, and that is why it is complaining about there being no ID method.

Re: Storing Class::DBI objects in a Session
by bart (Canon) on Nov 02, 2004 at 23:28 UTC
    Well for a start, you still have to use your custom Class::DBI module. The methods belonging to the stored objects are not stored in the CGI::Session wrapper, so you'll still have to load them the standard way, with use. And you ought to connect to the database, but I'm sure just using your module ought to do that for you.

    I would think having the id for a row and the various classes knowing all of the details of what database and what table to use, should be enough to fetch the correct record from the database — even with so little info to go by.

Re: Storing Class::DBI objects in a Session
by perrin (Chancellor) on Nov 03, 2004 at 00:43 UTC
    Class::DBI is an interface for interacting with your database, not a generic object system. If you want generic objects that you can store in a session, just use Class::Accessor directly (that's what Class::DBI uses). Class::DBI objects have class data, code refs, and live database handles, which are unlikely to serialize cleanly.
Re: Storing Class::DBI objects in a Session
by Joost (Canon) on Nov 03, 2004 at 11:44 UTC