我正在 Symfony 中使用 Doctrine 。我已使用 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)
任何人都可以看到我做错了什么,这阻止了建立外部关系吗?
最佳答案
本节:
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 中,当使用相同名称定义多个键时,它只会覆盖该键之前的值。
关于mysql - Doctrine ORM 不会在从 YaML 规范生成的数据库中创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37361109/