2010-05-11 40 views
3

我正在與Doctrine合作開展我的第一個Symfony項目,並且我碰到了一個麻煩。我試圖與兩名球員表達比賽。我想要的是PlayerOne和PlayerTwo,每個都被鍵入用戶表中的ID。這是我到目前爲止的一部分:與YAML的學說中的一對二關係

Game: 
    actAs: { Timestampable:- } 
    columns: 
    id: { type: integer, notnull: true, unique: true } 
    startDate: { type: timestamp, notnull: true } 
    playerOne: { type: integer, notnull: true } 
    playerTwo: { type: integer, notnull: true } 
    winner: { type: integer, notnull:true, default:0 } 
    relations: 
    User: { onUpdate: cascade, local: playerOne, foreign: id} 
    User: { onUpdate: cascade, local: playerTwo, foreign: id} 

這是行不通的。它構建得很好,但它生成的SQL僅包含對playerTwo的約束。我試過其他的幾件事情:

User: { onUpdate: cascade, local: [playerOne, playerTwo], foreign: id} 

另外:

User: [{ onUpdate: cascade, local: playerOne, foreign: id}, { onUpdate: cascade, local: playerTwo, foreign: id}] 

最後兩個擲錯誤,當我嘗試建立。有沒有人瞭解我想要做什麼並可以幫助我實現目標?

回答

6

嘗試不同的名字關係:

relations: 
    UserOne: { onUpdate: cascade, local: playerOne, foreign: id, class: User } 
    UserTwo: { onUpdate: cascade, local: playerTwo, foreign: id, class: User } 

編輯:也注意補充「類:用戶」參數,其中明確講述類型的關係。

+0

+1。你不能有兩個具有相同名稱的關係,因爲你不能有兩個具有相同名稱的不同屬性。 – 2010-05-15 21:12:44

1

如果您說$ game-> getUser(),則不清楚您指的是哪個用戶。因爲它們引用同一個表,所以需要給這兩個關係不同的名稱併爲它們聲明兩個不同的外部別名。基本上,您的遊戲桌需要能夠用不同的名稱(例如UserOne,UserTwo)引用每個關係,反之亦然。否則會混淆教義,關係不會正確設置。

我早些時候有類似的問題...使用MySQL Workbench在構建模型後生成模式的可視表示有助於確保關係完全按照需要。

希望有所幫助。