交响乐 + Doctrine - 可以将 2 个以上的表/模型链接在一起吗?
我创建了一个与 sfGuardUser 模型相关的 sfGuardUserProfile 模型。然后我定义另一个与 sfGuardUserProfile 相关的模型。创建数据库时,我没有收到任何错误,但是当我尝试将数据保存到操作文件中的 sfGuardUserProfile 时,出现以下错误:
SQLSTATE[23000]: Integrity Constraint Violation: 1452 Cannot add or update a child row: a外键约束失败
在我的 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`))
I've created an sfGuardUserProfile model that has a relation to the sfGuardUser model. Then I define another model with a relation to sfGuardUserProfile. I don't get any errors when I create the database, but when I try saving data to sfGuardUserProfile in my actions file, I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
In my schema.yml, I am defining the relationships as one to one.
I'm not sure why this would be failing. Does Doctrine simply not support adding a new relationship to a model that already has a relationship?
Edit
Here's my 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
I encounter the error when I do this:
$profile = $this->getUser()->getProfile();
$profile->setEmail('[email protected]');
$profile->save();
The generated SQL:
INSERT INTO sf_guard_user_profile (sf_guard_user_id, email) VALUES (?, ?) - (1, [email protected])
The exact error:
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`))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题是你的 FacebookAccount 模型没有链接到你的 Profile 模型上的主键,并且 Doctrine 不知道如何使用它。更改您的 FacebookAccount 以引用配置文件主键:
或与 sfGuardUser 的主键相关:
I think your problem is your FacebookAccount model not linking to the primary key on your Profile model and Doctrine not knowing how to work with it. Either change your FacebookAccount to reference the Profile primary key:
or relate to the primary key of sfGuardUser:
您必须确保不会破坏数据库完整性: http://msdn .microsoft.com/en-us/library/ms175464.aspx。您可以达到以正确的顺序插入行的目的,例如:
或像这样:
You have to ensure that you don't break database integrity: http://msdn.microsoft.com/en-us/library/ms175464.aspx . You can reach that inserting rows in correct order, for example:
or like this: