交响乐 + Doctrine - 可以将 2 个以上的表/模型链接在一起吗?

发布于 2024-08-21 02:32:37 字数 2063 浏览 5 评论 0原文

我创建了一个与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘落散花 2024-08-28 02:32:37

我认为你的问题是你的 FacebookAccount 模型没有链接到你的 Profile 模型上的主键,并且 Doctrine 不知道如何使用它。更改您的 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

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:

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

or relate to the primary key of sfGuardUser:

  relations:
    sfGuardUser:
      type:         one
      foreignType:  one
      class:        sfGuardUser
      local:        user_id
      foreign:      id
      onDelete:     CASCADE
      foreignAlias: FacebookAccount
雪花飘飘的天空 2024-08-28 02:32:37

您必须确保不会破坏数据库完整性: 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();

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:

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

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

or like this:

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

$sfGuardUserProfile = new sfGuardUserProfile();
$sfGuardUserProfile->User = $sfGuardUser;
sfGuardUserProfile->save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文