1

I'm writing a web-app that will give every client his own database (or schema, since I'm using PostgreSQL) and sub-domain. So what I want is basically:

Of course I don't plan to blindly throw URL parameters around, so after some checking for validity - and given that all my code, schema, etc. etc. are identical for all clients, and the database to connect to is the only difference - is there a way to do this without updating any of the main config files all the time?

I've looked around for some time and found some solutions that don't apply to my scenario because they either assume that different bundles use different databases (I have one bundle using different databases, depending on the client) or that you can simply configure a few different entity managers (I have many clients, not just 5 or 10).

Tom
  • 2,688
  • 3
  • 29
  • 53

1 Answers1

0

You could include a php file in your imports like..

imports:
        ...
    - { resource: parameters.php }
        ....

Then in that page use whatever method you use to get the subdomain and deal with that and set the db variables like..

<?php
require_once('a_file_that_generates_these_parameters.php')

$container->setParameter('database_host', $dbHost);
$container->setParameter('database_port', $dbPort);
$container->setParameter('database_name', $dbName);
$container->setParameter('database_user', $dbUser);
$container->setParameter('database_password', $dbPass);

Which would then be useable in your config in the same way the details in parameters.yml currently is. Like..

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        ....
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • Thanks. This is one way of doing it, but I think it's not very much in line with the rest of Symfony. I found and adapted another solution, it was posted here: http://stackoverflow.com/questions/15108732/symfony2-dynamic-db-connection-early-override-of-doctrine-service/24585284#24585284 If anyone has something better, I'm still very interested. – Tom Jul 05 '14 at 10:12
  • Yeah it's not very pretty and does feel quite hacky. – qooplmao Jul 05 '14 at 10:41