Fosuserbundle: Multiple User classes for different entity managers

Created on 2 Oct 2013  路  12Comments  路  Source: FriendsOfSymfony/FOSUserBundle

Hey!

I use multiple entity managers, as this is a common use case for any symfony application, but I want to have two different User entities, with different relationships and fields, for each entity manager.

Currently I have both entities already extending the base User class, but only the one that uses the default entity manager gets the database correctly setup on the schema:update command.

The other table remains only with the id field.

Any solutions for that? Maybe I need to change the manager at runtime, somehow...

Most helpful comment

@drgomesp FOSUserBundle itself only supports User class anyway (it cannot use multiple User classes as it would require duplicating the whole controllers to support each of them).

If you really want to map the superclasses with several managers (which is overcomplicating your architecture IMO but anyway), you can register the mapping by hand for Doctrine for other managers than the one known by FOSUserBundle, as described in the Symfony doc: http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration

doctrine:
    orm:
        entity_managers:
            # ...
            non_default:
                mapping:
                    FOSUserBundle:
                        type: xml
                        dir: '%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/model'
                        prefix: FOS\UserBundle\Model
                        is_bundle: false # needed as we don't follow the standard convention for bundles

but keep in mind that only one of your User classes can benefit of the features of FOSUserBundle.

All 12 comments

common use case for symfony apps ? really ? I never found any app needing this. Using 2 entity managers is meaningful if you have 2 different DBs, and I don't see why you would store users in both DBs.

@stof just because you never seen any doesn't mean that it's not a common scenario. Otherwise symfony wouldn't have support for multiple entity managers...

I have two different User entities, with different relationships defined in each one of them. The thing is both of them need to extend the base User entity from FOSUserBundle, as they will be used in login and everything else.

Is there a proper solution for that?

@drgomesp Supporting it does not mean it is a common scenario.

and having 2 classes extending the same mapped superclass does not mean you need 2 different entity managers to manage them

@stof ok let's stop the flame?

I do need two different entity managers because each entity is going to be persisted into two different databases.

Can you provide a nice solution within this library?

@drgomesp FOSUserBundle itself only supports User class anyway (it cannot use multiple User classes as it would require duplicating the whole controllers to support each of them).

If you really want to map the superclasses with several managers (which is overcomplicating your architecture IMO but anyway), you can register the mapping by hand for Doctrine for other managers than the one known by FOSUserBundle, as described in the Symfony doc: http://symfony.com/doc/current/reference/configuration/doctrine.html#mapping-configuration

doctrine:
    orm:
        entity_managers:
            # ...
            non_default:
                mapping:
                    FOSUserBundle:
                        type: xml
                        dir: '%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/model'
                        prefix: FOS\UserBundle\Model
                        is_bundle: false # needed as we don't follow the standard convention for bundles

but keep in mind that only one of your User classes can benefit of the features of FOSUserBundle.

If your looking into supporting multiple users (with each with there own EntityManager) have a look at the
https://github.com/rollerworks/RollerworksMultiUserBundle

Because each user-system is handled as self-standing its no problem to use multiple entity managers.

You must register the mappings yourself (as Stof described) but you can configure an entity manager per user-system with 'model_manager_name'

@stof the mapping is actually the only issue I have, since I can use custom managers to handle the two different entities and cases where they need to be handled.

I'll give it a try and get back to you. Thanks a lot.

@stof I managed to get it working by using two firewalls in the same context.

Now I'm having issues with querying the alternative database. I'm guessing Doctrine uses the mapping to internally work while we fetch data, so when I do findOneBy on the alternative class repository, nothing comes out or, when it comes, comes without the superclass fields (only the basic fields).

I think I'm going to have to do native queries in order to solve that issue.

Do you have any suggestions?

@stof Here's a valid use case : one DB holds CMS data, with admin users that can use the CMS (add/edit content pages). Another DB holds client information, clients should be able to login on the frontend to view their personal data. The client data has to be stored in a separate database (on a different server) since it contains sensitive data.

It would be nice if FOSUserBundle could deal with these scenario's out of the box. I'm currently testing the bundle mentioned by @sstok.

@wimvds This would require duplicating all services of the bundle, but also all controllers and routing, to have a separate stack for each user type. This would make the bundle much more complex for this use case benefiting only some users, while the extra complexity would impact everyone.
So using the bundle adding this extra feature on top of FOSUserBundle might be better

i do agree with @stof that its not a common use case to have multiple user manager and FOSUser cannot handle this as this would imply too much complexity (for as far as I am concern, the most common case scenarios you only need 1 user manager) for all fosuser community.

But I also agree that its often happens that we do need to handle multiple user managers and would be great if there was a bundle for it :-)

Closing this as configuring the whole bundle multiple times is not something I want to provide.
People really wanting to have multiple user managers should configure services themselves (and wire controllers to do it)

Was this page helpful?
0 / 5 - 0 ratings