Re: DB_DataObject and specific mysql client encoding
| From: | Justin Patrin | Date: | Mon, 28 Aug 2006 15:04:35 +0000 |
| Subject: | Re: DB_DataObject and specific mysql client encoding | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-25006@lists.php.net to get a copy of this message | ||
On 8/28/06, Markus Fischer <markus@fischer.name> wrote:
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'])) {One other solution would be to create a DO (after you set up config, of course) then use getDatabaseConnection() to get the DB object, then run your query. -- Justin Patrin$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.