How to define the use of utf-8 in Doctrine 2 in Zend Framework application.ini, when using Bisna
Solution 1
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.
Solution 2
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 = '...'
....
Solution 3
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'
)
),
)
)
)
);
Solution 4
It is possible to add it via application.ini, provided you use ZendX_Doctrine2 (at https://github.com/mridgway/ZendX_Doctrine2) with MySQL.
Then here's the line you need in application.ini:
resources.entitymanagerfactory.connectionOptions.driverOptions.1002 = "SET NAMES utf8"
(1002 == PDO::MYSQL_ATTR_INIT_COMMAND)
Don't forget to correctly set
default-character-set=utf8
in your my.cnf
Solution 5
Since this is for Doctrine 2, and ZendCasts is using Bisna, I believe you can just add this to your configuration.ini file
resources.doctrine.dbal.connections.default.parameters.driverOptions.charset = "utf8"
I'm not exactly sure how to test if it is sticking or not but let us know.
Related videos on Youtube
Comments
-
Doron about 4 years
The following ZendCasts cast, shows a way to use doctrine 2 in a zend framework environment.
Using this configuration, how can I make the connection use a utf-8 charset so the magic of"SET NAMES 'utf8'"
will happen ?What I'm really searching for is a way to configure it using the application.ini file.
If that's not possible using this configuration, how can this be done by code ? an_initDoctrine
method in the Bootstratp file ?Thank you.
UPDATE
It appears there's a post connect event which handles this, but I don't see how can I set it up via application.ini (if possible at all).
If not, can I set it up via a bootstrap method ? Will the bootstrap method run before any other doctrine connection code run, when relying on the Bisna library ? -
Doron about 13 yearsYou are correct regarding the use of Bisna, and the line you gave does have it's affect. However - according to doctrine-project.org/docs/dbal/2.0/en/reference/events.html, there's a post connect event to which I need to subscribe a MysqlSessionInit object with the utf-8 charset. Any idea ?
-
Vladimir Vukanac about 9 yearsYou are The Man! Thanx! Solution for "How to set Doctrine2 to connect to MySql using UTF-8 charset".
-
Eddie C. over 8 yearsThank you. I suggest to change the line
1002 => 'SET NAMES utf8'
intoPDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
-
Imeksbank about 5 yearsThank you. Works in Zend Expressive 3 as well.