Re: DataObject connect problem using sessions
| From: | Stijn de Reede | Date: | Thu, 05 Dec 2002 13:56:59 +0000 |
| Subject: | Re: DataObject connect problem using sessions | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-2906@lists.php.net to get a copy of this message | ||
Ok, I think I've fixed it. The errors were in two sections of
DataObject::_connect().
Firstly, only DataObject::_database_dsn_md5 is checked to see if a
connection exists, this variable keeps it's value when in a session, but
the connection resource loses it's value. So I've changed the check to this:
- if ($this->_database_dsn_md5) {
+ if ($this->_database_dsn_md5 &&
$connections[$this->_database_dsn_md5]) {
Maybe the first part of this check isn't necessary now anymore, but I'm
not very keen on changing things I'm not sure about.
Secondly, it appears that the previous patch (to preserve the value of
DataObject::_database) was a bit wrong in the DataObject::_connect()
method. I think I've fixed it now, maybe Sergey Lipnevich wants to check
if it still works for him.
Then, still more, allthough little to do with this problem, I've
rewritten the examples for the DataObject::joinAdd() method, as they
were incorrect. The current examples are taken from my 'production'
code, and consequently, should work.
Also, I fixed one more documentation line there:
- $this->_get_table(); /* ??? eh - load this tables config? = eg.
links if req.*/
+ $this->_get_table(); /* make sure $this->_database is set
(should this be done in a different way?) */
And finally, I corrected a typo of you Alan, I think:
- if (($field === false) &&isset($links[$this->__table])) {
+ if (($ofield === false) && isset($links[$this->__table])) {
As $field isn't declared anywhere and was changed to $ofield.
The diff is attached.
So, that's it for me, I hope I've been of some help to you all.
Stijn
Stijn De Reede wrote:
> 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
>>
>