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

前端 未结 8 1674
暖寄归人
暖寄归人 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:49

    I have this in my bootstrap:

        protected function _initDoctrineLibrary()
    {
        require_once('Doctrine/Doctrine.php');
        $this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine', 'autoload'),'Doctrine');
    
        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute(
            Doctrine::ATTR_MODEL_LOADING,
            Doctrine::MODEL_LOADING_CONSERVATIVE
        );
        $config = $this->getOption('doctrine');
        $conn = Doctrine_Manager::connection($config['dsn'],'doctrine');
        $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
        return $conn;
    }
    

    where in the application.ini you see

    doctrine.dsn = "mysql://user:password@host/databasename"
    

    I think you can do something similar

    0 讨论(0)
  • 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 = '...'
    ....
    
    0 讨论(0)
提交回复
热议问题