Symfony2 (FOSUserBundle Registration form) User 类和另一个类之间的一对一



我发现了一个类似的问题,但报告的解决方案对我不起作用。这是链接: FOSUserBundle 一对一映射实体未保存可能我错过了什么。

现状:我正在使用FOSUserBundle来注册新用户。用户的数据在具有相应实体和一对一关系的两个表之间拆分(一个表包含身份验证凭据,另一个表包含用户的个人资料数据,如姓名/姓氏等(。我已经覆盖了基本的 RegistrationFormType,如 FOSUserBundle 的文档所示。现在数据已成功保存到两个表.....但是外键字段仍然为空(在我的例子中是"utente_id"字段,或 DatiUtente 实体中的$utente变量(

以下是为您提供的要点文件:

  • Utente (实体 - 主用户类( https://gist.github.com/gittix09/8fcac25594cb435b7932
  • DatiUtente (Entity( https://gist.github.com/gittix09/321b5d22f0330f63fb02
  • 注册表格类型 https://gist.github.com/gittix09/0bb3964d92c12a8fb139
  • DatiUtenteFormType https://gist.github.com/gittix09/889533d9f21890a03724

  • 注册控制器 https://gist.github.com/gittix09/12d953621cfc8b503ff8

  • 注册表单处理程序 https://gist.github.com/gittix09/9de1ac967de8ff23995f

在 gist 文件中,我用"XX"更改了项目名称,但当然在我的代码中它是正确的。

另外,在 config.yml 中

# Fos UserBundle Configuration:
fos_user:
 registration:
        form:
            type: ac_user_registration
#Services list
services:
ac_user.registration.form.type:
      class: XXUserBundleFormTypeRegistrationFormType
      arguments: [%fos_user.model.user.class%]
      tags:
          - { name: form.type, alias: ac_user_registration }

在 DatiUtente 实体中更改 setUtente(( 像这样:

  public function setUtente(XXUserBundleEntityUtente $utente = null)
    {
        $utente->setDatiutente($this);
        $this->utente = $utente;
        return $this;
    }

解决方案:最后,我改变了两个实体(Utente和DatiUtente(之间关系的方向。

相关内容

最新更新