FpOpenIdBundle icon indicating copy to clipboard operation
FpOpenIdBundle copied to clipboard

add support of multiple entity managers

Open YahouSerious opened this issue 12 years ago • 11 comments

Is it possible to add a configuration option "entity_manager" for multitenancy ? Thanks

YahouSerious avatar May 29 '13 17:05 YahouSerious

Sorry, I dont get what you want. Could you be more descriptive?

  • Where to add it? bundle config security factory config?
  • multitenancy - what is this?

makasim avatar May 30 '13 06:05 makasim

I'm working on a multi customer application( multi database connection ). Each client got his own database, so I need to use a custom entity manager. Your "identity_class" use the default entity manager. This could be a good thing to use custom entity manager(in my case :)).

cf: http://symfony.com/doc/master/cookbook/doctrine/multiple_entity_managers.html

YahouSerious avatar May 30 '13 07:05 YahouSerious

Good question. I dont know a good way to solve it, honestly I dont even have time to investigate it deeper. So you do a PR, that's would be really good.

Solution for know to create your own provider and use it. or overwrite this service

makasim avatar May 30 '13 08:05 makasim

closed by mistake

makasim avatar May 30 '13 08:05 makasim

ok, but at this time, i got trouble with class OpenIdIdentity. It can't inherite from Fp\OpenIdBundle\Entity\UserIdentity. Are you sure you must not put something like "@ORM\Column(name="identity", type="string")" in your classes ? Thanks.

YahouSerious avatar May 30 '13 18:05 YahouSerious

make sure doctrine auto mapping enabled.

makasim avatar May 31 '13 05:05 makasim

I can't enable auto mapping when several entity managers are defined

YahouSerious avatar May 31 '13 10:05 YahouSerious

so you have to add fp openid mappings explisitly, use this example:

doctrine:
    orm:
        entity_managers:
            default:
                # The name of a DBAL connection (the one marked as default is used if not set)
                connection:                               default
                mappings: # Required
                    CoreBundle:                           { type: annotation, dir: Entity/, alias: '' }
                    JMSPaymentCoreBundle:                 { type: xml, dir: Resources/config/doctrine }

makasim avatar May 31 '13 10:05 makasim

Like this ? mappings: FpOpenIdBundle: type: annotation prefix: Fp\OpenId\Entity dir: "%kernel.root_dir%/../vendor/fp/openid-bundle/Fp/OpenIdBundle/Entity" alias: 'FpOpenId' is_bundle: false

YahouSerious avatar May 31 '13 11:05 YahouSerious

no, fpopenid uses xml for schemas, so that would be (not tested):

mappings: 
    FpOpenIdBundle: { type: xml, dir: Resources/config/doctrine }

makasim avatar May 31 '13 11:05 makasim

Thanks it works ! :)

Now i'm trying to inject the custom entity manager ...

config.yml:

# OpenId
fp_open_id:
    db_driver: orm
    entity_manager: "@doctrine.orm.customer_entity_manager"
    identity_class: App\SiteBundle\Entity\OpenIdIdentity

Configuration.php

/**
     * 
     *  {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('fp_open_id');

        $rootNode
            ->children()
                ->scalarNode('db_driver')->defaultNull()->end()
                ->scalarNode('entity_manager')->defaultValue('doctrine.orm.entity_manager')->end()
                ->scalarNode('identity_class')->defaultNull()->end()
            ->end()
        ;

        $this->addTemplateSection($rootNode);

        return $treeBuilder;
    }

FpOpenIdExtension.php

// ....

/**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        // ... 

        if ($configs['entity_manager']) {
            $this->loadEntityManagerr($configs, $container, $loader);
        }
    }

/**
     * @param array $configs
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
     *
     * @throws \InvalidArgumentException
     */
    public function loadEntityManagerr(array $configs, ContainerBuilder $container, $loader)
    {
        $container->setParameter('entity_manager', $configs['entity_manager']);
    }

And here is the trouble, i don't know how inject the entity manager in fp_openid.identity_manager service:

<services>
        <service id="fp_openid.identity_manager" class="%fp_openid.identity_manager.class%">
           <!--  <argument type="service" id="doctrine.orm.entity_manager" /> --> 
            <argument type="service" id="%entity_manager%" />
            <argument>%fp_openid.model.identity.class%</argument>
        </service>

    </services>

Thanks for your help

YahouSerious avatar Jun 01 '13 06:06 YahouSerious