【问题标题】:Doctrine how to write fixtures for self referencing relations教义如何为自引用关系编写固定装置
【发布时间】:2011-07-20 14:42:14
【问题描述】:

我在 Doctrine 1.2 中为自引用关系定义固定装置时遇到了麻烦,假设我使用the code from the manual,如何为此编写固定装置?

我试过这个夹具文件

User:
  JohnDoe:
    username: "john doe"
  MarkSmith:
    username: "mark smith"
    Friends: [ JohnDoe ]

但是当我运行学说加载任务时,我收到以下错误消息:

Catchable fatal error: Argument 2 passed to Doctrine_Record::setFriends() must be an instance of Doctrine_Access, none given

我会尝试在 FriendReference 夹具中传递用户,但我不知道该怎么做,因为 FriendReference 没有明确声明关系,所以我不能做这样的事情

FriendReference:
  First:
    ???: [ JohnDoe, MarkSmith ]

FriendReference:
  First:
    User1: JohnDoe
    User2: MarkSmith

更新

这是取自manual的示例架构

---
# schema.yml

# ...
User:
# ...
  relations:
    # ...
    Friends:
      class: User
      local: user1
      foreign: user2
      refClass: FriendReference
      equal: true

FriendReference:
  columns:
    user1:
      type: integer
      primary: true
    user2:
      type: integer
      primary: true

【问题讨论】:

  • 您能发布您的架构 YAML 文件吗?
  • @J0HN 当然,我发布了架构。希望您能够帮助我。这并不紧急,但我仍然需要它,我很乐意解决这个问题。
  • 好吧,您希望Users 将如何加载到Question?更新夹具示例。 :)
  • @J0HN 我最初的夹具示例是为 Doctrine 手册示例而设计的。我添加了它而不是我的实际方案,因为概念是相同的。如果我解决了手动示例的问题,我可以将其调整为我的实际模式(不同名称相同)
  • 好吧,也许这很愚蠢,但是您是否尝试过将Friends 声明添加到第一个User

标签: php doctrine entity-relationship fixtures doctrine-1.2


【解决方案1】:

我刚刚找到了这个问题的解决方案,我需要在我的架构文件中引入两个冗余关系。架构中的引用表变为

FriendReference:
  columns:
    user1:
      type: integer
      primary: true
    user2:
      type: integer
      primary: true
  relations: # <-- ADDED RELATIONS
    User1:
      class: User
      local: user1
    User2:
      class: User
      local: user2

有了这个定义,我可以用这种方式指定灯具

FriendReference:
  First:
    User1: JohnDoe
    User2: MarkSmith

原来如此简单……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多