문제

I am doing multiple database connection using the tutorial at http://www.yiiframework.com/wiki/544/multiple-database-connection-select-database-based-on-login-user-id-dynamic/ . The code is working fine in the model. But the problem is I am using an extension where I am using db connection using Yii::app()->db; Here I am getting exception Property "CWebApplication.dbadvert" is not defined. The controller of the extension is extended from CExtController. Please help.

도움이 되었습니까?

해결책 2

I wrote the query for the extension in a model as functions. and in the CExtController created an instance of the model. Then I called those functions and everything is working fine.

다른 팁

In the example you're referring dbadvert is set up for custom active record class RActiveRecord, not for the web application.

If you want to use it like Yii::app()->dbadvert, you would need to set it up in components section of your config.php like this

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

UPD

You can create a wrapper component for CDbConnection, that will change the connection string any way you want and put in as a webapp component.

<?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;
        }
}

That's a crude example of component, which you can set up as your 'db' component and then you'll get 'connections' option for storing the per-user configuration in that way:

'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.
            */
        ),
    ),
    ...
),
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top