【问题标题】:How to specify field name如何指定字段名称
【发布时间】:2014-05-20 21:48:52
【问题描述】:

我有下一个 yaml 格式的实体:

BW\UserBundle\Entity\User:
    type: entity
    table: users
    repositoryClass: BW\UserBundle\Entity\UserRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        username:
            type: string
            length: 25
            unique: true
        password:
            type: string
            length: 64
        email:
            type: string
            length: 60
            unique: true
        isActive:
            type: boolean
    lifecycleCallbacks: {  }

现在,当我更新方案时,我在 DB 中得到 isActive 字段名称,与属性名称相同。 如何在 DB 中指定属性名称 isActiveis_active 字段名称?

【问题讨论】:

    标签: symfony orm doctrine-orm entity yaml


    【解决方案1】:

    你需要像这样指定一个列名:

    BW\UserBundle\Entity\User:
        type: entity
        table: users
        repositoryClass: BW\UserBundle\Entity\UserRepository
        id:
            id:
                type: integer
                id: true
                generator:
                    strategy: AUTO
        fields:
            username:
                type: string
                length: 25
                unique: true
            password:
                type: string
                length: 64
            email:
                type: string
                length: 60
                unique: true
            isActive:
                type: boolean
                column: is_active
        lifecycleCallbacks: {  }
    

    更多信息 - http://docs.doctrine-project.org/en/2.0.x/reference/basic-mapping.html#property-mapping

    【讨论】:

    • 我用name 代替column,用column 可以,谢谢!
    【解决方案2】:

    您可以指定数据库的列名

    fields:
        username:
            type: string
            length: 25
            unique: true
        password:
            type: string
            length: 64
        email:
            type: string
            length: 60
            unique: true
        isActive:
            type: boolean
            column: is_active
    

    【讨论】:

      【解决方案3】:

      您在这里处理两种不同的约定。在 Symfony 中,使用驼峰式命名变量是很常见的。在数据库中,您经常会发现这个下划线的书面名称。如果您访问您的 isActive 属性,您的实体模型会将其映射到数据库中的 is_active 列。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-24
        • 2020-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-27
        • 1970-01-01
        相关资源
        最近更新 更多