从实体生成db表时如何使用char而不是varchar?

zel*_*owy 15 symfony doctrine-orm

我正在使用string(32)而不是integer实体id字段(它模拟guid类型).但是从实体ids创建数据库表后,字段类型设置为varchar(32).我认为这不是最好的主意,guid总是长度= 32,所以char类型会更合适.

有没有办法告诉Doctrine使用char或唯一的方法是在数据库中手动更改它?

Fra*_*ula 32

虽然althaus解决方案工作正常但我认为这应该是规范的方式:

带注释:

/**
 * @Column(type="string", length=2, options={"fixed" = true})
 */
protected $country;
Run Code Online (Sandbox Code Playgroud)

使用YAML(ID和非ID字段的示例):

Your\Nice\Entity:
    id:
        id:
            type: string
            length: 32
            options:
                fixed: true
    fields:
        country:
            type: string
            length: 2
            options:
                fixed: true
Run Code Online (Sandbox Code Playgroud)
  • options:其他选项的数组:
    • fixed:布尔值,用于确定字符串列的指定长度是固定还是变化(仅适用于字符串/二进制列,并且可能不受所有供应商支持).

官方文件


alt*_*aus 6

您可以告诉Doctrine使用特定于供应商的字段类型:

/**
 * @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
 */
protected $country;
Run Code Online (Sandbox Code Playgroud)

资源