3

I am trying to have multiple database connection in Symfony 2 with doctrine but not able to do so.

I have searched extensively in Google and stack-overflow but everywhere it's done via config.yml file or dynamic database where all DB's have same schema/entities .

But for my case the database is determined based on subdomain and the database schema is not same for all subdomains.

Ex:
test1.example.com => Should load test1 db
test2.example.com => Will load test2 db

Both test1 and test2 DB are different are created at DB level and not having entity entries in doctrine.

Can anyone please help me how to do this in Symfony 2.

3
  • Are you trying to do this without ever creating Doctrine entities? If so, how are entity objects managed?
    – geoB
    Dec 7, 2013 at 14:51
  • yes i am not creating doctrine entries as i will not know the schema/table structure before hand.Table and database names are known but not the fields of table as it can be changed/removed/altered by the admin of subdomain. Example: Admin of test3.example.com will add fields/customize the table which are directly done using SQL. After that, other users of test3.example.com will use those tables. I know i can directly query using SQL but it will cause a mess in coding as i have to take care of everything :( Hope there is some way in Doctrine + Symfony 2
    – adithyau
    Dec 7, 2013 at 15:23
  • Even if i create Doctrine Entities for each new DB, it causes more problems as doctrine entries relies on Physical files which is bad for SAAS environment :( (especially during auto scaling up/down based on load in AWS )
    – adithyau
    Dec 7, 2013 at 15:33

2 Answers 2

12

It seems to me that using Doctrines ODM is not the right way to approach this. You can still use Doctrine to connect to databases and query them. But if you have no entity classes the use of an entity manager seems to be inappropriate.

Use Doctrine for Connection handling

Here is how you create a connection to a Database with the doctrine Connection class:

/** @var \Doctrine\Bundle\DoctrineBundle\ConnectionFactory $connectionFactory */
$connectionFactory = $this->getContainer()->get('doctrine.dbal.connection_factory');
$connection = $connectionFactory->createConnection(
    array('pdo' => new \PDO("mysql:host=$hostname;dbname=$dbname", $username, $password))
);

Now you can use $connection as a simple PDO object:

$connection->executeQuery('SELECT * FROM your_table');

You could add this code as a service to make it accessible everywhere.
If you want to connect to a different database for a different domain you can use this code to identify the domain:

$this->getRequest()->getHost();

To access the domain in an action do this:

public function yourAction(Request $request, /* ... */)
{
    // the Controller extends the Container. So need to get it here:
    $connectionFactory = $this->get('doctrine.dbal.connection_factory');

    // also access the domain like this:
    $domain = $request->getHost();
}
3
  • Thanks for the help. I tried to do the same but failed. :( I $this->getContainer() is not available inside the Controller. I googled and found out it has to be done in services. I did the same. Created a service and added the code but still it is not working. I referred this forum.symfony-project.org/viewtopic.php?t=41082&p=132396 to create a service. Can you please help me in this ? Where should we write the getContainer() code and how to use them in our Controller.
    – adithyau
    Dec 8, 2013 at 7:29
  • I have added an example on how to access the Request object in a Controller
    – ferdynator
    Dec 8, 2013 at 12:25
  • Glad I was able to help.
    – ferdynator
    Dec 8, 2013 at 15:53
2

Thanks to byf-ferdy (https://stackoverflow.com/a/20444097/2976700) , i am able to figure out how to use other DB having no doctrine entities. Just use the following code in your action controller

$connectionFactory = $this->get('doctrine.dbal.connection_factory');                
$connection = $connectionFactory->createConnection(
                array('pdo' => new \PDO("mysql:host=$hostname;dbname=$dbname", 
                       $username,$password))
 );
 $query = $connection->executeQuery('SELECT * FROM multi_client');
 $results = $query->fetchAll();

To know the subdomain accessed one can use $domain = $request->getHost();

Accordingly one change change the DB name and other parameters. Hope it helps others

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.