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

前端 未结 8 1713
暖寄归人
暖寄归人 2020-12-28 08:22

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

8条回答
  •  轮回少年
    2020-12-28 08:52

    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 = '...'
    ....
    

提交回复
热议问题