j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

UTF8 Compatibitlity #2

Closed ghost closed 14 years ago

ghost commented 14 years ago

To make Idiorm UTF-8 compatible, add:

array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")

to the setup_db function in the main class.

Code:

    private static function setup_db() {
        if (!is_object(self::$db)) {
            if (self::$config['username'] && self::$config['password']) {
                self::$db = new PDO(
                    self::$config['connection_string'],
                    self::$config['username'],
                    self::$config['password'],
                    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                );
            } else {
                self::$db = new PDO(self::$config['connection_string']);
            }
            self::$db->setAttribute(PDO::ATTR_ERRMODE, self::$config['error_mode']);
        }
    }
j4mie commented 14 years ago

This is actually slightly more tricky that it looks. If I use the code you supplied, it creates the assumption that if a username and password are supplied, the database being used must be MySQL. This isn't necessarily the case - for example the MS SQL driver also requires these options: http://www.php.net/manual/en/ref.pdo-dblib.php . Although Idiorm probably doesn't even support MS SQL (I haven't tested it), I'm a bit reluctant to build in this assumption.

My current idea is to keep the API fairly low level here, and add a driver_options setting, which would pass a raw array of settings through to the PDO constructor if supplied. So the full initialisation for MySQL would look like this:

ORM::configure('mysql:host=localhost;dbname=my_database');
ORM::configure('username', 'database_user');
ORM::configure('password', 'top_secret');
ORM::configure('driver_options', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

Do you have any thoughts on this solution?

j4mie commented 14 years ago

Add 'driver_options' setting to allow driver-specific connection options to be passed through to the PDO constructor. Closed by 6547b15aee997fb0db43c7fc0b1f33110993e215

ghost commented 14 years ago

Sorry about my late reply. The 'driver_options' config setting sounds great. Looking forward to an updated version of the class. Btw, are there any other changes/fixes planned?

j4mie commented 14 years ago

Hi,

This setting was added and documented in SHA: 6547b15aee997fb0db43c7fc0b1f33110993e215 . There are a few other fixes in this release. Any suggestions for other changes for the next version would be appreciated!