2012-06-18 33 views
2

我的崗位模型的schema.yml(Symfony的1.4)學說/ MySQL的關係是這樣的:與多列PK臺

Post: 
    columns: 
    id: { type: bigint, notnull: true, primary: true } 
    blog_id: { type: bigint, notnull: true, primary: true } 
    user_id: { type: bigint, notnull: true } 
    subject: { type: string(255), notnull: true } 
    short_body: { type: text, notnull: true } 
    long_body: { type: text } 

,你可以看到張貼有一個多列PK。我的問題是「我如何與這個模型創建n:1關係?」

舉個例子,我想是這樣的:

PostComment: 
    columns: 
    post_id: { type: bigint, notnull: true } 
    blog_id: { type: bigint, notnull: true } 
    name: { type: string(255), notnull: true } 
    email: { type: string(255) } 
    text: { type: text, notnull: true } 
    relations: 
    Post: 
     #Here is my problem. What should I write here? 
     local: ???? 
     foreign: ???? 
     foreignAlias: Comments 
     onDelete: cascade 
     onUpdate: cascade 

我如何處理這種關係?

+0

我不熟悉這種表示法,但您需要一對多的表示法。另外postcomment應該有它的ID我猜! – GorillaApe

回答

1

你不能。 您可以將另一個主鍵添加到Post模型,並將另外幾個字段保持爲唯一。如果您這樣做,我強烈建議您將「id」作爲主鍵,並使用另一個字段名稱作爲唯一字符以及「blog_id」。 然後,像通常一樣使用PostComment中的關係。

+0

感謝您的回覆。但是另一個問題是,「我如何在yml語法中創建多列唯一約束?」 –

+1

參見http://www.symfony-project.org/gentle-introduction/1_4/en/08-Inside-the-Model-Layer-Doctrine#chapter_08_sub_indexes –