我正在与Doctrine合作开发我的第一个Symfony项目,但我遇到了一个问题。我试着表达一个有两个玩家的博弈。我想要的关系是PlayerOne和PlayerTwo,每个都与Users表中的一个ID相关联。这是我到目前为止所学到的一部分:
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}]当我尝试构建时,最后两个抛出错误。有没有人能理解我的意图,并能帮助我实现它?
发布于 2010-05-11 18:02:03
为关系尝试不同的名称:
relations:
UserOne: { onUpdate: cascade, local: playerOne, foreign: id, class: User }
UserTwo: { onUpdate: cascade, local: playerTwo, foreign: id, class: User }编辑:还请注意添加了"class: User“参数,该参数显式地告知关系的类型。
发布于 2010-05-11 09:32:10
如果你说$game->getUser(),就不清楚你指的是哪个用户。因为它们引用同一个表,所以您需要为这两个关系指定不同的名称,并为它们声明两个不同的foreignAliases。基本上,您的游戏表需要能够使用不同的名称(例如,UserOne,UserTwo)引用每个关系,反之亦然。否则,它会混淆Doctrine,关系也不会正确设置。
我之前也遇到过类似的问题...在构建模型之后,使用MySQL工作台生成模式的可视化表示,有助于确保关系完全符合需要。
希望这能有所帮助。
https://stackoverflow.com/questions/2807328
复制相似问题