LoginSignup
0
0

More than 5 years have passed since last update.

Doctrineでカラムごとに文字コードとコレーションを設定する

Posted at

昔調べた時に見当たらないと思ってたんだけど、よく調べたらあったので書いておく。

Doctrine - 21.2.1. @Column

テーブルのコレーションは collate なのにカラムのコレーションは collation っていう罠が。 MySQL 使ってるとコレーション設定したいケースは割とあると思うのでちゃんと対応しててえらいですね。

App\Entity\User:
  type: entity
  table: users
  options:
    charset: utf8mb4
    collate: utf8mb4_general_ci
  fields:
    code:
      type: string
      length: 40
      options:
        charset: utf8mb4
        collation: utf8mb4_bin

ちなみに Symfony だと app/config/config.yml でテーブルの文字コードとコレーションの一括設定できます。

doctrine:
  dbal:
    driver:   pdo_mysql
    host:     "%database_host%"
    port:     "%database_port%"
    dbname:   "%database_name%"
    user:     "%database_user%"
    password: "%database_password%"
    charset:  utf8mb4
    default_table_options:
      charset: utf8mb4
      collate: utf8mb4_general_ci
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0