【问题标题】:Doctrine migrations table collation教义迁移表排序规则
【发布时间】:2015-05-12 08:31:13
【问题描述】:

试图找到一种以编程方式创建具有特定排序规则的表的方法,但似乎无法找到正确执行此操作的方法。我正在使用 "doctrine/doctrine-migrations-bundle": "2.1.*@dev" 和 Symfony 2.3,

我在我的 config.yml 中设置:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  LATIN1
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"

        entity_managers:
                    default:
                        auto_mapping: true

doctrine_migrations:
    dir_name: %kernel.root_dir%/../src/CF/EscritorioBundle/Migrations
    namespace: MyNameSpace\Migrations
    table_name: migrations
    name: Application Migrations

当我运行doctrine:database:create 工具时,它会使用 LATIN1 字符集和 latin1_swedish_ci 作为默认排序规则创建数据库。 然后我运行我的迁移,所有的表都是utf8_general_ci

查看了$schema->createTable() 函数,但找不到传递我需要的排序规则的方法。这里的正确解决方法是什么?

【问题讨论】:

    标签: php symfony doctrine-orm doctrine


    【解决方案1】:

    嗯,这有点难看,但我发现的唯一方法是在所有表之后生成另一组迁移,这些迁移改变了表并转换了编码。迁移 up()down() 函数看起来像:

    ......    
        /**
         * @param Schema $schema
         */
        public function up(Schema $schema)
        {
            // this up() migration is auto-generated, please modify it to your needs
            $this->addSql("ALTER TABLE MyTable CONVERT TO CHARACTER SET LATIN1 COLLATE latin1_general_ci");
        }
    
        /**
         * @param Schema $schema
         */
        public function down(Schema $schema)
        {
            // this down() migration is auto-generated, please modify it to your needs
            $this->addSql("ALTER TABLE MyTable CONVERT TO CHARACTER SET UTF8 COLLATE utf8_general_ci");
        }
    

    【讨论】:

      【解决方案2】:

      您可以设置 default_table_options 连接选项来实现此目的: 在 symfony 中,这是通过以下方式完成的:

      doctrine:
          dbal:
              default_table_options:
                  charset: latin1
                  collate: latin1_general_ci
      

      对于那些希望在简单的原则中执行此操作的人,这将转换为原则连接选项 defaultDatabaseOptions,并与您的数据库凭据等一起传递给实体管理器:

      [
          ...
          'driver' => ...
          'user' => ...
          ...
          'defaultTableOptions' => [
               'charset' => 'latin1',
               'collate' => 'latin1_general_ci'
          ]
      ]
      

      【讨论】:

        【解决方案3】:

        创建新表时,您不能添加表选项。因此无需使用 alter sql 进行后续迁移。

        未测试是否可以对表进行更新。

        /**
         * @param Schema $schema
         */
        public function up(Schema $schema)
        {
            // this up() migration is auto-generated, please modify it to your needs
            $table = $schema->createTable('new_table');
            $table->addOption('charset', 'latin1');
            $table->addOption('collate', 'latin1_general_ci');
        
            // ....
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多