Reputation: 1898
I am failing to get UTF-8 characters to display correctly when querying them from the database using Doctrine. I have tried everything I could find on the internet (Eg: setCharset seemed to be the best option). If I print UTF-8 characters directly from PHP they are displayed correctly, so it is not a problem of output to the browser. Here are my configurations:
application.ini
doctrine.conn.host = 'localhost'
doctrine.conn.user = 'someuser'
doctrine.conn.pass = 'somepass'
doctrine.conn.driver = 'pdo_mysql'
doctrine.conn.dbname = 'zoo'
doctrine.path.models = APPLICATION_PATH "/models"
bootstrap method for doctrine
$classLoader = new \Doctrine\Common\ClassLoader(
'Doctrine',
APPLICATION_PATH . '/../library/'
);
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$cache = new \Doctrine\Common\Cache\ArrayCache();
$config->setMetadataCacheImpl( $cache );
$config->setQueryCacheImpl( $cache );
$driver = $config->newDefaultAnnotationDriver(
APPLICATION_PATH . '/models'
);
$config->setMetadataDriverImpl( $driver );
$config->setProxyDir( APPLICATION_PATH . '/models/Proxies' );
$config->setAutoGenerateProxyClasses( true );
$config->setProxyNamespace( 'App\Proxies' );
$connectionSettings = $this->getOption( 'doctrine' );
$conn = array(
'driver' => $connectionSettings['conn']['driver'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['host'],
);
$entityManager = \Doctrine\ORM\EntityManager::create( $conn, $config );
$entityManager->getConnection()->setCharset('utf8');
$registry = Zend_Registry::getInstance();
$registry->entityManager = $entityManager;
return $entityManager;
Any help is highly appreciated.
Thank you.
Upvotes: 1
Views: 1407
Reputation: 8046
I was struggling with this for a long time too but found the solutions for my case. You have to add an EventSubscriber to your EntityManager.
$entityManager = \Doctrine\ORM\EntityManager::create( $conn, $config );
$entityManager->getEventManager()
->addEventSubscriber(
new \Doctrine\DBAL\Event\Listeners\MysqlSessionInit('utf8', 'utf8_unicode_ci')
);
Hopefully this will help you.
Upvotes: 1
Reputation: 302
What about doctrine.conn.driverOptions.1002 = "SET NAMES 'UTF8'"
? Maybe it helps.
Oh i saw, that you're bootstrapping it manually. You have to add the lines in the connection array too.
Maybe you will have a look into the Bisna-library (Tutorial how to use it), it helps you to integrate doctrine2 into the ZF.
Upvotes: 4
Reputation: 4392
I was facing the same problem about 6 months ago, and I solved this problem by writing following line at
resources.db.params.charset = "utf8"
May help you...
Upvotes: 1