Doctrine2: How to set all tables to collate with UTF8

前端 未结 14 973
夕颜
夕颜 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:38

    @ORM\Table(name="user", options={"collate"="utf8_unicode_ci", "charset"="utf8 ", "engine"="InnoDB"})

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

    Since DoctrineBundle 2.0, utf8mb4 is the default value for MySQL. So, you don't need to configure anything anymore.

    See https://github.com/doctrine/DoctrineBundle/blob/master/UPGRADE-2.0.md

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

    The charset: UTF8 option is just useful to ask Doctrine to execute SET NAMES UTF-8 on each page. I don't have any specific configuration for Doctrine, and my tables are by default in utf8_general_ci InnoDB. Read this part of the documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/faq.html#how-do-i-set-the-charset-and-collation-for-mysql-tables, it answers your question:

    You can’t set these values inside the annotations, yml or xml mapping files. To make a database work with the default charset and collation you should configure MySQL to use it as default charset, or create the database with charset and collation details. This way they get inherited to all newly created database tables and columns.

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

    The behavior of collate has changed in doctrine: http://www.doctrine-project.org/jira/browse/DDC-2139

    The collation is now set to utf8_unicode_ci if you don't specify anything at the table level. The way to go right now is to add it to options in your table declaration:

    /**
     * @ORM\Table(options={"collate"="utf8_swedish_ci"})
     * @ORM\Entity
     */
    

    This will generate the correct collation for the table:

    $ php app/console doctrine:schema:update --dump-sql --env=test | grep swedish
    ...  DEFAULT CHARACTER SET utf8 COLLATE utf8_swedish_ci ENGINE = InnoDB;
    

    I've filed an issue for the documentation to be updated to reflect this behaviour.

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

    I had have the same problem, and after reading this documentation from doctrine project page I decided to abandon solution with yml file.

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

    I create manually my data base with with UTF8 collate (ex. with phpmyadmin). If I do this, all tables create with command doctrine:schema:create will have collate utf8.

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