How to define the use of utf-8 in Doctrine 2 in Zend Framework application.ini, when using Bisna

If you are not using Bisna, you could simply do something like the following:

Pass the config stuff directly to EntityManager's connection options (although driverOptions is not documented)

// $options is a simple array to hold your data
$connectionOptions = array(
    'driver'   => $options['conn']['driv'],
    'user'     => $options['conn']['user'],
    'password' => $options['conn']['pass'],
    'dbname'   => $options['conn']['dbname'],
    'host'     => $options['conn']['host'],
    'charset'  => 'utf8',
    'driverOptions' => array(
        1002 => 'SET NAMES utf8'
    )
);

$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);

I'm using the following custom bootstrap resource to initialize doctrine therefore $options is in application.ini and is accessible there by $this->getOptions();

// \library\My\Application\Resource\Doctrine.php
class My_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
 {

    public function init()
    {
       $options = $this->getOptions();
       $config = new \Doctrine\ORM\Configuration();
       //doctrine autoloader, config and other initializations
       ...
       $connectionOptions = array(
               .... //see above
       );
       $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
       $registry = Zend_Registry::getInstance();
       $registry->em = $em;
       return $em;
    }
}

It will bootstrap automatically if you put in application.ini

resources.doctrine.conn.host = '127.0.0.1'
resources.doctrine.conn.user = '...'
resources.doctrine.conn.pass = '...'
....

works fine for me

resources.doctrine.dbal.connections.default.parameters.driverOptions.1002 = "SET NAMES 'UTF8'"

1002 is the integer value of PDO::MYSQL_ATTR_INIT_COMMAND:

Command to execute when connecting to the MySQL server. Will automatically be re-executed when reconnecting. Note, this constant can only be used in the driver_options array when constructing a new database handle.


this worked for me. config/autoload/doctrine.local.php

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host' => 'localhost',
                    'port' => '3306',
                    'user' => '...',
                    'password' => '...',
                    'dbname' => '...',

                    'driverOptions' => array(
                        \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
                    )

                ),

            )
        )
    )
);