【问题标题】:Doctrine ORM not creating foreign keys in database generated from YaML specificationDoctrine ORM 不在从 YaML 规范生成的数据库中创建外键
【发布时间】:2016-05-21 09:25:39
【问题描述】:

我在 Symfony 中使用学说。我已经使用 YaML 指定了我的数据库实体。我能够从我的 YaML 文件生成数据库 - 但是,令我恐惧的是,当我检查生成的表时 - 一些 实体正在丢失外键引用等。

Doctrine 似乎默默地失败了,因为屏幕上没有发出任何错误,doctrine:schema:update --force 任务运行并报告它已成功完成。

这是一个此类实体的示例:

AppBundle\Entity\User:
    type: entity
    table: user
    repositoryClass: UserRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }

    manyToOne:
        user_type:
            targetEntity: UserType
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE

    manyToOne:
        user_title:
            targetEntity: UserTitle
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE

    oneToMany:
        actions:
            targetEntity: UserAction
            mappedBy: User

    oneToMany:
        type_roles:
            targetEntity: UserTypeRole
            mappedBy: User

    uniqueConstraints:
        idxu_user_lname_eml:
            columns: [last_name, user_email]

    indexes:
        idx_user_name:
            columns: [last_name, first_name]
        idx_user_email:
            columns: user_email            


    fields:
        first_name:
            type: string
            length: 32
            nullable: false
            unique: false

        middle_name:
            type: string
            length: 32
            nullable: true
            unique: false 

        last_name:
            type: string
            length: 64
            nullable: false
            unique: false     

        email:
            type: string
            length: 32
            column: user_email
            unique: true
            options:
                fixed: true
                comment: User's email address

        address_line1:
            type: string
            length: 128
            nullable: false

        address_line2:
            type: string
            length: 128
            nullable: true

        address_line3:
            type: string
            length: 128
            nullable: true

        town_city:
            type: string
            length: 128
            nullable: true

        county_district:
            type: string
            length: 128
            nullable: true

        state_region:
            type: string
            length: 128
            nullable: true

        post_zipcode:
            type: string
            length: 12
            nullable: true

        country:
            type: string
            length: 128
            nullable: true

        login_count:
            type: integer
            nullable: false
            options:
                unsigned: true
                default: 0

        last_login:
            type: datetime
            nullable: true

这是根据上述规范生成的表格:

mysql> describe user;
+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| id              | int(11)          | NO   | PRI | NULL    | auto_increment |
| user_type_id    | int(11)          | NO   | MUL | NULL    |                |
| first_name      | varchar(32)      | NO   |     | NULL    |                |
| middle_name     | varchar(32)      | YES  |     | NULL    |                |
| last_name       | varchar(64)      | NO   | MUL | NULL    |                |
| user_email      | char(32)         | NO   | UNI | NULL    |                |
| address_line1   | varchar(128)     | NO   |     | NULL    |                |
| address_line2   | varchar(128)     | YES  |     | NULL    |                |
| address_line3   | varchar(128)     | YES  |     | NULL    |                |
| town_city       | varchar(128)     | YES  |     | NULL    |                |
| county_district | varchar(128)     | YES  |     | NULL    |                |
| state_region    | varchar(128)     | YES  |     | NULL    |                |
| post_zipcode    | varchar(12)      | YES  |     | NULL    |                |
| country         | varchar(128)     | YES  |     | NULL    |                |
| login_count     | int(10) unsigned | NO   |     | 0       |                |
| last_login      | datetime         | YES  |     | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
16 rows in set (0.07 sec)

任何人都可以看到我做错了什么,那就是阻止建立外国关系?

【问题讨论】:

    标签: mysql symfony doctrine yaml


    【解决方案1】:

    本节:

    manyToOne:
        user_type:
            targetEntity: UserType
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE
    
    manyToOne:
        user_title:
            targetEntity: UserTitle
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE
    
    oneToMany:
        actions:
            targetEntity: UserAction
            mappedBy: User
    
    oneToMany:
        type_roles:
            targetEntity: UserTypeRole
            mappedBy: User
    

    应该是这样的:

    manyToOne:
        user_type:
            targetEntity: UserType
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE
        user_title:
            targetEntity: UserTitle
            joinColumn:
                name: user_type_id
                referencedColumnName: id
                nullable: false
                onDelete: RESTRICT
                onUpdate: CASCADE
    
    oneToMany:
        actions:
            targetEntity: UserAction
            mappedBy: User
        type_roles:
            targetEntity: UserTypeRole
            mappedBy: User
    

    在 YaML 中,当使用相同名称定义多个键时,它只会覆盖该键的先前值。

    【讨论】:

    • 您的答案未包含建议的更改(即为不同的键使用不同的名称)。无论如何,即使建议进行修改,FK 也无法正确生成
    • 检查您的 PHP 实体中的关系是否正确实现。 Doctrine 仅在拥有方检查关联(使用inversedBy 定义关联的实体)。如果您从关系的反面创建关联,则必须确保该关联也在关联的拥有方中创建。例如,在User 类中:public function addAction(UserAction $userAction) { $this->actions[] = $userAction; $userAction->setUser($this); }
    【解决方案2】:

    事后看来(可能是睡个好觉),问题的解决方案很明显:我错误地指定了实体,在我的 YaML 中,正确的 YaML 表示应该是这样的:

    AppBundle\Entity\User:
        type: entity
        table: user
        repositoryClass: UserRepository
        id:
            id:
                type: integer
                generator: { strategy: AUTO }
    
        manyToOne:
            user_type:
                targetEntity: UserType
                joinColumn:
                    name: user_type_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE
    
            user_title:
                targetEntity: UserTitle
                joinColumn:
                    name: user_title_id
                    referencedColumnName: id
                    nullable: false
                    onDelete: RESTRICT
                    onUpdate: CASCADE
    
        oneToMany:
            actions:
                targetEntity: UserAction
                mappedBy: User
    
            roles:
                targetEntity: UserTypeRole
                mappedBy: User
    
        uniqueConstraints:
            idxu_user_lname_eml:
                columns: [user_email]
    
        indexes:
            idx_user_name:
                columns: [last_name, first_name]           
    
    
        fields:
            first_name:
                type: string
                length: 32
                nullable: false
                unique: false
    
            middle_name:
                type: string
                length: 32
                nullable: true
                unique: false 
    
            last_name:
                type: string
                length: 64
                nullable: false
                unique: false     
    
            email:
                type: string
                length: 32
                column: user_email
                unique: true
                options:
                    fixed: true
                    comment: User's email address
    
            address_line1:
                type: string
                length: 128
                nullable: false
    
            address_line2:
                type: string
                length: 128
                nullable: true
    
            address_line3:
                type: string
                length: 128
                nullable: true
    
            town_city:
                type: string
                length: 128
                nullable: true
    
            county_district:
                type: string
                length: 128
                nullable: true
    
            state_region:
                type: string
                length: 128
                nullable: true
    
            post_zipcode:
                type: string
                length: 12
                nullable: true
    
            country:
                type: string
                length: 128
                nullable: true
    
            login_count:
                type: integer
                nullable: false
                options:
                    unsigned: true
                    default: 0
    
            last_login:
                type: datetime
                nullable: true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2020-12-19
      相关资源
      最近更新 更多