我正在使用Symfony/YaML/Doctrine将数据存储在MySQL后端。我想使用我声明的一些FK创建一个索引(以加快查询速度)。然而,当我尝试生成模式时,我会得到以下错误:
[DDoctrine\DBAL\Schema\SchemaException]没有名称为的列表"pms"上的"to_user_id"
当我注释掉索引声明时,我能够毫无问题地生成表。这就是我的YaML文件的样子:
AppBundleEntityPrivateMessage:
type: entity
table: pms
repositoryClass: PMSRepository
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
from_user:
targetEntity: User
joinColumn:
name: from_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
to_user:
targetEntity: User
joinColumn:
name: to_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
message_type:
targetEntity: PrivateMessageType
joinColumn:
name: pms_type_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
manyToOne:
message_status:
targetEntity: PrivateMessageStatus
joinColumn:
name: pms_status_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
indexes:
idx_pms:
columns: [from_user_id, to_user_id, created_at]
fields:
subject:
type: text
nullable: false
body:
type: text
nullable: false
attach_1:
type: string
length: 128
nullable: true
attach_2:
type: string
length: 128
nullable: true
attach_3:
type: string
length: 128
nullable: true
created_at:
type: datetime
问题很简单:不要在几个manyToOne
中拆分所有的关联。正确的变体应该像一样
AppBundleEntityPrivateMessage:
type: entity
table: pms
repositoryClass: PMSRepository
id:
id:
type: integer
generator: { strategy: AUTO }
manyToOne:
from_user:
targetEntity: User
joinColumn:
name: from_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
to_user:
targetEntity: User
joinColumn:
name: to_user_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
message_type:
targetEntity: PrivateMessageType
joinColumn:
name: pms_type_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
message_status:
targetEntity: PrivateMessageStatus
joinColumn:
name: pms_status_id
referencedColumnName: id
nullable: false
onDelete: RESTRICT
onUpdate: CASCADE
indexes:
idx_pms:
columns: [from_user_id, to_user_id, created_at]
fields:
subject:
type: text
nullable: false
body:
type: text
nullable: false
attach_1:
type: string
length: 128
nullable: true
attach_2:
type: string
length: 128
nullable: true
attach_3:
type: string
length: 128
nullable: true
created_at:
type: datetime