2010-09-23 61 views
5

有什麼方法來加載具有循環引用的夾具?舉個例子,我有以下夾具:學說夾具 - 循環引用

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] 

正如你所看到的,Nicole_Team引用妮可......但是妮可還引用Nicole_Team。

當Manager不是必需的列時,這是OK的(夾具加載,但管理器爲NULL),但現在需要加載夾具。

我能看到的唯一解決方法就是將團隊關係放在自己的對象中(例如'Profile'),這樣關係就不再是循環的了。

有沒有其他方法?每個用戶都必須在一個團隊中,但只有少數用戶是團隊經理。我對我的數據模型可能設計得不好並且有改進空間的事實頗有看法。

回答

1

如何:

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] 

爲了避免循環引用,你必須把關係中的一個模型。

+0

感謝您的回答。我嘗試了這一點,在BusinessEntityTeam和sfGuardUser之間添加了一個雙向關係,以便與參考相關。夾具加載時沒有引發錯誤,但值仍爲NULL – PeterB 2010-09-24 16:17:38

+0

爲什麼添加雙向關係?我所做的就是避免這種關係。如果你想了解一個團隊的經理,你必須在團隊模型中寫一個方法。如函數getManager()並自己實現該方法。 sql應該是「從用戶u選擇top 1,其中u.manager_for =?」 – 2010-09-25 16:10:53

+0

我犯了一個錯誤。你不需要自己編寫一個getManager()方法。而且您也不需要添加雙向關係。這種關係是自動雙向的。例如,您將用戶添加到團隊中,然後您有兩種方法:user-> getTeam()和team-> getUsers()。舉例來說:將會有兩個方法user-> getManagedTeam()和team-> getManager()。我必須讀你的schema.xml來告訴你更多的細節。 – 2010-09-26 01:18:23