我正在尝试在Doctrine中的答案和问题表之间创建OneToMany关系。这些是基本的YAML模式
答案架构
type: entity
table: fs_answer
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
questionId:
type: integer
unsigned: false
nullable: false
column: question_id
body:
type: text
nullable: false
oneToOne:
question:
targetEntity: FsQuestion
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
question_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
问题架构:
type: entity
table: fs_question
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
body:
type: text
nullable: false
oneToMany:
answer:
targetEntity: FsAnswer
cascade: { }
mappedBy: question
inversedBy: answers
joinColumns:
question_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
当我使用doctrine:schema:update
更新架构时,它会生成下面的SQL代码并将unique key
放入答案表中的'question_id'。
CREATE TABLE IF NOT EXISTS `fs_answer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question_id` int(11) NOT NULL,
`body` longtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_552D082B1E27F6BF` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
如何避免这种独特的关键内容?逻辑上(一对多)答案表中的问题ID不应该是唯一键。
答案 0 :(得分:1)
实际上它就像下面的代码一样简单
问题:
oneToMany:
answer:
targetEntity: FsAnswer
mappedBy: question
cascade: ["persist"]
答案:
manyToOne:
question:
targetEntity: FsQuestion
inversedBy: answer
答案 1 :(得分:0)
每个答案只有一个问题吗?您将其定义为oneToOne类型的事实证实了这一点。
看起来你的joinColumn东西搞砸了。感到惊讶的是它没有引起错误。
查看学说手册中的示例:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html