FOSUserBundle properties in the registration form from another object

Guy van

I decided to use FOSUserBundle to handle user authentication in my application. In my application, companies can register their data (name, address, etc.).

Therefore, I decided to create a company called "Company" to store data associated with the company, and an object called "User" to separately store user data from FOSUserBundle.

I started to redefine the default registration form, as described in the documentation Overriding the FOSUserBundle default forms , but the problem is that I can’t add my fields from the Company to the form, it always tries to access the User object (perhaps because this is due to FOSUserBundle).

Is there a way to put company entity fields in the FOSUser registration form?

I don’t want to spoil the data model, using the company as the base object for FOSUser and adding all the fields to it (username, email address, password, etc.).

Yours faithfully,

+4
source share
1 answer

Semantically speaking, a User does not have to be a person, so I see no reason for the company that is your application user. I also see no reason to have two separate tables in your database.

I would create a separate company class that extends the user class:

 namespace Acme\Bundle\AcmeBundle\Entity; use FOS\UserBundle\Entity\User; class Company extends User { // Your company related fields and related getters/setters go here. } 

Then override the user class from your configuration (e.g. in app/config/config.yml ):

 fos_user: user_class: Acme\Bundle\AcmeBundle\Entity\Company 

UserBundle now knows which class to use as User . After setting up your custom class, you can override RegistrationFormType to include your custom fields.

If you still want to use two separate objects in your application, I suggest you connect them through an Individual relationship , and then insert your Company object in RegistrationFormType , following the documents of embedding one object in the form.

Link: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/overriding_forms.md

+5
source

All Articles