2010-02-11 29 views
0

我創建了一個與sfGuardUser模型有關係的sfGuardUserProfile模型。然後我定義另一個與sfGuardUserProfile關係的模型。我在創建數據庫時沒有遇到任何錯誤,但是當我嘗試將數據保存到我的操作文件中的sfGuardUserProfile時,出現此錯誤:Symfony + Doctrine - 可以將兩個以上的表格/模型鏈接在一起?

SQLSTATE [23000]:完整性約束違規:1452無法添加或更新子行:外鍵約束失敗

在我的schema.yml中,我將關係定義爲一對一。

我不知道爲什麼這會失敗。 Doctrine是否僅僅不支持向已經存在關係的模型添加新的關係?

編輯 這裏是我的schema.yml:

sfGuardUserProfile: 
    tableName: sf_guard_user_profile 
    columns: 
    sf_guard_user_id: { type: integer(4) } 
    email:   { type: string(255) } 
    relations: 
    User: 
     class:  sfGuardUser 
     type:   one 
     foreignType: one 
     onDelete:  CASCADE 
     local:  sf_guard_user_id 
     foreign:  id 
     foreignAlias: Profile 

FacebookAccount: 
    tableName: facebook_account 
    columns: 
    user_id: { type: integer(4) } 
    page_id: { type: integer } 
    relations: 
    sfGuardUserProfile: 
     type:   one 
     foreignType: one 
     class:  sfGuardUserProfile 
     local:  user_id 
     foreign:  sf_guard_user_id 
     onDelete:  CASCADE 
     foreignAlias: FacebookAccount 

我遇到的錯誤當我這樣做:

$profile = $this->getUser()->getProfile(); 
$profile->setEmail('[email protected]'); 
$profile->save(); 

生成的SQL:

INSERT INTO sf_guard_user_profile (sf_guard_user_id, email) VALUES (?, ?) - (1, [email protected]) 

確切錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`site`.`sf_guard_user_profile`, CONSTRAINT `sf_guard_user_profile_sf_guard_user_id_facebook_account_user_id` FOREIGN KEY (`sf_guard_user_id`) REFERENCES `facebook_account` (`user_id`)) 
+0

你能告訴的定義和查詢你嘗試用生成的執行(也許SQL)? – 2010-02-11 19:00:40

+0

我已編輯以顯示更多細節 – 2010-02-11 19:57:26

回答

2

我覺得你的問題不是鏈接到您的個人資料模型和主鍵你FacebookAccount模型學說不知道如何使用它。要麼改變你的FacebookAccount引用資料的主鍵:

relations: 
    sfGuardUserProfile: 
     type:   one 
     foreignType: one 
     class:  sfGuardUserProfile 
     local:  user_id 
     foreign:  id 
     onDelete:  CASCADE 
     foreignAlias: FacebookAccount 

或涉及到sfGuardUser的主鍵:

relations: 
    sfGuardUser: 
     type:   one 
     foreignType: one 
     class:  sfGuardUser 
     local:  user_id 
     foreign:  id 
     onDelete:  CASCADE 
     foreignAlias: FacebookAccount 
+0

我嘗試了第一種方法,並在創建數據庫時出現此錯誤: SQLSTATE [HY000]:general error:1005無法創建表'site。#sql-cb0_1cb'(errno:15)。失敗查詢:「ALTER TABLE facebook_account ADD CONSTRAINT facebook_account_user_id_sf_guard_user_profile_id FOREIGN KEY(user_id)REFERENCES sf_guard_user_profile(id)ON DELETE CASCADE」。失敗查詢:ALTER TABLE facebook_account ADD CONSTRAINT facebook_account_user_id_sf_guard_user_profile_id FOREIGN KEY(user_id)REFERENCES sf_guard_profile(id)ON DELETE CASCADE。 – 2010-02-12 01:54:20

+0

(我用完了最後一條評論中的字符......)並且我無法真正執行第二種方法b/c $ this-> getUser() - > FacebookAccount失敗。我實際上更喜歡這樣做,但我不想開始黑客sfDoctrineGuardUser類。 – 2010-02-12 01:55:34

+0

對於第一個錯誤,我建議試着放下你的桌子,讓Symfony從頭開始創建它,如果這是可能的話。對於第二種方法,您可以使用$ this-> getUser() - > getFacebookAccount(),以防萬一不是錯字(或$ user = $ this-> getUser(); $ user ['FacebookAccount'] .. )。 sfDoctrineGuardUser的核心功能位於它的基類中,隨意修改lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUser.class.php中的對象以滿足您的需求。 – nortron 2010-02-12 02:34:21

1

您必須確保您不會破壞數據庫完整性:http://msdn.microsoft.com/en-us/library/ms175464.aspx。您可以到達正確的順序,在插入行,例如:

$sfGuardUser = new sfGuardUser(); 
$sfGuardUser->id = 1; 
$sfGuardUser->save(); 

$sfGuardUserProfile = new sfGuardUserProfile(); 
$sfGuardUserProfile->user_id = $sfGuardUser->id; 
$sfGuardUserProfile->save(); 

或像這樣:

$sfGuardUser = new sfGuardUser(); 
$sfGuardUser->id = 1; 

$sfGuardUserProfile = new sfGuardUserProfile(); 
$sfGuardUserProfile->User = $sfGuardUser; 
sfGuardUserProfile->save(); 
+0

感謝您的迴應,弗拉基米爾,但它看起來像是sfGuardUserProfile和FacebookAccount之間的約束問題。在這種情況下,我甚至沒有使用FacebookAccount模型,我只使用sfGuardUserProfile。 – 2010-02-11 20:16:16

+0

當我發佈我的答案時,我沒有閱讀您的更新。不要緊,你不用FacebookAccount工作,因爲你有FK sf_guard_user_profile.sf_guard_user_id - > facebook_account.user_id,所以當你在sf_guard_user_profile(sf_guard_user_id = 1)中插入行時,你必須檢查在facebook_account中有一行相同的user_id = 1。 – 2010-02-11 20:25:03

+0

在使用sfGuardUserProfile之前,我嘗試使用user_id = 1創建一個FacebookAccount模型,但仍然出現錯誤。似乎我無法創建任何模型,無論我嘗試的是哪種順序。 – 2010-02-11 21:02:29

相關問題