views:

40

answers:

1

Is there any way to load fixtures that have circular referencing? As an example I have the following fixture:

BusinessEntityTeam:
  Nicole_Team:
    name: Nicole's Team
    Manager: [Nicole]
    Business: [ACMEWidgets]

sfGuardUser
  Nicole:
    first_name:     Nicole
    last_name:      Jones
    email_address:  [email protected]
    username:       nicole
    password:       nicole
    Groups:         [Group_abc]
    Team:           [Nicole_Team]

As you can see, Nicole_Team references Nicole... but Nicole also references Nicole_Team.

When Manager wasn't a required column this was OK (the fixture loaded, but Manager was NULL), but now it's required it's impossible to load the fixture.

The only work-around I can see is to put the Team relation in its own object ('Profile' for example) so the relations are no longer circular.

Is there any other approach? Every user has to be in a team, but only a few users are team managers. I'm quite open to the fact that my data model may be badly designed and have room for improvement.

+1  A: 

How about this:

BusinessEntityTeam:
  Nicole_Team:
    name: Nicole's Team
    Business: [ACMEWidgets]

sfGuardUser
  Nicole:
    first_name:     Nicole
    last_name:      Jones
    email_address:  [email protected]
    username:       nicole
    password:       nicole
    Groups:         [Group_abc]
    Team:           [Nicole_Team]
    ManagerFor:     [Nicole_Team]

In order to avoid circular referencing, you have to put the relations in one model.

Peter Long
Thanks for your answer. I tried this, adding a bidirectional relationship between BusinessEntityTeam and sfGuardUser in order to have a relation to reference. The fixture loads with no error being thrown, but the value is still NULL
PeterB
Why did you add a bidirectional relationship? What I did is to avoid such a relationship. If you want to know a team's manager, you'd have to write a method in your team model. such as function getManager() and implement the method yourself. The sql should be something like "select top 1 from user u where u.manager_for = ?"
Peter Long
I made a mistake. You do not need to write a getManager() method yourself. And you don't need to add a bidirectional relationship either. The relationship are automatically bidirectional. For example, you add a relationship from User to Team, then you have two methods: user->getTeam() and team->getUsers() automatically. For your example: there will be two methods user->getManagedTeam() and team->getManager(). I have to read your schema.xml to tell you more detail.
Peter Long
What you write sounds correct, but still isn't working in my case. I think this is because I have a lot of loops/references between all tables, and there's other dependencies not resolved. I'm therefore marking this as the correct answer.
PeterB