0

I'm not asking you to help me with a code purpose, but for an advice of how to do what I've got to do.

Since a few weeks, i'm developing a web application based on the symfony3 framework, and now, I have to modify what I did because my boss wants me to open the solution on multiple "groups". In fact, the group will correspond with a database which will be the group's content for the application.

I explain with a schema:

Login page -> login / password / group

If GROUP = A, database = db_group_a (for exemple) If GROUP = B, database = db_group_b (..)

I dont know if it's clear, but here's what I have to do, and I really dont know how to do it with Symfony (In simple PHP, I would do it quite simply, but Symfony makes me ask you). If you could help me, it would be awesome. Thanks!

1 Answer 1

2

In symfony you can define multiple databases like this:

$container->loadFromExtension('doctrine', array(
    'dbal' => array(
        'default_connection' => 'default',
        'connections' => array(
            'default' => array(
                'driver'   => '%database_driver%',
                'host'     => '%database_host%',
                'port'     => '%database_port%',
                'dbname'   => '%database_name%',
                'user'     => '%database_user%',
                'password' => '%database_password%',
                'charset'  => 'UTF8',
            ),
            'group_a' => array(
                'driver'   => '%database_driver2%',
                'host'     => '%database_host2%',
                'port'     => '%database_port2%',
                'dbname'   => '%database_name2%',
                'user'     => '%database_user2%',
                'password' => '%database_password2%',
                'charset'  => 'UTF8',
            ),
            'group_b' => array(
                'driver'   => '%database_driver2%',
                'host'     => '%database_host2%',
                'port'     => '%database_port2%',
                'dbname'   => '%database_name2%',
                'user'     => '%database_user2%',
                'password' => '%database_password2%',
                'charset'  => 'UTF8',
            ),
        ),
    ),

Now, once you logged in and found out, what group is the user privileged to, you can set that in your session and add as a parameter to queries similar to this:

$allowed_db = 'group_a';
$customers = $this->get('doctrine')
            ->getRepository('AcmeCustomerBundle:Customer', $allowed_db)
            ->findAll();
Sign up to request clarification or add additional context in comments.

6 Comments

Notice: since I don't have a running Symphony copy on my PC right now, the code example in not tested and taken from the web as example.
Hey, thanks, it looks awesome ! I thought it was only in the config.yml that we had to manage de databases. Well, where do i need to write a code like yours? (the first one)
I understand, thanks :) (is it possible to open a room to discuss? I have a few more questions, really quickly)
Sure, go ahead and ask
can you create the room? I think I can't yet because of my level
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.