How to define the use of utf-8 in Doctrine 2 in Ze

2019-02-01 21:43发布

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 ?

8条回答
姐就是有狂的资本
2楼-- · 2019-02-01 22:05

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.

查看更多
仙女界的扛把子
3楼-- · 2019-02-01 22:07

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 = '...'
....
查看更多
放荡不羁爱自由
4楼-- · 2019-02-01 22:08

You could set the default table charset like that to utf8:

// Create new Doctrine Manager instance
$doctrineManager = Doctrine_Manager::getInstance();

// Set charset to UTF8
$doctrineManager->setAttribute(
    Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET,
    'utf8'
);

Quote:

an _initDoctrine method in the Bootstratp file ?

Yes.

查看更多
beautiful°
5楼-- · 2019-02-01 22:12

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.

查看更多
何必那么认真
6楼-- · 2019-02-01 22:17

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

查看更多
老娘就宠你
7楼-- · 2019-02-01 22:17

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

查看更多
登录 后发表回答