简体   繁体   中英

UTF-8 Zend Framework and Doctrine 2

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.

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.

I was facing the same problem about 6 months ago, and I solved this problem by writing following line at

application.ini

resources.db.params.charset = "utf8"

May help you...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM