2012-03-07 40 views
0

我试图在原则中的答案和问题表之间创建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代码,并把​​为 '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的唯一键。

回答

1

其实这是因为下面的代码一样简单

问:

oneToMany: 
    answer: 
    targetEntity: FsAnswer 
    mappedBy: question 
    cascade: ["persist"] 

答:

manyToOne: 
    question: 
    targetEntity: FsQuestion 
    inversedBy: answer 
0

每个答案都有唯一的一个问题吧?事实上,你将其定义为onetoOne确认这一点。

它看起来像你的joinColumn的东西搞砸了。惊讶它没有提出错误。

回顾教义手册中的例子:

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association-mapping.html

+0

对,每个答案只有一个问题。其实外交工作,但是,我不想要的唯一的钥匙 – seferov 2012-03-07 16:17:33

+0

同样,你在讲述主义从问题的答案到建立OnetoOne关系,同时从问题创建OneToMany来回答。只是不去工作。将OneToOne更改为ManyToOne并确保正确定义了joinColumn。 – Cerad 2012-03-07 16:46:51