Dynamically bind component to connection

678 Views Asked by At

I have a symfony web site that can be browsed in two different contexts. I mean contexts, not applications (I use ysfDimensionsPlugin). In the first context, I authenticate the users using sfGuard bound to a database db1; in the second context, I authenticate the users using sfGuard but bound to a database db2.

Those 2 connections are defined in databases.yml as standard sfDoctrineDatabase objects. In schema.yml, I bind the sfGuard components to the db1 connection. So in my sfGuard base classes, I have this:

Doctrine_Manager::getInstance()->bindComponent('sfGuardUser', 'db1');

What I try to do is to dynamically bind the sfGuard components to the db2 connection if I am in the second context. So in a global preExecute method, I do this:

Doctrine_Manager::getInstance()->bindComponent('sfGuardUser', 'db2');
Doctrine_Manager::getInstance()->bindComponent('sfGuardGroup', 'db2');

The binding is made, but it is immediately overwritten when a query is made: sfAutoload loads the sfGuard classes, including the base classes, calling this:

Doctrine_Manager::getInstance()->bindComponent('sfGuardUser', 'db1');

So I'm asking: how would you implement that to work it out?

1

There are 1 best solutions below

3
On

I'm not sure but u can try this. Store the binding arguments in a file(yml or json). The binding will happen based on the arguments(variables) in the file. If it contains 2 then bind both else bind the one present in the file.

for eg you have a json file

{
    "context1": {
        "db1": [
            "sfGuardUser"
        ]
    },
    "context2": {
        "db2": [
            "sfGuardUser",
            "sfGuardGroup"
        ]
    }
}

And in app.yml

all:
  bind: context1

You read the app.yml and bind to the context arguments in the json file. Suppose bind value in app.yml is context1. Then you will bind sfGuardUser with db1 in your preExecute function.

You can change the app.yml values dynamically by using

sfConfig::set('app_bind',"context2");