Enforce utf8 for multiple db connections

Posted by Felix Geisendörfer, on Nov 10, 2007 - in PHP & CakePHP » DataSources, Models & Behaviors

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Hey folks,

this is just a quick update for Dessert #6 - MySql & UTF-8. I've been using the approach outlined in that old post pretty much until today, when I realized that it has two major flaws: It does not work when using multiple db connections (i.e. using load balancing or connecting to a 3rd party db), and it might interfere with other databases that don't need this utf8 thing to be set.

In order to fix it I came up with this improved version of the code.

php
  1. class AppModel extends Model{
  2.    function __construct($id = false, $table = null, $ds = null) {
  3.       static $utf8Enabled = array();
  4.       if (!isset($utf8Enabled[$this->useDbConfig])) {
  5.          $db =& ConnectionManager::getDataSource($this->useDbConfig);
  6.          if (low(get_class($db)) == 'dbomysql') {
  7.             $db->execute('SET NAMES utf8');
  8.          }
  9.          $utf8Enabled[$this->useDbConfig] = true;
  10.       }
  11.       parent::__construct($id, $table, $ds);
  12.    }
  13. }

This will make sure the 'SET NAMES utf8' query is only run for MySql connections and if there are more then 1 used, then each gets the query executed exactly once.

HTH,
-- Felix Geisendörfer aka the_undefined

Print this Post | Digg This | Stumble It | Delicious

7 Comments

Mariano Iglesias on Nov 10, 2007:

Or if you are using CakePHP 1.2 just set the 'encoding' setting on your configuration per connection, such as:

var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'password',
'database' => 'database',
'schema' => '',
'prefix' => '',
'encoding' => 'utf8'
);

Felix Geisendörfer on Nov 10, 2007:

Mariano: Wooooooo! How cool is that, thanks : p.

Dieter_be on Nov 11, 2007:

Ah, because you mentioned the dessert series I couldn't focus on your blogpost itself, it made me think of the great face-in-cake picture :')

PHPDeveloper.org on Nov 12, 2007:

Felix Geisendorfer's Blog: Enforce utf8 for multiple db connections...

Felix Geisendorfer has another quick CakePHP tip - an update from ......

[...] Felix Geisendorfer has another quick CakePHP tip - an update from a previous tip for forcing utf8 connections on multiple databases: This is just a quick update for Dessert #6 - MySql & UTF-8. I’ve been using the approach outlined in that old post pretty much until today, when I realized that it has two major flaws: It does not work when using multiple db connections (i.e. using load balancing or connecting to a 3rd party db), and it might interfere with other databases that don’t need this utf8 thing to be set. [...]

open source cms on Nov 26, 2007:

ConnectionManager::getDataSource($this->useDbConfig);

open source cms on Nov 26, 2007:

sorry, but your blog cut my text because I use < ... no comment

Add a comment