代码之家  ›  专栏  ›  技术社区  ›  Steven Mercatante Dimitri Kopriwa

symfony+条令-可以将两个以上的表/模型连接在一起吗?

  •  0
  • Steven Mercatante Dimitri Kopriwa  · 技术社区  · 15 年前

    我创建了一个与sfguarduser模型有关系的sfguarduserprofile模型。然后我定义了另一个与sfguardUserProfile相关的模型。创建数据库时我不会收到任何错误,但是当我尝试将数据保存到操作文件中的sfguarduserprofile时,我会收到以下错误:

    sqlstate[23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败

    在schema.yml中,我将关系定义为一对一。

    我不知道为什么会失败。理论是否只是不支持在已有关系的模型中添加新的关系?

    编辑 这是我的模式。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`))
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   nortron    15 年前

    我认为你的问题是你的facebook账户模型没有链接到你的配置文件模型上的主键,以及不知道如何使用它的原则。更改facebook帐户以引用配置文件主键:

      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
    
        2
  •  1
  •   Vladimir Mihailenco    15 年前

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