【问题标题】:One to two relationship in Doctrine with YAMLDoctrine 与 YAML 的一对二关系
【发布时间】:2011-02-17 22:29:41
【问题描述】:

我正在使用 Doctrine 进行我的第一个 Symfony 项目,但遇到了麻烦。我试图表达一个有两个玩家的游戏。我想要的关系是 PlayerOne 和 PlayerTwo 分别被键入用户表中的一个 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}]

当我尝试构建时,最后两个抛出错误。有没有人了解我正在尝试做的事情并可以帮助我实现它?

【问题讨论】:

    标签: mysql symfony1 doctrine yaml


    【解决方案1】:

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

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

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      尝试不同的关系名称:

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

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

      【讨论】:

      • +1。不能有 2 个同名的关系,因为不能有 2 个不同的属性同名。
      猜你喜欢
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多