【问题标题】:Using Multiple Database Connection Not Working For Extension使用多个数据库连接不适用于扩展
【发布时间】:2023-04-07 12:42:01
【问题描述】:

我正在使用http://www.yiiframework.com/wiki/544/multiple-database-connection-select-database-based-on-login-user-id-dynamic/ 的教程进行多个数据库连接。代码在模型中运行良好。但问题是我正在使用一个扩展,我正在使用 Yii::app()->db; 使用 db 连接;在这里我得到异常属性“CWebApplication.dbadvert”未定义。扩展的控制器是从 CExtController 扩展而来的。请帮忙。

【问题讨论】:

    标签: yii-extensions yii yii-components


    【解决方案1】:

    在您引用的示例中,dbadvert 是为自定义活动记录类 RActiveRecord 设置的,而不是为 Web 应用程序设置的。

    如果你想像Yii::app()->dbadvert 一样使用它,你需要像这样在config.phpcomponents 部分设置它

    'dbadvert' => array(
        'class' => 'CDbConnection'
        *params here*
    ),
    

    UPD

    您可以为 CDbConnection 创建一个包装器组件,它将以您想要的任何方式更改连接字符串并作为 webapp 组件放入。

    <?php
    
    class CMultiuserDatabaseConnection extends CApplicationComponent {
    
            public function __call($name, $params) {
                    $db = $this->db;
                    return call_user_func_array(($db, $name), $params);
            }
    
            public $dbConnectionClass = 'CDbConnection';
            public $connections = null;
            public $defaultConfiguration = null;
    
            public function getDatabaseConfiguration($user) {
                    if (!$this->connections) { return array(); }
                    return array_key_exists($user, $this->connections) ? $this->connections[$user] : $this->defaultConfiguration;
            }
    
            public function getDb() {
                    $user = Yii::app()->user;
                    if ($user->isGuest) { return false; }
                    $username = $user->name;
                    $config = $this->getDatabaseConfiguration($username);
                    if (!$config) { return false; }
                    $dsn = array_key_exists('dsn', $config) ? $config['dsn'] : null;
                    if (!$dsn) { return false; }
                    $user = array_key_exists('user', $config) ? $config['user'] : null;
                    $password = array_key_exists('password', $config) ? $config['password'] : null;
                    $result = new $this->dbConnectionClass($dsn, $user, $password);
                    return $result;
            }
    }
    

    这是一个粗略的组件示例,您可以将其设置为“db”组件,然后您将获得“连接”选项,以这种方式存储每个用户的配置:

    'components' => array(
        ...
        'db' => array(
            'class' => "CMultiuserDatabaseConnection",
            'connections' => array(
                "first-user-name" => array(
                    // just another db configuration here, for user-one
                ),
                "second-user-name" => array(
                    // just another db configuration herer, for user two
                ),
                ...
            ),
            'defaultConfiguration' => array(
                /*
                * here goes configuration for all other user, that were not specified 
                * in connections.
                */
            ),
        ),
        ...
    ),
    

    【讨论】:

    • 但我想动态更改连接字符串。如果我把它写在配置文件中,那就不可能了。
    • 请举个例子。
    • 如何根据登录用户使用它来更改数据库?
    • @user1690835 我已经删除了我的 cmets 并将所有内容从它们移到答案的 UPD 部分,看看那里。
    • 用户是在数据库中动态创建的。所以我不能使用你的代码。
    【解决方案2】:

    我将模型中的扩展查询编写为函数。并在 CExtController 中创建了模型的一个实例。然后我调用了这些函数,一切正常。

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      相关资源
      最近更新 更多