简体   繁体   中英

Using an extended Kohana DB class for multiple databases with ORM

On the current project I'm working on, data is spread across two databases. The method we're trying to use is to use an alias for the second database and then extending the database class to replace the alias with the actual database name.

In /classes/database/mysql.php, we've added this:

class Database_MySQL extends Kohana_Database_MySQL {
    public static $alias;
    public static $sprtDbName;

    public function __construct($name, $config) {
        $con = $config['connection'];
        self::$sprtDbName = "$con[database]_support";
        parent::__construct($name, $config);
    }

    public function query($type, $sql, $as_object = FALSE, array $params = NULL) {
        $sql = str_ireplace('SUPPORT_ALIAS', self::$sprtDbName, $sql);
        return parent::query($type, $sql, $as_object, $params);
    }
}

And in /config/database.php, we have this:

$db_config = array(
    'dev_local' => array(
        'type'      => 'mysql',
        'connection' => array(
            'hostname'  => 'localhost',
            'username'  => 'username',
            'password'  => 'password',
            'database'  => 'db_name'
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
    'support' => array(
        'type'      => 'mysql',
        'connection' => array(
            'hostname'  => 'localhost',
            'username'  => 'username',
            'password'  => 'password',
            'database'  => 'SUPPORT_ALIAS'
        ),
        'table_prefix' => '',
        'charset'      => 'utf8',
        'caching'      => FALSE,
        'profiling'    => TRUE,
    ),
);

Here's the problem: in one of my ORM classes, when I start the class off like this, it works fine:

class Model_Something extends ORM {
    protected $_table_name = 'SUPPORT_ALIAS.something';
    public $doc_id = null;
    public $document_compiled = null;

But when I use this method:

class Model_Something extends ORM {
    protected $_table_name = 'something';
    public $doc_id = null;
    public $document_compiled = null;
    protected $_db = 'support';

I get this error:

Database_Exception [ 1044 ]: Access denied for user 'username'@'localhost' to database 'SUPPORT_ALIAS'

The alias never gets replaced. What am I missing?

I can't really answer your question per se, but I would use the Kohana built-in support for non-standard databases:

http://kohanaframework.org/3.2/guide/orm/models#use-a-non-default-database

So, instead of:

protected $_db = 'support';

Use:

protected $_db_group = 'support';

And thus, no need for the class Database_MySQL extends Kohana_Database_MySQL

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM