Set document manager at run time in symfony3 mongodb bundle?

I’m building a multi tenant app with each client having a different database on the same server (mongoDB). I want to set the appropriate client (tenant) database based on an api parameter that will be passed to each request. In other words, the database and document manager should be set at run time based on the tenant’s site from which the request originated. The architecture is such that there’s a single auth database to store the user and allow for authentication and a single user can have separate profiles for different tenants. When the user authenticates, I set a JWT token whose payload includes the identifier of the tenant site. Each subsequent requests, I retrieve the tenant ID and now I want to select the database/document manger of that tenant to be used to handle that request. I’ve looked into several possibilities but I’m not able to come up with a concrete solution to this problem. Has anyone done this before using symfony/mongoDB?


Source: stackoverflow-php