问题
I've found a similar question but the reported solution didn't work for me. Here is the link: FOSUserBundle One-To-One mapped Entity not saved Probably I'm missing something.
Current situation: I'm using the FOSUserBundle to register new users. The users' data are split between two tables with corresponding Entities and ONE TO ONE relation (one table contains the authentication credentials and the other one the users' profile data like name/surname etc). I've overwritten the basic RegistrationFormType as shown in the FOSUserBundle's documentation. Now the data are persisted successfully TO BOTH TABLES..... BUT the foreign key field remains empty (in my case "utente_id" field, or $utente variable in DatiUtente entity)
Here are the gist files for you:
- Utente (Entity - main User class) https://gist.github.com/gittix09/8fcac25594cb435b7932
- DatiUtente (Entity) https://gist.github.com/gittix09/321b5d22f0330f63fb02
- RegistrationFormType https://gist.github.com/gittix09/0bb3964d92c12a8fb139
DatiUtenteFormType https://gist.github.com/gittix09/889533d9f21890a03724
RegistrationController https://gist.github.com/gittix09/12d953621cfc8b503ff8
- RegistrationFormHandler https://gist.github.com/gittix09/9de1ac967de8ff23995f
In the gist files I changed the project name with "XX" but of course in my code it's correct.
Also, in config.yml
# Fos UserBundle Configuration:
fos_user:
registration:
form:
type: ac_user_registration
#Services list
services:
ac_user.registration.form.type:
class: XX\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: ac_user_registration }
回答1:
In DatiUtente Entity change setUtente() like this :
public function setUtente(\XX\UserBundle\Entity\Utente $utente = null)
{
$utente->setDatiutente($this);
$this->utente = $utente;
return $this;
}
回答2:
Solution: In the end I changed the direction of the relationship between the two entities (Utente and DatiUtente).
来源:https://stackoverflow.com/questions/27441573/symfony2-fosuserbundle-registration-form-one-to-one-relation-between-user-clas