Configuring multiple sessions for PHPCR-ODM

If you need more than one PHPCR backend, you can define sessions as child of the session information. Each session has a name and the configuration following the same schema as what is directly in session. You can also overwrite which session to use as default_session. Once you have multiple sessions, you can also configure multiple document managers with those sessions.

Autowiring always gives you the default session and the default document manager. When working with multiple sessions and managers, you need to explicitly specify the services. For the document managers, you can also go through the manager registry (see at the end of this page).

Multiple PHPCR Sessions

# app/config/config.ymldoctrine_phpcr:    session:        default_session: ~        sessions:            <name>:                workspace: ... # Required                username:  ~                password:  ~                backend:                    # ...                options:                    # ...

Multiple Document Managers

If you are using the ODM, you will also want to configure multiple document managers.

Inside the odm section, you can add named entries in the document_managers. To use the non-default session, specify the session attribute.

# app/config/config.ymlodm:    default_document_manager: ~    document_managers:        <name>:            session: <sessionname>            # ... configuration as above

Bringing it all together

The following full example uses the default manager for AppBundle and the documents provided by the CMF. Additionally, it has a website and DMS manager that connects to the Jackrabbit of Magnolia CMS. That manager looks for models in the MagnoliaBundle.

doctrine_phpcr:    # configure the PHPCR sessions    session:        sessions:            default:                backend: "%phpcr_backend%"                workspace: "%phpcr_workspace%"                username: "%phpcr_user%"                password: "%phpcr_pass%"            website:                backend:                    type: jackrabbit                    url: "%magnolia_url%"                workspace: website                username: "%magnolia_user%"                password: "%magnolia_pass%"            dms:                backend:                    type: jackrabbit                    url: "%magnolia_url%"                workspace: dms                username: "%magnolia_user%"                password: "%magnolia_pass%"    # enable the ODM layer    odm:        auto_generate_proxy_classes: "%kernel.debug%"        document_managers:            default:                session: default                mappings:                    AppBundle: ~                    CmfContentBundle: ~                    CmfMenuBundle: ~                    CmfRoutingBundle: ~            website:                session: website                configuration_id: magnolia.odm_configuration                mappings:                    MagnoliaBundle: ~            dms:                session: dms                configuration_id: magnolia.odm_configuration                mappings:                    MagnoliaBundle: ~

You can access the managers through the manager registry available in the service Doctrine\Bundle\PHPCRBundle\ManagerRegistry:

use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;

/** @var $container \Symfony\Component\DependencyInjection\ContainerInterface */

// get the named manager from the registry
$dm = $container->get(ManagerRegistry::class)->getManager('website');

// get the manager for a specific document class
$dm = $container->get(ManagerRegistry::class)->getManagerForClass('CmfContentBundle:StaticContent');

Additionally, each manager is available as a service in the DI container. The service name pattern is doctrine_phpcr.odm.<name>_document_manager so for example the website manager is called doctrine_phpcr.odm.website_document_manager.