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

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

    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

    0 讨论(0)
  • 2020-12-28 08:35

    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.

    0 讨论(0)
  • 2020-12-28 08:41

    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.

    0 讨论(0)
  • 2020-12-28 08:41

    For LoSo library and Doctrine 2 and MySQL add

    resources.doctrine2.connection.driverOptions.1002 = "SET NAMES 'UTF8'"
    

    to your application.ini

    0 讨论(0)
  • 2020-12-28 08:43

    this worked for me. config/autoload/doctrine.local.php

    <?php
    return array(
        'doctrine' => array(
            'connection' => array(
                'orm_default' => array(
                    'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                    'params' => array(
                        'host' => 'localhost',
                        'port' => '3306',
                        'user' => '...',
                        'password' => '...',
                        'dbname' => '...',
    
                        'driverOptions' => array(
                            \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
                        )
    
                    ),
    
                )
            )
        )
    );
    
    0 讨论(0)
  • 2020-12-28 08:44

    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.

    0 讨论(0)
提交回复
热议问题