FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

Extending the extended User Class

Open Fraktl opened this issue 11 years ago • 9 comments

I know it's possible to extend the BaseUser (in older versions of FOS and just the UserInterface in newer) when you create your own UserBundle in your vendor namespace.

Is it possible to extend that Extended User?

I understand that you have to specify the User Class in the fos_user config like this:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Acme\UserBundle\Entity\AcmeUser

Will this still work if you extend the AcmeUser?

For example, i'd like to keep different information for different roles in my application.

Employee extends AcmeUser Intern extends AcmeUser

(I'm not talking about user roles but actual different attributes per user class)

Fraktl avatar Jun 18 '13 07:06 Fraktl

FOSUserBundle is not designed to work with entity inheritance (and you should probably avoid it as it is a performance killer for Doctrine as relational databases are bad at storing inheritance)

stof avatar Jun 18 '13 08:06 stof

@stof Is there a solution for my use-case? Is it possible then to use different classes that implements the UserInterface and use them? Right now the only way I see it is to create different registration form types (and put all possible attributes in the User class) and handle the role assignment in the controller.

Fraktl avatar Jun 18 '13 08:06 Fraktl

May I suggest as I used .. the system that I'm developing now I have 3 user types.

  1. Provider      1.1 Personal      1.2 Corporate
  2. Consumer

Thus I define the Provider as abstract and inherited the entity Personal and Corporate.

I'm also the type Consumer. Friendlier suggestion mentioned at the beginning of this review I suggest you add a property on the User entity that you can add its identifier, eg when you add vendor user_id = provider_id.

I hope it helps.

hugohenrique avatar Jun 18 '13 16:06 hugohenrique

@stof does that include JOINED Table inheritance?

sstok avatar Jun 20 '13 13:06 sstok

@sstok yes. FOSUserBundle needs to know the name of the entity class. As soon as you have multiple entities, it breaks as you have several classes.

stof avatar Jun 20 '13 17:06 stof

I was actually talking about the performance impact ;) how much slower is it actually?

sstok avatar Jun 21 '13 08:06 sstok

@sstok it forbids using lazy-loading in any place where you have a ToOne relation with the user as target

stof avatar Jun 21 '13 08:06 stof

@sstok Is it still inadvisable to inherit the BaseUser class? Or has it changed since 2013?

Fabuloops avatar Jun 14 '18 10:06 Fabuloops

@Fabuloops I am not using the FosUserBundle anymore, so I don't know :)

sstok avatar Jun 25 '18 06:06 sstok