Re: [Fwd: Re: [PEAR-DEV] serialize datatype for MDB2]

From: Date: Tue, 06 Jun 2006 07:25:24 +0000
Subject: Re: [Fwd: Re: [PEAR-DEV] serialize datatype for MDB2]
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-42777@lists.php.net to get a copy of this message
Lukas Smith wrote:
+1 for the feature itself, but a big -1 if it is not very strict implemented. in most cases arrays inserted accidentally, in that case i'd prefer an fatal or warning. $array = array('foo'); $mdb2->quote($array); // fatal error
Fatal error would be a bit harsh. Anyways specifying no type means "guess" for all other types. Serialize seems to be the best guess for an array. Note that the same is not done for objects due to the existance of __toString() in PHP 5.x
$mdb2->quote($array, 'text') // fatal error
That will cause a notice.
$mdb2->quote($array, 'serialize') // ok
Ok :)
i'm a bit unsure about the deserialization. there only two ways of deserialization which makes sense are text and serialize. in a defensive manner you can try to detect an serialized array/object and throw an warning if something like int is requested. $result = $mdb2->query('SELECT someint, somearray FROM foo', array('integer', 'integer')); $row = $result->fetchRow() //warning error
In this case the value for the second column would be 0 as the result is casted to an integer. Currently everything is forced to the given type. Note this means that if you want to work with large integers with bcmath, you will need to pass them in as text. Also not that in the following case the value for the someint column will be false as the unserialize() call will fail: $result = $mdb2->query('SELECT someint, somearray FROM foo', array('serialize', 'serialize')); $row = $result->fetchRow() I am describing the current implementation in my framework here, where I keep a modified version of MDB2. However there is another possible solution here. MDB2 has a module concept. This means that the datatype handling can be replaced with a custom implementation. $mdb2->loadModule('Mymodule', 'datatype', false); This would for example load the class Mymodule from peardir/MDB2/MyModule.php and store it in the instance in the property "datatype". This means that the normal datatype module would not be used. Using __call() overloading in PHP5 you could wrap around the actual datatype module to add custom datatypes. Also you could simply do: require 'Mymodule.php'; // or use __autoload() $mdb2->datatype = new Mymodule(); regards, Lukas

« previous php.pear.dev (#42777) next »