【问题标题】:One-to-one relations with sfGuardPlugin与 sfGuardPlugin 的一对一关系
【发布时间】:2013-06-26 19:24:27
【问题描述】:

我在我的 schema.yml 中添加了以下类(我使用的是 symfony 1.4):

LoginKey:
  connection: doctrine
  tableName: sos_login_key
  options:
    type: InnoDB
    collate: utf8_unicode_ci
    charset: utf8
  columns:
    id:
      type: integer(8)
      fixed: false
      unsigned: false
      primary: true
      autoincrement: true
      notnull: true
    user_id:
      notnull: true
      type: integer(8)
    keycode:
      notnull: true
      type: string(255)
    expires_at:
      type: datetime
      default: null
  relations:
    sfGuardUser:
      local: user_id
      foreign: id
      type: one

sfGuardUser:
  relations:
    LoginKey:
      local: id
      foreign: user_id
      type: one

现在,当我尝试在查询中对这些表进行 JOIN (sfGuardUser innerJoin LoginKey) 时,LoginKey 记录不会得到水合;当我尝试访问 LoginKey 时,我看到每行都完成了一个新查询。 我该怎么办?

【问题讨论】:

    标签: symfony-1.4


    【解决方案1】:

    关系的拥有方是LoginKey,因此您必须仅在此处定义关系。添加foreignType: one 并删除sfGuardUser 部分。所以一个正确的模式看起来像这样:

    LoginKey:
      columns:
        user_id:
          type: integer
          notnull: true
        keycode:
          type: string(255)
          notnull: true
        expires_at:
          type: datetime
          default: null
      relations:
        sfGuardUser:
          local: user_id
          foreign: id
          type: one
          foreignType: one
    

    附带说明,如果您想扩展插件的架构,您必须使用 package 参数(但在您的情况下,它不是必需的,因为不必向 sfGuardUser 添加新字段)

    sfGuardUser:
      package:
        sfDoctrineGuardPlugin.lib.model.doctrine
      relations:...
    

    如果不使用它,这些类将生成到错误的位置并与其他类发生冲突,因此您必须删除这些类。试试php symfony doctrine:clean 并检查它是否从lib/modellib/model/base 中删除了所有未使用的类(sfGuardUser)。

    【讨论】:

    • 太好了,谢谢,这肯定看起来更好。尽管发生了变化,但我仍然遇到保湿问题。还有其他想法吗?
    • 您是否删除了重复的 sfGuardUser 类(也来自 lib/model/base)?重新生成模型并清除缓存?你试过什么? $user->getLoginKey();?
    • 我刚刚意识到我留下了一个重载的 getLoginKey() 版本,这是罪魁祸首……现在感觉很笨;)谢谢你的帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多