Doctrine2: How to set all tables to collate with UTF8

前端 未结 14 971
夕颜
夕颜 2020-12-08 02:55

I am using Doctrine with Symfony2. My config.yml file looks something like this:-

Doctrine Configuration

doctrine:
    dbal:
        driver: %data         


        
相关标签:
14条回答
  • 2020-12-08 03:30

    Use code below to set collation, engine and charset (annotation example):

    /**
    * @ORM\Table(
    *     name="temporary", 
    *     options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"}
    * )  
    * @ORM\Entity
    */    
    

    Source: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-column

    0 讨论(0)
  • 2020-12-08 03:31

    I suggest you try add this setting to your Doctrine configuration:

        options:
            1002: "SET NAMES 'UTF8' COLLATE 'utf8_unicode_ci'"
    

    So it looks like this:

        doctrine:
            dbal:
                driver: %database_driver%
                host: %database_host%
                port: %database_port%
                dbname: %database_name%
                user: %database_user%
                password: %database_password%
                charset: UTF8
                options:
                    1002: "SET NAMES 'UTF8' COLLATE 'utf8_unicode_ci'"              
    

    As a reference Doctrine Configuration: http://symfony.com/doc/2.0/reference/configuration/doctrine.html#reference-dbal-configuration

    And in case you are using MySql: http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

    BTW. I had problems with displaying polish characters and adding only set names without collation (as below) helped.

        options:
            1002: "SET NAMES 'UTF8'
    
    0 讨论(0)
  • 2020-12-08 03:32

    I've got this problems with fetching data which includes polish characters from database. It looks like:

    effects of displaying data from db

    my config.yml is like:

    doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        options:
             1002:  "SET NAMES 'UTF8'"
    

    and I've been searching for an answer for couple of hours - Im so tired of this. "1002: SET NAMES 'utf8'" Doesnt work for me. But when I tryed to access my db from simple php script (out of symfony) the effect was the same but when I added line:

    mysql_query("SET NAMES 'utf8'");
    

    it worked properly. So it seems to be little strange. All the tables in my db have 'utf8_unicode_ci' set.

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

    I met the same problems. By search the code, I find the code in vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php

    if ( ! isset($options['collate'])) {
      $options['collate'] = 'utf8_unicode_ci';
    }
    

    So I changed it to 'utf8_general_ci', and it worked. All of tables' collation changed to utf8_general_ci after rebuild, but I still have one question: when I changed the annotations, I got the following error message:

    [Creation Error] The annotation @ORM\Table declared on class Gvsun\UserBundle\Entity\User does not have a property named "collation". Available properties: name, schema, indexes, uniqueConstraints, options

    I search the documentation of Doctrine, but I didn't find the 'option' property, can someone tell me how to use options property, I think it might be able to change collate in annotations settings?

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

    I have successfully used options: collate in YML configuration of Symfony 2.7.

    MyBundle\Entity\Company:
        type: entity
        repositoryClass: MyBundle\Repository\CompanyRepository
        table: company
        options:
            collate: utf8_general_ci
    
    0 讨论(0)
  • 2020-12-08 03:36

    if you are looking for a way to have migrations be created correctly you should setup the connection

    you can set the default_table_options connection option to achieve this: in symfony this is done through:

    doctrine:
        dbal:
            default_table_options:
                charset: utf8
                collate: utf8_general_ci
    

    for those looking to do it in plain doctrine this translates to doctrine connection option defaultDatabaseOptions, and is passed to the entity manager together with your database credentials etc:

    [
        ...
        'driver' => ...
        'user' => ...
        ...
        'defaultDatabaseOptions' => [
             'charset' => 'utf8',
             'collate' => 'utf8_general_ci'
        ]
    ]
    
    0 讨论(0)
提交回复
热议问题