0

我有一个 symfony 网站,可以在两种不同的环境中浏览。我的意思是上下文,而不是应用程序(我使用ysfDimensionsPlugin)。在第一个上下文中,我使用绑定到数据库 db1 的 sfGuard 对用户进行身份验证;在第二种情况下,我使用 sfGuard 对用户进行身份验证,但绑定到数据库 db2。

这两个连接在 databases.yml 中定义为标准 sfDoctrineDatabase 对象。在 schema.yml 中,我将 sfGuard 组件绑定到 db1 连接。所以在我的 sfGuard 基类中,我有这个:

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

如果我在第二个上下文中,我尝试做的是动态地将 sfGuard 组件绑定到 db2 连接。所以在全局 preExecute 方法中,我这样做:

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

绑定已完成,但在进行查询时会立即被覆盖:sfAutoload 加载 sfGuard 类,包括基类,调用以下代码:

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

所以我问:你将如何实现它来解决它?

4

1 回答 1

0

我不确定,但你可以试试这个。将绑定参数存储在文件(yml 或 json)中。绑定将根据文件中的参数(变量)发生。如果它包含 2 则绑定两者,否则绑定文件中存在的一个。

例如,您有一个 json 文件

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

在 app.yml

all:
  bind: context1

您阅读 app.yml 并绑定到 json 文件中的上下文参数。假设 app.yml 中的绑定值为 context1。然后,您将在 preExecute 函数中将 sfGuardUser 与 db1 绑定。

您可以使用动态更改 app.yml 值

sfConfig::set('app_bind',"context2");
于 2012-01-05T15:07:45.313 回答