Re: DataObject connect problem using sessions
| From: | Stijn de Reede | Date: | Thu, 05 Dec 2002 09:25:06 +0000 |
| Subject: | Re: DataObject connect problem using sessions | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-2903@lists.php.net to get a copy of this message | ||
Ok, firstly, is register_globals turned on? I think you should code with it turned Off, but with your code it should be On.
Possibly this is due to the fact that you cannot store resource variables in a session, see http://www.php.net/manual/en/function.session-register.php at the note:
---------------
Note: It is not currently possible to register resource variables in a session. For example, you can not create a connection to a database and store the connection id as a session variable and expect the connection to still be valid the next time the session is restored.
---------------
I think DataObject should maybe use the magic functions (http://www.php.net/manual/en/language.oop.magic-functions.php) __sleep and __wakeup to close and restore the DB connection. Or, even better maybe, before executing a query, it should check if a connection exists, and if not, create one.
I'll look into it (I'm not a developer of DataObject, but I'm planning on using DataObjects in combination with sessions too). I'll CC this to Alan Knowles (lead developer) in case he's not reading this mailinglist (yeah, I know, i'm bugging you a lot lately :-)).
Stijn
L. Caplan wrote:
I'm faced with a rather odd bug for which I think I've figured out the
cause, yet can't really think of a good solution. The context where I
originally found it is a bit different that what I describe here, but I
wrote some simple code to help narrow down the problem.
Consider the following code:
//sessiontest1.php
//----------------
include('myinclude.inc.php');
session_start();
if(!session_is_registered("testuser"))
session_register("testuser");
$testuser = new DataObjects_User;
$testuser->get('user_id', 1);
$testuser->active = 0;
$testuser->update();
//----------------
That code works perfectly, and the update goes off without a hitch.
Everything seems fine, however, in the next document, I try to used the
$testuser object which was stored in the session...
//sessiontest2.php
//----------------
include("myinclude.inc.php");
session_start();
print_r($testuser);
$testuser->active = 1;
$testuser->update();
//----------------
This *should* work, but instead, I get this error:
"Fatal error: Call to a member function on a non-object in DataObject.php
on line 1022"
The contents of that line are:
$results[$this->_DB_resultid] = $__DB->query($string);
so, further investigation revealed that $__DB was in fact NULL, as were
the $connections and $results variables set at the top of the function.
so, it appears as though calls to PEAR::getStaticProperty are returning
NULL values for some reason, though I've got no idea why.
If I create a new object of the same class (User) and use setFrom(), then
attempt to update, it works just fine. This issue only comes up on objects
which were created in other scripts, then stored in sessions.
I'd really like to figure out why this is happening, because as you can
imagine, it potentially affects a lot of functionality across my
application. Thanks very much in advance for any help!
Leigh Caplan
maelman@u.washington.edu