【问题标题】:Symfony + Doctrine - possible to link more than 2 tables/models together?Symfony + Doctrine - 可以将超过 2 个表/模型链接在一起吗?
【发布时间】:2011-01-15 20:00:09
【问题描述】:

我创建了一个与 sfGuardUser 模型相关的 sfGuardUserProfile 模型。然后我定义了另一个与 sfGuardUserProfile 相关的模型。创建数据库时我没有收到任何错误,但是当我尝试将数据保存到操作文件中的 sfGuardUserProfile 时,我收到此错误:

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('someone@somewhere.com');
$profile->save();

生成的SQL:

INSERT INTO sf_guard_user_profile (sf_guard_user_id, email) VALUES (?, ?) - (1, someone@somewhere.com)

确切的错误:

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`))

【问题讨论】:

  • 你能显示定义以及你尝试执行的查询(可能使用生成的 SQL)吗?
  • 我已编辑以显示更多细节

标签: php symfony1 doctrine sfguard


【解决方案1】:

我认为您的问题是您的 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

【讨论】:

  • 我尝试了第一种方法,创建数据库时出现这个错误:SQLSTATE[HY000]: general error: 1005 Can't create table '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。
  • (我在最后一条评论中用完了字符...),我不能真正执行第二种方法 b/c $this->getUser()->FacebookAccount 失败。我实际上更喜欢这样做,但我不想开始破解 sfDoctrineGuardUser 类。
  • 对于第一个错误,如果可能的话,我建议尝试删除您的表并让 Symfony 从头开始​​创建它。对于第二种方法,您将使用 $this->getUser()->getFacebookAccount() 以防万一这不是拼写错误(或 $user = $this->getUser(); $user['FacebookAccount'].. .)。 sfDoctrineGuardUser 的核心功能在它的基类中,您可以随意修改 lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUser.class.php 中的对象以满足您的需求。
  • 感谢您的回复,Cryo。我尝试了 $this->getUser()->getFacebookAccount() 但收到此错误:调用未定义的方法 myUser::getFacebookAccount。除非您的意思是我必须在此之前编辑基类? (我希望我知道为什么这个模型的行为与您可以关联的其他模型不同。)
  • 抱歉,您需要执行 $this->getUser()->getGuardUser()->getFacebookAccount()。
【解决方案2】:

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

【讨论】:

  • 感谢 Vladimir 的回复,但看起来约束问题在 sfGuardUserProfile 和 FacebookAccount 之间。在这种情况下,我什至不使用 FacebookAccount 模型,我只使用 sfGuardUserProfile。
  • 发布我的答案时,我没有阅读您的更新。你不使用 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。
  • 在使用 sfGuardUserProfile 之前,我尝试使用 user_id = 1 创建 FacebookAccount 模型,但我仍然收到错误消息。无论我尝试哪种顺序,我似乎都无法创建任何一个模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多