2010-02-11 112 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

相关问题