Re: DataObject connect problem using sessions
| From: | Alan Knowles | Date: | Fri, 06 Dec 2002 03:38:58 +0000 |
| Subject: | Re: DataObject connect problem using sessions | ||
| References: | 1 2 3 | Groups: | php.pear.dev php.pear.general |
| Request: | Send a blank email to pear-dev+get-11403@lists.php.net to get a copy of this message | ||
Ok, most of that went in :)
if ($this->_database_dsn_md5 && $connections[$this->_database_dsn_md5]) {
OK - I just added an @ here.
the other fix to connect I've left out - is it broken without this? - as I think It would break sergey's dynamic database setting stuff.
examples =OK addedI've used $this->_connect(), rather than $this->_get_table(); fixed the $field bug :) Regards Alan Stijn de Reede wrote:
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:------------------------------------------------------------------------ Index: DataObject.php =================================================================== RCS file: /repository/pear/DB_DataObject/DataObject.php,v retrieving revision 1.40 diff -u -r1.40 DataObject.php --- DataObject.php 5 Dec 2002 03:55:13 -0000 1.40 +++ DataObject.php 5 Dec 2002 13:34:55 -0000 @@ -934,7 +934,7 @@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{ $connections = &PEAR::getStaticProperty('DB_DataObject','connections');- if ($this->_database_dsn_md5) {// already connected + if ($this->_database_dsn_md5 && $connections[$this->_database_dsn_md5]) {// already connected if (PEAR::isError($connections[$this->_database_dsn_md5])) { DB_DataObject::raiseError( $connections[$this->_database_dsn_md5]->message,@@ -952,10 +952,10 @@$dsn = @$this->_database_dsn;if (!$dsn) { - if (!$this->_database) { - $this->_database = @$options["table_{$this->__table}"]; - } - if (@$this->_database) { + if ($database = @$options["table_{$this->__table}"]) { + if (!$this->_database) { + $this->_database = @$options["table_{$this->__table}"]; + } $dsn = $options["database_{$this->_database}"]; } else if ($options['database']) { $dsn = $options['database'];@@ -1294,14 +1294,24 @@* joinAdd - adds another dataobject to this, building a joined query. * * example (requires links.ini to be set up correctly) - * $pi = new DataObjects_Product_images; - * $p = new DataObjects_Product; - * $i = new DataObject_Images; - * $p->id = 24; // set the product id to 24 - * $pi->joinAdd($p); // add the product connection - * $pi->joinAdd($i); // add the image connectoin - * $pi->find(); - * while ($pi->fetch()) { + * // get all the images for product 24 + * $i = new DataObject_Image(); + * $pi = new DataObjects_Product_image(); + * $pi->product_id = 24; // set the product id to 24 + * $i->joinAdd($pi); // add the product_image connectoin + * $i->find(); + * while ($i->fetch()) { + * // do stuff + * } + * // an example with 2 joins + * // get all the images linked with products or productgroups + * $i = new DataObject_Image(); + * $pi = new DataObject_Product_image(); + * $pgi = new DataObject_Productgroup_image(); + * $i->joinAdd($pi); + * $i->joinAdd($pgi); + * $i->find(); + * while ($i->fetch()) { * // do stuff * } *@@ -1320,7 +1330,7 @@DB_DataObject::raiseError("joinAdd: called without an object", DB_DATAOBJECT_ERROR_NODATA,PEAR_ERROR_DIE); } - $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?) */ $links = PEAR::getStaticProperty('DB_DataObject', "{$this->_database}.links"); @@ -1344,7 +1354,7 @@ /* otherwise see if there are any links from this table to the obj. */ - if (($field === false) &&isset($links[$this->__table])) { + if (($ofield === false) && isset($links[$this->__table])) { foreach ($links[$this->__table] as $k => $v) { /* link contains {this column} = {linked table}:{linked column} */ $ar = explode(':', $v);