Doctrine 与 YAML 中的一对二关系

发布于 2024-08-31 16:10:49 字数 961 浏览 1 评论 0原文

我正在使用 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}]

当我尝试构建时,最后两个会抛出错误。有没有人了解我想要做什么并可以帮助我实现它?

I'm working on my first Symfony project with Doctrine, and I've run into a hitch. I'm trying to express a game with two players. The relationship I want to have is PlayerOne and PlayerTwo each being keyed to an ID in the Users table. This is part of what I've got so far:

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}

That doesn't work. It builds fine, but the SQL it generates only includes a constraint for playerTwo. I've tried a few other things:

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

Also:

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

Those last two throw errors when I try to build. Is there anyone out there who understands what I'm trying to do and can help me achieve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-09-07 16:10:49

尝试不同的关系名称:

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

编辑:还请注意添加了“class:User”参数,该参数明确说明关系的类型。

Try different names for relations:

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

Edited: also note added "class: User" parameter, which explicitly tells about type of the relation.

望她远 2024-09-07 16:10:49

如果您说 $game->getUser(),则不清楚您指的是哪个用户。因为它们引用同一个表,所以您需要为这两个关系指定不同的名称,并为它们声明两个不同的foreignAliases。基本上,您的游戏表需要能够使用不同的名称(例如 UserOne、UserTwo)引用每个关系,反之亦然。否则,它会混淆教义,并且无法正确建立关系。

我之前遇到过类似的问题...在构建模型后使用 MySQL Workbench 生成架构的可视化表示有助于确保关系完全符合需要。

希望有帮助。

If you say $game->getUser(), it's not clear which user you're referring to. Because they reference the same table, you need to give the two relations different names and declare two different foreignAliases for them. Basically, your game table needs to be able to refer to each relation with a different name (e.g. UserOne, UserTwo) and vice versa. It'll confuse Doctrine otherwise and relations won't be set up correctly.

I had a similar problem earlier... using MySQL Workbench to generate a visual representation of the schema after building the models helped make sure the relations were all exactly as needed.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文