PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » MDB2 » Bug #4679

Cannot serialize wrapped DB-objects

Details

Request #4679Cannot serialize wrapped DB-objects
Submitted2005-06-25 10:59 UTC
Frompear at fobie dot net
StatusWont fix
PackageMDB2
PHP Version2.0.0beta5
OSLinux
Roadmaps(Not assigned)

Comments

[2005-06-25 10:59 UTC] pear at fobie dot net

Description:
------------
When trying to serialize a wrapped DB-object will give an error. Supplying the __sleep() and __wakeup()-functions will make this problem go away. Tho, I'm not really sure how one can make this code honor the currently state (if in a transaction etc) -- so my code is no good.

I tried to use the wrapped DB-class with PEAR::Log, this is why I found this error.

Reproduce code:
---------------
<?php
require_once('MDB2/Wrapper/peardb.php');

$db = &DB::connect($dsn);
$serialize = serialize($db);
?>

Expected result:
----------------
uniquely serialized object.

Actual result:
--------------
Fatal error: Call to undefined function: MDB2_Driver_mysql::__sleep(). in /var/www/shared/PEAR/php/MDB2.php on line 1550

[2005-06-29 12:23 UTC] smith at backendmedia dot com

what version of PHP are you using?
what version of MDB2 are you using?

[2005-08-23 11:35 UTC] pear at fobie dot net

Sorry for the delay. Didn't recieve any notification and had a hard drive crash.

I'm using PHP 5.0.4 and MDB2 2.0.0beta5. For those of you who's having the same problem, add a __sleep()-function to MDB2/Driver/mysql.php (or which driver you're using).

[2005-08-23 22:31 UTC] pear at fobie dot net

That sounds strange. Have you made sure that $dsn is a correct DSN and that the object really is the correct object -- and that DB.php isn't loaded prior to this?

The PHP manual states that "serialize() handles all types, except the resource-type." -- which makes me believe that your $db object maybe didn't contain any resource-type (maybe the object variable $connection)?

I have no problem to reproduce this bug under PHP 5.0.4 and beta5. Here's a lame patch. I realized that MDB2_Driver_Common would be a better place later when investigated how DB/common.php uses __sleep(). :)

--- trunk/components/PEAR/MDB2.php (revision 26)
+++ trunk/components/PEAR/MDB2.php (revision 27)
@@ -1114,6 +1114,20 @@
}

// }}}
+ // {{{ __sleep()
+
+ /**
+ * returns an array with names of the variables to serialize
+ */
+ function __sleep()
+ {
+ // XXX Should check if $in_transaction
+ return array('dsn', 'db_index', 'dbsyntax', 'database_name',
+ 'connected_database_name', 'fetchmode', 'options',
+ 'opened_persistent');
+ }
+
+ // }}}
// {{{ errorInfo()

/**