我正在尝试在Doctrine中的answers和questions表之间创建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放入answers表中的'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;如何避免这种独一无二的关键问题?从逻辑上讲(一对多),在answers表中问题id不应该有唯一的键。
发布于 2012-03-08 20:06:25
实际上,它和下面的代码一样简单
问题:
oneToMany:
answer:
targetEntity: FsAnswer
mappedBy: question
cascade: ["persist"]答案:
manyToOne:
question:
targetEntity: FsQuestion
inversedBy: answer发布于 2012-03-08 00:08:28
每个答案都有且只有一个问题,对吗?您将其定义为oneToOne这一事实在某种程度上证实了这一点。
看起来你的joinColumn资料搞得一团糟。令人惊讶的是它没有引发错误。
复习理论手册中的示例:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html
https://stackoverflow.com/questions/9604730
复制相似问题