2012-03-07 53 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