DB_DataObject and specific mysql client encoding
| From: | Markus Fischer | Date: | Mon, 28 Aug 2006 14:14:09 +0000 |
| Subject: | DB_DataObject and specific mysql client encoding | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-25000@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
today I faced the problem that data returned from the and sent to the
database was not in UTF8 when using DB_DataObject.
The core problem as it seems to me is that the mysql client library on
most if not on all systems is compiled with 'latin1' as default charset.
If you manually connect to the database you can use e.g.
mysql_query("SET NAMES UTF8", $rLink);
but this is not so easy with DB_DataObject.
I opted for the following solution and would like to know what others
think and if it would possible to have this support directly in
DB_DataObject. Or a cleaner solution, if possible.
I made a class "Custom_DB_DataObject extends DB_DataObject" and
re-implemented _connect(). It's intersting that the real _connect() is
marked as private which would prevent overriding it, luckily PEAR is
PHP4 code so this is not enforced:
public function _connect() {
parent::_connect();
if
(isset($GLOBALS['_DB_DATAOBJECT']['CONFIG']['mysql']['client_encoding']))
{
$DB =
$GLOBALS['_DB_DATAOBJECT']['CONNECTIONS'][$this->_database_dsn_md5];
if ($DB->phptype == 'mysql') {
$DB->query("SET NAMES
{$GLOBALS['_DB_DATAOBJECT']['CONFIG']['mysql']['client_encoding']}");
}
}
return true;
}
This requires initializing the DataObject with a structure like
array(
'require_prefix' => 'DataObjects/',
[...]
'mysql' => array(
'client_encoding' => 'UTF8'
),
[...]
);
It's very specific to mysql of course, that's why i used the nested
array. I see problems in the area that many people load the config from
a real .ini which doesn't support one more nesting, but technically it
seemed the cleanest solution to e.g. have more database-specific overrides.
Maybe there's an more elegant way to solve this.
- - Markus
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFE8vox1nS0RcInK9ARAoV/AJ9g0ACdBy+aHvXwDqZrlfqqXJva8QCgzV4Q
ruspytH2Ni9Hbl+kiKUU2vw=
=LVc0
-----END PGP SIGNATURE-----